using FKGees.Services;

namespace FKGees.DocsDefinitions.Decrets;

public class FaktSprz : IDecretsDefinition
{
    public int DocType => 33;
    public string Type => "FaktSprz";
    public List<Definition> Definitions { get; } =
    [
        new Definition("200-01", "WN", "[sbr]"), 
        new Definition("732-0-24", "MA", "[sbr]"),
        new Definition("732-0-24", "WN", "[svr]"), 
        new Definition("222-0-24", "MA", "[svr]"),
        new Definition("343-0-24", "WN", "[svr]"),
        new Definition("737-0-24", "MA", "[svr]"),
        new Definition("737-0-24", "WN", "[sbr]"),
        new Definition("332-0-24", "MA", "[sbr]"),
    ];

    private readonly PcmService _pcmService;

    public FaktSprz(PcmService pcmService)
    {
        _pcmService = pcmService;
    }

    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)
        {
            var fakt = _pcmService.WzNrFaktury(dok.DokId);
            Definitions.ForEach(d =>
            {
                var item = dok.DecretKontr();
                item.Data = dok.DataPom;
                item.DataWplywu = dok.Data;
                item.Nr = dok.NrDok;
                item.Opis = fakt;
                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
        {
            "[sbr]" => doc.Netto + doc.Podatek,
            "[svr]" => doc.Podatek,
            _ => 0
        };
    }
}