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

namespace Elwig.Models.Entities {
    [Table("delivery"), PrimaryKey("Year", "DId"), Index("DateString", "ZwstId", "LNr", IsUnique = true), Index("LsNr", IsUnique = true)]
    public class Delivery {
        [Column("year")]
        public int Year { get; set; }

        [Column("did")]
        public int DId { 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("time")]
        public string? TimeString { get; set; }

        [NotMapped]
        public TimeOnly? Time {
            get => (TimeString == null) ? null : TimeOnly.ParseExact(TimeString, "HH:mm:ss");
            set => TimeString = value?.ToString("HH:mm:ss");
        }

        [NotMapped]
        public DateTime DateTime {
            get => Date.ToDateTime(Time ?? TimeOnly.MinValue);
            set {
                Date = DateOnly.FromDateTime(value);
                Time = TimeOnly.FromDateTime(value);
            }
        }

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

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

        [Column("lnr")]
        public int LNr { get; set; }

        [Column("lsnr")]
        public required string LsNr { get; set; }

        [Column("mgnr")]
        public int MgNr { get; set; }

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

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

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

        [InverseProperty("Delivery")]
        public virtual ISet<DeliveryPart> Parts { get; private set; } = null!;
        [NotMapped]
        public IEnumerable<DeliveryPart> FilteredParts => PartFilter == null ? Parts : Parts.Where(p => PartFilter(p));

        [NotMapped]
        public Predicate<DeliveryPart>? PartFilter { get; set; }

        public int Weight => Parts.Select(p => p.Weight).Sum();
        public int FilteredWeight => FilteredParts.Select(p => p.Weight).Sum();

        public IEnumerable<string> SortIds => Parts
            .GroupBy(p => p.SortId)
            .OrderByDescending(g => g.Select(p => p.Weight).Sum())
            .Select(g => g.Key);
        public IEnumerable<string> FilteredSortIds => FilteredParts
            .GroupBy(p => p.SortId)
            .OrderByDescending(g => g.Select(p => p.Weight).Sum())
            .Select(g => g.Key);

        public string SortIdString => string.Join(", ", SortIds);
        public string FilteredSortIdString => string.Join(", ", FilteredSortIds);

        public double Kmw => Utils.AggregateDeliveryPartsKmw(Parts);
        public double FilteredKmw => Utils.AggregateDeliveryPartsKmw(FilteredParts);

        public double Oe => Utils.KmwToOe(Kmw);
        public double FilteredOe => Utils.KmwToOe(FilteredKmw);

        public int SearchScore(IEnumerable<string> keywords) {
            var list = new string?[] {
                LsNr, Time?.ToString("HH:mm"),
                Member.FamilyName, Member.MiddleName, Member.GivenName, Member.BillingAddress?.Name,
                Comment
            }.ToList();
            list.AddRange(Parts.Select(p => p.Comment).Distinct());
            return Utils.GetSearchScore(list, keywords);
        }
    }
}
