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

namespace Elwig.Models.Entities {
    [Table("delivery_schedule"), PrimaryKey("Year", "DsNr")]
    public class DeliverySchedule {

        [Column("year")]
        public int Year { get; set; }

        [Column("dsnr")]
        public int DsNr { get; set; }

        [Column("date")]
        public required string DateString { get; set; }
        [NotMapped]
        public DateOnly Date {
            get => DateOnly.ParseExact(DateString, "yyyy-MM-dd");
            set => DateString = value.ToString("yyyy-MM-dd");
        }

        [Column("zwstid")]
        public required string ZwstId { get; set; }

        [Column("description")]
        public required string Description { get; set; }

        [NotMapped]
        public string Identifier => $"{Date:dd.MM.} - {ZwstId} - {Description}";

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

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

        [Column("max_weight")]
        public int? MaxWeight { get; set; }
        [NotMapped]
        public int AnnouncedWeight => AnnouncedWeightOverride ?? Announcements.Sum(a => a.Weight);
        [NotMapped]
        public int? AnnouncedWeightOverride { get; set; }
        [NotMapped]
        public double? Percent => (double)AnnouncedWeight / MaxWeight * 100;

        [Column("cancelled")]
        public bool IsCancelled { get; set; }
        [NotMapped]
        public TextDecorationCollection? TextDecoration => IsCancelled ? TextDecorations.Strikethrough : null;

        [Column("ancmt_from")]
        public long? AncmtFromUnix { get; set; }
        [NotMapped]
        public DateTime? AncmtFrom {
            get => AncmtFromUnix != null ? DateTimeOffset.FromUnixTimeSeconds(AncmtFromUnix.Value).LocalDateTime : null;
            set => AncmtFromUnix = value != null ? new DateTimeOffset(value.Value).ToUnixTimeSeconds() : null;
        }

        [Column("ancmt_to")]
        public long? AncmtToUnix { get; set; }
        [NotMapped]
        public DateTime? AncmtTo {
            get => AncmtToUnix != null ? DateTimeOffset.FromUnixTimeSeconds(AncmtToUnix.Value).LocalDateTime : null;
            set => AncmtToUnix = value != null ? new DateTimeOffset(value.Value).ToUnixTimeSeconds() : null;
        }

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

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

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

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

        [InverseProperty(nameof(DeliveryScheduleWineVar.Schedule))]
        public virtual ICollection<DeliveryScheduleWineVar> Varieties { get; private set; } = null!;

        [InverseProperty(nameof(DeliveryAncmt.Schedule))]
        public virtual ICollection<DeliveryAncmt> Announcements { get; private set; } = null!;

        public int SearchScore(IEnumerable<string> keywords) {
            return Utils.GetSearchScore([Description], keywords);
        }
    }
}
