using Elwig.Documents;
using Elwig.Helpers;
using Elwig.Models.Dtos;
using Microsoft.EntityFrameworkCore;

namespace Tests.UnitTests.DocumentTests {
    [TestFixture]
    public class CreditNoteTest {

        [Test]
        public async Task Test_01_VirtualCreditNote() {
            using var ctx = new AppDbContext();
            var m = await ctx.Members.FindAsync(101);
            var p = await ctx.MemberPayments.Where(p => p.Year == 2020 && p.AvNr == 1).SingleAsync();
            var data = await CreditNoteDeliveryData.ForPaymentVariant(ctx.CreditNoteDeliveryRows, ctx.PaymentVariants, 2020, 1);
            using var doc = new CreditNote(ctx, p, data[m!.MgNr], false, false, false, false,
                ctx.GetMemberUnderDelivery(2020, m!.MgNr).GetAwaiter().GetResult());
            var text = await Utils.GeneratePdfText(doc);
            Assert.Multiple(() => {
                Assert.That(text, Contains.Substring("""
                    MUSTERMANN Max
                    Winzerstraße 1
                    2223 Hohenruppersdorf
                    """));
                Assert.That(text, Contains.Substring("0123463"));  // Betriebsnummer
                Assert.That(text, Contains.Substring("pauschaliert"));
                Assert.That(text, Contains.Substring($"Wolkersdorf, am {Elwig.Helpers.Utils.Today:dd.MM.yyyy}"));
                Assert.That(text, Contains.Substring("Traubengutschrift Max Mustermann – Probevariante"));
                Assert.That(text, Contains.Substring("AT81 1234 5678 9012 3457"));
                Assert.That(text, Contains.Substring("""
                    20201001X001 1 Grüner Veltliner 73 15,0 ungeb.: 3 219 0,5000 - - 1 609,50
                    20201001X003 1 Grüner Veltliner abgew. 75 15,4 ungeb.: 2 561 - -
                    20201001X003 2 Grüner Veltliner Kabinett / abgew.
                    87 17,6 ungeb.: 3 129 - -
                    20201001X003 3 Grüner Veltliner abgew. 79 16,1 ungeb.: 1 280 - -
                    20201001X005 1 Welschriesling 84 17,0 ungeb.: 3 192 - -
                    20201001X005 2 Welschriesling 84 17,1 ungeb.: 2 190 - -
                    """));
                Assert.That(text, Contains.Substring("Gesamtbetrag: € 1 000,00"));
                Assert.That(text, Contains.Substring("Auszahlungsbetrag: € 1 000,00"));
            });
        }
    }
}
