using Elwig.Helpers;
using Elwig.Models.Entities;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Elwig.Documents {
    public class DeliveryJournal : Document {

        public string Filter;
        public IEnumerable<DeliveryPart> Deliveries;

        public DeliveryJournal(string filter, IEnumerable<DeliveryPart> deliveries) : base($"Lieferjournal {filter}") {
            Filter = filter;
            Deliveries = deliveries;
        }

        public DeliveryJournal(string filter, IQueryable<DeliveryPart> deliveries) :
            this(filter, deliveries
                .Include(p => p.Delivery).ThenInclude(d => d.Member)
                .Include(p => p.Variety)
                .ToList()) { }

        public DeliveryJournal(AppDbContext ctx, DateOnly date) :
            this(date.ToString("dd.MM.yyyy"), ctx.DeliveryParts
                .Where(p => p.Delivery.DateString == date.ToString("yyy-MM-dd"))
                .OrderBy(p => p.Delivery.DateString)
                .ThenBy(p => p.Delivery.TimeString)
                .ThenBy(p => p.Delivery.LsNr)
                .ThenBy(p => p.DPNr)) { }
    }
}
