namespace FKGees.DocsDefinitions.Decrets;

public class Pz : IDecretsDefinition
{
    public int DocType => 2;
    public string Type => "Pz";
    public List<Definition> Definitions { get; } =
    [
        new Definition("305-60-24", "MA", "[znr]"), 
        new Definition("342-0-24", "MA", "[snr]-[znr]"),
        new Definition("343-0-24", "MA", "[sbr]-[snr]"), 
        new Definition("332-0-24", "WN", "[sbr]")
    ];

    public Task<List<DecretsResult>> Process(IReadOnlyList<Dok> documents)
    {
        var result = new List<DecretsResult>();

        var toProcess = documents
            .Where(x => x.TypDok == DocType && x.RozliczaRozliczanyDok.Count == 0);

        foreach (var dok in toProcess)
        {
            Definitions.ForEach(d =>
            {
                var item = dok.DecretKontr();
                item.Data = dok.DataPom;
                item.DataWplywu = dok.Data;
                item.Nr = dok.NrDok;
                item.Opis = d.Account == "305-60-24" ? item.k_NIP : dok.NrDok;
                item.StronaKonta = d.AccountSide;
                item.KontoFk = d.Account;
                item.Kwota = ProcessValue(dok, d.Expression).AsString();

                result.Add(item);
            });
        }

        return Task.FromResult(result);
    }

    private static decimal ProcessValue(Dok doc, string expression)
    {
        return expression switch
        {
            "[znr]" => doc.Netto,
            "[snr]-[znr]" => doc.NettoDet - doc.Netto,
            "[sbr]-[snr]" => doc.Podatek,
            "[sbr]" => doc.NettoDet + doc.PodatekDet,
            _ => 0
        };
    }
}