using Elwig.Helpers;
using Elwig.Models.Entities;
using Elwig.Services;
using Elwig.ViewModels;
using Microsoft.EntityFrameworkCore;

namespace Tests.UnitTests.ServiceTests {
    [TestFixture]
    public class DeliveryServiceTest {

        private static async Task InitViewModel(DeliveryAdminViewModel vm) {
            using var ctx = new AppDbContext();
            vm.MemberSource = await ctx.Members.ToListAsync();
            vm.BranchSource = await ctx.Branches.ToListAsync();
            vm.WineVarSource = await ctx.WineVarieties.ToListAsync();
            List<object> attrs = (await ctx.WineAttributes.ToListAsync()).Cast<object>().ToList();
            attrs.Insert(0, new NullItem());
            vm.WineAttrSource = attrs;
            List<object> cults = (await ctx.WineCultivations.ToListAsync()).Cast<object>().ToList();
            cults.Insert(0, new NullItem());
            vm.WineCultSource = cults;
            vm.WineQualityLevelSource = await ctx.WineQualityLevels.ToListAsync();
            vm.WineOriginSource = await ctx.WineOrigins.ToListAsync();
            vm.WineKgSource = await ctx.Katastralgemeinden.ToListAsync();
            vm.ModifiersSource = await ctx.Modifiers.Where(m => m.Year == 2022).ToListAsync();
        }

        private static async Task<Delivery?> GetDelivery(string lsnr) {
            using var ctx = new AppDbContext();
            return await ctx.Deliveries
                .Where(d => d.LsNr == lsnr)
                .Include(d => d.Parts)
                .ThenInclude(p => p.PartModifiers)
                .ThenInclude(m => m.Modifier)
                .AsSplitQuery()
                .FirstOrDefaultAsync();
        }

        private static DeliveryPart[] GetParts(Delivery d) {
            return [.. d.Parts.OrderBy(p => p.DPNr)];
        }

        [Test]
        public async Task TestCreate_01_Minimal() {
            var vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);

            vm.Date = "01.10.2022";
            vm.Branch = vm.BranchSource.First();
            vm.MgNr = 101;
            vm.Member = vm.MemberSource.First(m => m.MgNr == vm.MgNr);
            vm.SortId = "GV";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.GradationKmw = 15.0;
            vm.GradationOe = 73;
            vm.Weight = 1234;
            vm.IsManualWeighing = true;
            vm.IsNetWeight = false;
            vm.WineQualityLevel = vm.WineQualityLevelSource.First(l => l.QualId == "QUW");
            vm.WineOrigin = vm.WineOriginSource.First(o => o.HkId == "WLNO");

            var p1 = await vm.UpdateDeliveryPart(2022, null, null, false, false, true);
            Assert.That(p1.Delivery.LsNr, Is.EqualTo("20221001X001"));

            var d = await GetDelivery("20221001X001");

            Assert.That(d, Is.Not.Null);
            Assert.That(d.Parts, Has.Count.EqualTo(1));
            var p = d.Parts.First();
            Assert.That(p, Is.Not.Null);
            Assert.Multiple(() => {
                Assert.That(d.LsNr, Is.EqualTo("20221001X001"));
                Assert.That(d.Date, Is.EqualTo(new DateOnly(2022, 10, 1)));
                Assert.That(d.ZwstId, Is.EqualTo("X"));
                Assert.That(d.MgNr, Is.EqualTo(101));
                Assert.That(p.SortId, Is.EqualTo("GV"));
                Assert.That(p.AttrId, Is.Null);
                Assert.That(p.CultId, Is.Null);
                Assert.That(p.Kmw, Is.EqualTo(15.0));
                Assert.That(p.Oe, Is.EqualTo(73));
                Assert.That(p.QualId, Is.EqualTo("QUW"));
                Assert.That(p.Weight, Is.EqualTo(1234));
                Assert.That(p.IsNetWeight, Is.False);
                Assert.That(p.IsManualWeighing, Is.True);
                Assert.That(p.HkId, Is.EqualTo("WLNO"));
            });

            vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);
            Assert.DoesNotThrow(() => {
                vm.FillInputs(d);
                vm.FillInputs(p);
            });
            Assert.Multiple(() => {
                Assert.That(vm.LsNr, Is.EqualTo("20221001X001"));
                Assert.That(vm.Date, Is.EqualTo("01.10.2022"));
                Assert.That(vm.Branch?.ZwstId, Is.EqualTo("X"));
                Assert.That(vm.MgNr, Is.EqualTo(101));
                Assert.That(vm.SortId, Is.EqualTo("GV"));
                Assert.That(vm.WineAttr?.AttrId, Is.Null);
                Assert.That(vm.WineCult?.CultId, Is.Null);
                Assert.That(vm.GradationKmw, Is.EqualTo(15.0));
                Assert.That(vm.WineQualityLevel?.QualId, Is.EqualTo("QUW"));
                Assert.That(vm.Weight, Is.EqualTo(1234));
                Assert.That(vm.IsNetWeight, Is.False);
                Assert.That(vm.IsManualWeighing, Is.True);
                Assert.That(vm.WineOrigin?.HkId, Is.EqualTo("WLNO"));
            });
        }

        [Test]
        public async Task TestCreate_02_Advanced() {
            var vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);

            vm.Date = "02.10.2022";
            vm.Branch = vm.BranchSource.First();
            vm.MgNr = 102;
            vm.Member = vm.MemberSource.First(m => m.MgNr == vm.MgNr);
            vm.SortId = "ZW";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.WineAttr = vm.WineAttrSource.Skip(1).First() as WineAttr;
            vm.WineCult = vm.WineCultSource.Skip(1).First() as WineCult;
            vm.GradationKmw = 15.9;
            vm.GradationOe = 78;
            vm.Weight = 3456;
            vm.IsManualWeighing = false;
            vm.IsNetWeight = true;
            vm.WeighingData = "{}";
            vm.WineQualityLevel = vm.WineQualityLevelSource.First(l => l.QualId == "QUW");
            vm.WineOrigin = vm.WineOriginSource.First(o => o.HkId == "WLNO");
            vm.Modifiers.Add(vm.ModifiersSource.First());

            var p1 = await vm.UpdateDeliveryPart(2022, null, null, false, false, true);
            Assert.That(p1.Delivery.LsNr, Is.EqualTo("20221002X001"));

            var d = await GetDelivery("20221002X001");

            Assert.That(d, Is.Not.Null);
            Assert.That(d.Parts, Has.Count.EqualTo(1));
            var p = d.Parts.First();
            Assert.That(p, Is.Not.Null);
            Assert.Multiple(() => {
                Assert.That(d.LsNr, Is.EqualTo("20221002X001"));
                Assert.That(d.Date, Is.EqualTo(new DateOnly(2022, 10, 2)));
                Assert.That(d.ZwstId, Is.EqualTo("X"));
                Assert.That(d.MgNr, Is.EqualTo(102));
                Assert.That(p.SortId, Is.EqualTo("ZW"));
                Assert.That(p.AttrId, Is.Not.Null);
                Assert.That(p.CultId, Is.Not.Null);
                Assert.That(p.Kmw, Is.EqualTo(15.9));
                Assert.That(p.Oe, Is.EqualTo(78));
                Assert.That(p.QualId, Is.EqualTo("QUW"));
                Assert.That(p.Weight, Is.EqualTo(3456));
                Assert.That(p.IsNetWeight, Is.True);
                Assert.That(p.IsManualWeighing, Is.False);
                Assert.That(p.WeighingData, Is.EqualTo("{}"));
                Assert.That(p.HkId, Is.EqualTo("WLNO"));
                Assert.That(p.Modifiers.Count(), Is.EqualTo(1));
            });

            vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);
            Assert.DoesNotThrow(() => {
                vm.FillInputs(d);
                vm.FillInputs(p);
            });
            Assert.Multiple(() => {
                Assert.That(vm.LsNr, Is.EqualTo("20221002X001"));
                Assert.That(vm.Date, Is.EqualTo("02.10.2022"));
                Assert.That(vm.Branch?.ZwstId, Is.EqualTo("X"));
                Assert.That(vm.MgNr, Is.EqualTo(102));
                Assert.That(vm.SortId, Is.EqualTo("ZW"));
                Assert.That(vm.WineAttr?.AttrId, Is.Not.Null);
                Assert.That(vm.WineCult?.CultId, Is.Not.Null);
                Assert.That(vm.GradationKmw, Is.EqualTo(15.9));
                Assert.That(vm.WineQualityLevel?.QualId, Is.EqualTo("QUW"));
                Assert.That(vm.Weight, Is.EqualTo(3456));
                Assert.That(vm.IsNetWeight, Is.True);
                Assert.That(vm.IsManualWeighing, Is.False);
                Assert.That(vm.WeighingData, Is.EqualTo("{}"));
                Assert.That(vm.WineOrigin?.HkId, Is.EqualTo("WLNO"));
                Assert.That(vm.Modifiers, Has.Count.EqualTo(1));
            });
        }

        [Test]
        public async Task TestCreate_03_TwoParts() {
            var vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);

            vm.Date = "03.10.2022";
            vm.Branch = vm.BranchSource.First();
            vm.MgNr = 101;
            vm.Member = vm.MemberSource.First(m => m.MgNr == vm.MgNr);
            vm.SortId = "GV";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.GradationKmw = 15.0;
            vm.GradationOe = 73;
            vm.WineQualityLevel = vm.WineQualityLevelSource.First(l => l.QualId == "QUW");
            vm.Weight = 1234;
            vm.IsManualWeighing = true;
            vm.IsNetWeight = true;
            vm.WineOrigin = vm.WineOriginSource.First(o => o.HkId == "WLNO");
            var p1 = await vm.UpdateDeliveryPart(2022, null, null, false, false, true);

            Assert.That(p1.Delivery.LsNr, Is.EqualTo("20221003X001"));
            vm.FillInputs(p1.Delivery);

            vm.SortId = "WR";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.GradationKmw = 18.0;
            vm.GradationOe = 89;
            vm.WineQualityLevel = vm.WineQualityLevelSource.First(l => l.QualId == "KAB");
            vm.Weight = 2345;
            var p2 = await vm.UpdateDeliveryPart(p1.Year, p1.DId, null, false, false, true);

            Assert.That(p2.Delivery.LsNr, Is.EqualTo("20221003X001"));

            var d = await GetDelivery("20221003X001");

            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                Assert.That(d.Parts, Has.Count.EqualTo(2));
                Assert.That(d.MgNr, Is.EqualTo(101));
            });
            var p = d.Parts.First();
            Assert.That(p, Is.Not.Null);
            Assert.Multiple(() => {
                Assert.That(p.DPNr, Is.EqualTo(1));
                Assert.That(p.SortId, Is.EqualTo("GV"));
            });
            p = d.Parts.Skip(1).First();
            Assert.That(p, Is.Not.Null);
            Assert.Multiple(() => {
                Assert.That(p.DPNr, Is.EqualTo(2));
                Assert.That(p.SortId, Is.EqualTo("WR"));
            });
        }

        [Test]
        public async Task TestCreate_04_ChangeMember() {
            var vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);

            vm.Date = "04.10.2022";
            vm.Branch = vm.BranchSource.First();
            vm.MgNr = 101;
            vm.Member = vm.MemberSource.First(m => m.MgNr == vm.MgNr);
            vm.SortId = "GV";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.GradationKmw = 15.0;
            vm.GradationOe = 73;
            vm.WineQualityLevel = vm.WineQualityLevelSource.First(l => l.QualId == "QUW");
            vm.Weight = 1234;
            vm.IsManualWeighing = true;
            vm.IsNetWeight = true;
            vm.WineOrigin = vm.WineOriginSource.First(o => o.HkId == "WLNO");
            var p1 = await vm.UpdateDeliveryPart(2022, null, null, false, false, true);

            Assert.That(p1.Delivery.LsNr, Is.EqualTo("20221004X001"));
            vm.FillInputs(p1.Delivery);

            vm.MgNr = 102;
            vm.Member = vm.MemberSource.First(m => m.MgNr == vm.MgNr);
            vm.SortId = "GV";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.GradationKmw = 15.0;
            vm.GradationOe = 73;
            vm.WineQualityLevel = vm.WineQualityLevelSource.First(l => l.QualId == "QUW");
            vm.Weight = 1234;
            vm.IsManualWeighing = true;
            vm.IsNetWeight = true;
            vm.WineOrigin = vm.WineOriginSource.First(o => o.HkId == "WLNO");

            var p2 = await vm.UpdateDeliveryPart(p1.Year, p1.DId, null, false, false, true);

            Assert.That(p2.Delivery.LsNr, Is.EqualTo("20221004X001"));

            var d = await GetDelivery("20221004X001");

            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                Assert.That(d.Parts, Has.Count.EqualTo(2));
                Assert.That(d.MgNr, Is.EqualTo(102));
            });
        }

        [Test]
        public async Task TestCreate_05_LNrChanged() {
            var vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);

            vm.Date = "29.09.2022";
            vm.Branch = vm.BranchSource.First();
            vm.LsNr = "20220929X001";  // "old" LNr, simulated change in DB
            vm.MgNr = 101;
            vm.Member = vm.MemberSource.First(m => m.MgNr == vm.MgNr);
            vm.SortId = "GV";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.GradationKmw = 15.0;
            vm.GradationOe = 73;
            vm.WineQualityLevel = vm.WineQualityLevelSource.First(l => l.QualId == "QUW");
            vm.Weight = 1234;
            vm.IsManualWeighing = true;
            vm.IsNetWeight = true;
            vm.WineOrigin = vm.WineOriginSource.First(o => o.HkId == "WLNO");
            var p1 = await vm.UpdateDeliveryPart(2022, null, null, false, false, true);

            Assert.That(p1.Delivery.LsNr, Is.EqualTo("20220929X002"));

            var d = GetDelivery("20220929X002");
            Assert.That(d, Is.Not.Null);
        }

        [Test]
        public async Task TestUpdate_01_Simple() {
            var vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);

            var d = await GetDelivery("20231010X001");
            Assert.That(d, Is.Not.Null);
            var p = d.Parts.First();
            Assert.That(p, Is.Not.Null);
            Assert.DoesNotThrow(() => {
                vm.FillInputs(d);
                vm.FillInputs(p);
            });

            vm.SortId = "WR";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.GradationKmw = 15.9;
            vm.GradationOe = 79;

            Assert.DoesNotThrowAsync(async () => await DeliveryService.UpdateDeliveryPart(vm, p.Year, p.DId, p.DPNr, false, false, true));

            d = await GetDelivery("20231010X001");
            Assert.That(d, Is.Not.Null);
            p = d.Parts.First();
            Assert.That(p, Is.Not.Null);

            vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);
            Assert.DoesNotThrow(() => {
                vm.FillInputs(d);
                vm.FillInputs(p);
            });
            Assert.Multiple(() => {
                Assert.That(vm.SortId, Is.EqualTo("WR"));
                Assert.That(vm.GradationKmw, Is.EqualTo(15.9));
            });
        }

        [Test]
        public async Task TestUpdate_02_Member() {
            var vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);

            var d = await GetDelivery("20231010X002");
            Assert.That(d, Is.Not.Null);
            var p = d.Parts.First();
            Assert.That(p, Is.Not.Null);
            Assert.DoesNotThrow(() => {
                vm.FillInputs(d);
                vm.FillInputs(p);
            });
            Assert.That(d.MgNr, Is.EqualTo(101));

            vm.SortId = "GV";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.MgNr = 102;
            vm.Member = vm.MemberSource.First(m => m.MgNr == vm.MgNr);

            Assert.DoesNotThrowAsync(async () => await DeliveryService.UpdateDeliveryPart(vm, p.Year, p.DId, p.DPNr, false, false, true));

            d = await GetDelivery("20231010X002");
            Assert.That(d, Is.Not.Null);
            Assert.That(d.MgNr, Is.EqualTo(102));
        }

        [Test]
        public async Task TestUpdate_03_DateAndTime() {
            var vm = new DeliveryAdminViewModel();
            await InitViewModel(vm);

            var d = await GetDelivery("20231015X001");
            Assert.That(d, Is.Null);
            d = await GetDelivery("20231010X003");
            Assert.That(d, Is.Not.Null);
            var p = d.Parts.First();
            Assert.That(p, Is.Not.Null);
            Assert.DoesNotThrow(() => {
                vm.FillInputs(d);
                vm.FillInputs(p);
            });
            Assert.That(d.MgNr, Is.EqualTo(101));

            vm.SortId = "GV";
            vm.WineVar = vm.WineVarSource.First(v => v.SortId == vm.SortId);
            vm.Date = "15.10.2023";
            vm.Time = "12:00";

            Assert.DoesNotThrowAsync(async () => await DeliveryService.UpdateDeliveryPart(vm, p.Year, p.DId, p.DPNr, true, true, false));

            d = await GetDelivery("20231010X003");
            Assert.That(d, Is.Null);
            d = await GetDelivery("20231015X001");
            Assert.That(d, Is.Not.Null);
        }

        [Test]
        public async Task TestSplit_01_Depreciate_One() {
            var d = await GetDelivery("20231001X001");
            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                var ps = GetParts(d);
                Assert.That(ps, Has.Length.EqualTo(3));
                Assert.That(ps[0].Weight, Is.EqualTo(1000));
                Assert.That(ps[0].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[1].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[2].QualId, Is.Not.EqualTo("WEI"));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.DepreciateDelivery(d.Year, d.DId, [1000, 0, 0]));
            d = await GetDelivery("20231001X001");
            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                var ps = GetParts(d);
                Assert.That(ps, Has.Length.EqualTo(3));
                Assert.That(ps[0].Weight, Is.EqualTo(1000));
                Assert.That(ps[0].QualId, Is.EqualTo("WEI"));
                Assert.That(ps[1].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[2].QualId, Is.Not.EqualTo("WEI"));
            });
        }

        [Test]
        public async Task TestSplit_02_Depreciate_Partial() {
            var d = await GetDelivery("20231001X002");
            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                var ps = GetParts(d);
                Assert.That(ps, Has.Length.EqualTo(3));
                Assert.That(ps[0].Weight, Is.EqualTo(1000));
                Assert.That(ps[0].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[1].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[2].QualId, Is.Not.EqualTo("WEI"));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.DepreciateDelivery(d.Year, d.DId, [600, 0, 0]));
            d = await GetDelivery("20231001X002");
            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                var ps = GetParts(d);
                Assert.That(ps, Has.Length.EqualTo(4));
                Assert.That(ps[0].Weight, Is.EqualTo(400));
                Assert.That(ps[0].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[1].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[2].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[3].Weight, Is.EqualTo(600));
                Assert.That(ps[3].QualId, Is.EqualTo("WEI"));
            });
        }

        [Test]
        public async Task TestSplit_03_Depreciate_Mixed() {
            var d = await GetDelivery("20231001X003");
            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                var ps = GetParts(d);
                Assert.That(ps, Has.Length.EqualTo(3));
                Assert.That(ps[0].Weight, Is.EqualTo(1000));
                Assert.That(ps[0].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[1].Weight, Is.EqualTo(1000));
                Assert.That(ps[1].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[2].QualId, Is.Not.EqualTo("WEI"));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.DepreciateDelivery(d.Year, d.DId, [1000, 700, -5]));
            d = await GetDelivery("20231001X003");
            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                var ps = GetParts(d);
                Assert.That(ps, Has.Length.EqualTo(4));
                Assert.That(ps[0].Weight, Is.EqualTo(1000));
                Assert.That(ps[0].QualId, Is.EqualTo("WEI"));
                Assert.That(ps[1].Weight, Is.EqualTo(300));
                Assert.That(ps[1].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[2].Weight, Is.EqualTo(1000));
                Assert.That(ps[2].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[3].Weight, Is.EqualTo(700));
                Assert.That(ps[3].QualId, Is.EqualTo("WEI"));
            });
        }

        [Test]
        public async Task TestSplit_04_Depreciate_Complete() {
            var d = await GetDelivery("20231001X004");
            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                var ps = GetParts(d);
                Assert.That(ps, Has.Length.EqualTo(3));
                Assert.That(ps[0].Weight, Is.EqualTo(1000));
                Assert.That(ps[0].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[1].Weight, Is.EqualTo(1000));
                Assert.That(ps[1].QualId, Is.Not.EqualTo("WEI"));
                Assert.That(ps[2].Weight, Is.EqualTo(1000));
                Assert.That(ps[2].QualId, Is.Not.EqualTo("WEI"));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.DepreciateDelivery(d.Year, d.DId, [1000, 1100, int.MaxValue]));
            d = await GetDelivery("20231001X004");
            Assert.That(d, Is.Not.Null);
            Assert.Multiple(() => {
                var ps = GetParts(d);
                Assert.That(ps, Has.Length.EqualTo(3));
                Assert.That(ps[0].Weight, Is.EqualTo(1000));
                Assert.That(ps[0].QualId, Is.EqualTo("WEI"));
                Assert.That(ps[1].Weight, Is.EqualTo(1000));
                Assert.That(ps[1].QualId, Is.EqualTo("WEI"));
                Assert.That(ps[2].Weight, Is.EqualTo(1000));
                Assert.That(ps[2].QualId, Is.EqualTo("WEI"));
            });
        }

        [Test]
        public async Task TestSplit_05_OtherMember_One() {
            var d1 = await GetDelivery("20231002X001");
            var d2 = await GetDelivery("20231002X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                Assert.That(d1.MgNr, Is.EqualTo(101));
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.SplitDeliveryToMember(d1.Year, d1.DId, [1000, 0, 0], 102));
            d1 = await GetDelivery("20231002X001");
            d2 = await GetDelivery("20231002X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(d1.MgNr, Is.EqualTo(101));
                Assert.That(d2.MgNr, Is.EqualTo(102));
                Assert.That(ps1, Has.Length.EqualTo(2));
                Assert.That(ps1[0].DPNr, Is.EqualTo(2));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].DPNr, Is.EqualTo(3));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(1));
                Assert.That(ps2[0].DPNr, Is.EqualTo(1));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
            });
        }

        [Test]
        public async Task TestSplit_06_OtherMember_Partial() {
            var d1 = await GetDelivery("20231003X001");
            var d2 = await GetDelivery("20231003X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                Assert.That(d1.MgNr, Is.EqualTo(101));
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.SplitDeliveryToMember(d1.Year, d1.DId, [400, -1, -2], 102));
            d1 = await GetDelivery("20231003X001");
            d2 = await GetDelivery("20231003X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(d1.MgNr, Is.EqualTo(101));
                Assert.That(d2.MgNr, Is.EqualTo(102));
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(600));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(1));
                Assert.That(ps2[0].Weight, Is.EqualTo(400));
            });
        }

        [Test]
        public async Task TestSplit_07_OtherMember_Mixed() {
            var d1 = await GetDelivery("20231004X001");
            var d2 = await GetDelivery("20231004X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                Assert.That(d1.MgNr, Is.EqualTo(101));
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.SplitDeliveryToMember(d1.Year, d1.DId, [200, 1000, int.MinValue], 102));
            d1 = await GetDelivery("20231004X001");
            d2 = await GetDelivery("20231004X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(d1.MgNr, Is.EqualTo(101));
                Assert.That(d2.MgNr, Is.EqualTo(102));
                Assert.That(ps1, Has.Length.EqualTo(2));
                Assert.That(ps1[0].DPNr, Is.EqualTo(1));
                Assert.That(ps1[0].Weight, Is.EqualTo(800));
                Assert.That(ps1[1].DPNr, Is.EqualTo(3));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(2));
                Assert.That(ps2[0].DPNr, Is.EqualTo(1));
                Assert.That(ps2[0].Weight, Is.EqualTo(200));
                Assert.That(ps2[1].DPNr, Is.EqualTo(2));
                Assert.That(ps2[1].Weight, Is.EqualTo(1000));
            });
        }

        [Test]
        public async Task TestSplit_08_OtherMember_Complete() {
            var d1 = await GetDelivery("20231005X001");
            var d2 = await GetDelivery("20231005X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                Assert.That(d1.MgNr, Is.EqualTo(101));
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.SplitDeliveryToMember(d1.Year, d1.DId, [1000, int.MaxValue, 1100], 102));
            d1 = await GetDelivery("20231005X001");
            d2 = await GetDelivery("20231005X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps2 = GetParts(d2);
                Assert.That(d2.MgNr, Is.EqualTo(102));
                Assert.That(ps2, Has.Length.EqualTo(3));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
                Assert.That(ps2[1].Weight, Is.EqualTo(1000));
                Assert.That(ps2[2].Weight, Is.EqualTo(1000));
            });
        }

        [Test]
        public async Task TestSplit_09_OtherDelivery_One() {
            var d1 = await GetDelivery("20231006X001");
            var d2 = await GetDelivery("20231006X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(1));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.SplitDeliveryToLsNr(d1.Year, d1.DId, [0, 1000, -4], d2.LsNr));
            d1 = await GetDelivery("20231006X001");
            d2 = await GetDelivery("20231006X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(ps1, Has.Length.EqualTo(2));
                Assert.That(ps1[0].DPNr, Is.EqualTo(1));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].DPNr, Is.EqualTo(3));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(2));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
                Assert.That(ps2[1].Weight, Is.EqualTo(1000));
            });
        }

        [Test]
        public async Task TestSplit_10_OtherDelivery_Partial() {
            var d1 = await GetDelivery("20231007X001");
            var d2 = await GetDelivery("20231007X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(1));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.SplitDeliveryToLsNr(d1.Year, d1.DId, [0, 300, int.MinValue], d2.LsNr));
            d1 = await GetDelivery("20231007X001");
            d2 = await GetDelivery("20231007X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].Weight, Is.EqualTo(700));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(2));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
                Assert.That(ps2[1].Weight, Is.EqualTo(300));
            });
        }

        [Test]
        public async Task TestSplit_11_OtherDelivery_Mixed() {
            var d1 = await GetDelivery("20231008X001");
            var d2 = await GetDelivery("20231008X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(1));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.SplitDeliveryToLsNr(d1.Year, d1.DId, [850, 1000, -4], d2.LsNr));
            d1 = await GetDelivery("20231008X001");
            d2 = await GetDelivery("20231008X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(ps1, Has.Length.EqualTo(2));
                Assert.That(ps1[0].DPNr, Is.EqualTo(1));
                Assert.That(ps1[0].Weight, Is.EqualTo(150));
                Assert.That(ps1[1].DPNr, Is.EqualTo(3));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(3));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
                Assert.That(ps2[1].Weight, Is.EqualTo(850));
                Assert.That(ps2[2].Weight, Is.EqualTo(1000));
            });
        }

        [Test]
        public async Task TestSplit_12_OtherDelivery_Complete() {
            var d1 = await GetDelivery("20231009X001");
            var d2 = await GetDelivery("20231009X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Not.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps1 = GetParts(d1);
                var ps2 = GetParts(d2);
                Assert.That(ps1, Has.Length.EqualTo(3));
                Assert.That(ps1[0].Weight, Is.EqualTo(1000));
                Assert.That(ps1[1].Weight, Is.EqualTo(1000));
                Assert.That(ps1[2].Weight, Is.EqualTo(1000));
                Assert.That(ps2, Has.Length.EqualTo(1));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
            });

            Assert.DoesNotThrowAsync(async () => await DeliveryService.SplitDeliveryToLsNr(d1.Year, d1.DId, [1200, int.MaxValue, 1000], d2.LsNr));
            d1 = await GetDelivery("20231009X001");
            d2 = await GetDelivery("20231009X002");
            Assert.Multiple(() => {
                Assert.That(d1, Is.Null);
                Assert.That(d2, Is.Not.Null);
            });
            Assert.Multiple(() => {
                var ps2 = GetParts(d2);
                Assert.That(ps2, Has.Length.EqualTo(4));
                Assert.That(ps2[0].Weight, Is.EqualTo(1000));
                Assert.That(ps2[1].Weight, Is.EqualTo(1000));
                Assert.That(ps2[2].Weight, Is.EqualTo(1000));
                Assert.That(ps2[3].Weight, Is.EqualTo(1000));
            });
        }

        [Test]
        public async Task TestDelete_01_Normal() {
            using (var ctx = new AppDbContext()) {
                Assert.That(await ctx.Deliveries.FindAsync(2022, 2), Is.Not.Null);
            }
            Assert.DoesNotThrowAsync(async () => await DeliveryService.DeleteDelivery("20220930X001"));
            using (var ctx = new AppDbContext()) {
                Assert.That(await ctx.Deliveries.FindAsync(2022, 2), Is.Null);
            }
        }
    }
}
