using Elwig.Helpers;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;

namespace Elwig.Models.Entities {
    [Table("delivery_part"), PrimaryKey("Year", "DId", "DPNr")]
    public class DeliveryPart : IDelivery {
        [Column("year")]
        public int Year { get; set; }

        [Column("did")]
        public int DId { get; set; }

        [ForeignKey("Year, DId")]
        public virtual Delivery Delivery { get; private set; } = null!;

        [Column("dpnr")]
        public int DPNr { get; set; }

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

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

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

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

        [Column("cultid")]
        public string? CultId { get; set; }

        [ForeignKey("CultId")]
        public virtual WineCult? Cultivation { get; private set; }

        [Column("weight")]
        public int Weight { get; set; }

        [Column("kmw")]
        public double Kmw { get; set; }
        [NotMapped]
        public double Oe {
            get => Utils.KmwToOe(Kmw);
            set => Kmw = Utils.OeToKmw(value);
        }

        [Column("qualid")]
        public required string QualId { get; set; }

        [ForeignKey("QualId")]
        public virtual WineQualLevel Quality { get; private set; } = null!;

        [Column("hkid")]
        public required string HkId { get; set; }

        [ForeignKey("HkId")]
        public virtual WineOrigin Origin { get; private set; } = null!;

        [Column("kgnr")]
        public int? KgNr { get; set; }

        [ForeignKey("KgNr")]
        public virtual WbKg? Kg { get; private set; }

        [Column("rdnr")]
        public int? RdNr { get; set; }

        [ForeignKey("KgNr, RdNr")]
        public virtual WbRd? Rd { get; private set; }

        [Column("net_weight")]
        public bool IsNetWeight { get; set; }

        [Column("manual_weighing")]
        public bool IsManualWeighing { get; set; }

        [Column("spl_check")]
        public bool IsSplCheck { get; set; }

        [Column("hand_picked")]
        public bool? IsHandPicked { get; set; }

        [Column("lesewagen")]
        public bool? IsLesewagen { get; set; }

        [Column("gebunden")]
        public bool? IsGebunden { get; set; }

        [Column("temperature")]
        public double? Temperature { get; set; }

        [Column("acid")]
        public double? Acid { get; set; }

        [Column("scale_id")]
        public string? ScaleId { get; set; }

        [Column("weighing_id")]
        public string? WeighingId { get; set; }

        [Column("weighing_reason")]
        public string? WeighingReason { get; set; }

        [Column("comment")]
        public string? Comment { get; set; }

        [InverseProperty(nameof(DeliveryPartModifier.Part))]
        public virtual ICollection<DeliveryPartModifier> PartModifiers { get; private set; } = null!;

        [NotMapped]
        public IEnumerable<Modifier> Modifiers => PartModifiers.Select(m => m.Modifier).OrderBy(m => m.Ordering);

        [InverseProperty(nameof(PaymentDeliveryPart.DeliveryPart))]
        public virtual PaymentDeliveryPart? Payment { get; private set; }

        [NotMapped]
        public string OriginString => Origin.OriginString + "\n" + (Kg?.Gl != null ? $" / {Kg.Gl.Name}" : "") + (Kg != null ? $" / {Kg.AtKg.Gem.Name} / KG {Kg.AtKg.Name}" : "") + (Rd != null ? $" / Ried {Rd.Name}" : "");

        [InverseProperty(nameof(DeliveryPartBucket.Part))]
        public virtual ICollection<DeliveryPartBucket> Buckets { get; private set; } = null!;
    }
}
