namespace FKGees.DocsDefinitions.Decrets;

public class MmMinus : IDecretsDefinition
{
    public int DocType => 6;
    public string Type => "Mm-";
    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);

        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 = dok.TekstZaCo();
                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.NettoMag),
            "[snr]-[znr]" => -(doc.NettoDet - doc.Netto),
            "[sbr]-[snr]" => -(doc.Podatek),
            "[sbr]" => -(doc.NettoDet + doc.PodatekDet),
            _ => 0
        };
    }
}