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

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

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

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

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

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

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

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

        [Column("kmw")]
        public double Kmw { get; set; }

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

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

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

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

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

        [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("gerebelt")]
        public bool IsGerebelt { get; set; }

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

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

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

        [Column("lesewagen")]
        public bool? IsLesewagen { 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("comment")]
        public string? Comment { get; set; }

        [InverseProperty("Part")]
        public virtual ISet<DeliveryPartAttr> PartAttributes { get; private set; }

        [NotMapped]
        public IEnumerable<WineAttr> Attributes => PartAttributes.Select(a => a.Attr);

        [InverseProperty("Part")]
        public virtual ISet<DeliveryPartModifier> PartModifiers { get; private set; }

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