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

namespace Elwig.Models.Entities {
    [Table("area_commitment_type"), PrimaryKey("VtrgId"), Index("SortId", "AttrId", "Discriminator", IsUnique = true)]
    public class AreaComType {
        [Column("vtrgid")]
        public required string VtrgId { get; set; }

        [Column("sortid")]
        public required string SortId { get; set; }

        [Column("attrid")]
        public string? AttrId { get; set; }

        [Column("disc")]
        public string? Discriminator { get; set; }

        [Column("min_kg_per_ha")]
        public int? MinKgPerHa { get; set; }

        [Column("penalty_per_kg")]
        public long? PenaltyPerKgValue { get; set; }
        [NotMapped]
        public decimal? PenaltyPerKg {
            get => PenaltyPerKgValue != null ? Utils.DecFromDb(PenaltyPerKgValue.Value, 4) : null;
            set => PenaltyPerKgValue = value != null ? Utils.DecToDb(value.Value, 4) : null;
        }

        [Column("penalty_amount")]
        public long? PenaltyAmoutValue { get; set; }
        [NotMapped]
        public decimal? PenaltyAmount {
            get => PenaltyAmoutValue != null ? Utils.DecFromDb(PenaltyAmoutValue.Value, 4) : null;
            set => PenaltyAmoutValue = value != null ? Utils.DecToDb(value.Value, 4) : null;
        }

        [Column("penalty_none")]
        public long? PenaltyNoneValue { get; set; }
        [NotMapped]
        public decimal? PenaltyNone {
            get => PenaltyNoneValue != null ? Utils.DecFromDb(PenaltyNoneValue.Value, 4) : null;
            set => PenaltyNoneValue = value != null ? Utils.DecToDb(value.Value, 4) : null;
        }

        [ForeignKey("SortId")]
        public virtual WineVar WineVar { get; private set; } = null!;

        [ForeignKey("AttrId")]
        public virtual WineAttr? WineAttr { get; private set; }

        [NotMapped]
        public string DisplayName => WineVar.Name + (WineAttr != null ? $" {WineAttr.Name}" : "") + (Discriminator != null ? $" ({Discriminator})" : "");
    }
}
