using Elwig.Helpers;
using Microsoft.EntityFrameworkCore;
using System;
using System.ComponentModel.DataAnnotations.Schema;

namespace Elwig.Models.Entities {
    [Table("wine_quality_level"), PrimaryKey("QualId")]
    public class WineQualLevel : IEquatable<WineQualLevel> {
        [Column("qualid")]
        public string QualId { get; private set; } = null!;

        [Column("origin_level")]
        public int? OriginLevel { get; private set; }

        [Column("predicate")]
        public bool IsPredicate { get; private set; }

        [Column("min_kmw")]
        public double? MinKmw { get; private set; }

        [NotMapped]
        public double? MinOe => MinKmw != null ? Utils.KmwToOe((double)MinKmw) : null;

        [Column("name")]
        public string Name { get; private set; } = null!;

        public string MinKmwStr => (MinKmw == null) ? "" : $"(mind. {MinKmw:#.0}°)";

        public override bool Equals(object? obj) {
            return Equals(obj as WineQualLevel);
        }

        public bool Equals(WineQualLevel? obj) {
            return QualId == obj?.QualId;
        }

        public static bool operator ==(WineQualLevel? q1, WineQualLevel? q2) {
            return q1?.Equals(q2) ?? Equals(q1, q2);
        }

        public static bool operator !=(WineQualLevel? q1, WineQualLevel? q2) {
            return !(q1?.Equals(q2) ?? Equals(q1, q2));
        }

        public override int GetHashCode() {
            return QualId.GetHashCode();
        }
    }
}
