namespace FKGees.DocsDefinitions.Decrets;

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

    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 = new DecretsResult
                {
                    k_Skrot = "Detal",
                    k_Nazwa = "Odbiorca detaliczny",
                    Data = dok.DataPom,
                    DataWplywu = dok.Data,
                    Nr = dok.NrDok,
                    Opis = dok.NrDok,
                    StronaKonta = d.AccountSide,
                    KontoFk = d.Account,
                    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.Razem - doc.Kwota1 + doc.Kwota2,
            "[svr]" => doc.Podatek,
            _ => 0
        };
    }
}