using CashLineERP.Contracts.Finance;

namespace CashLineERP.Application.Finance;

public sealed class GeneralLedgerService : IGeneralLedgerService
{
    private readonly IGeneralLedgerReadRepository _repository;

    public GeneralLedgerService(IGeneralLedgerReadRepository repository)
    {
        _repository = repository;
    }

    public IReadOnlyCollection<GeneralLedgerBatchResponse> GetGeneralLedgerBatches() =>
        _repository.GetGeneralLedgerBatches()
            .Select(batch => new GeneralLedgerBatchResponse(
                batch.GeneralLedgerBatchId,
                batch.BatchNumber,
                batch.BatchType,
                batch.BranchId,
                batch.PeriodId,
                batch.StatusCode,
                batch.DescriptionText,
                batch.SourceModule,
                batch.SourceDocumentId,
                batch.BatchTotalAmount,
                batch.Journals.Select(journal => new GeneralLedgerJournalResponse(
                    journal.GeneralLedgerJournalId,
                    journal.JournalNumber,
                    journal.JournalType,
                    journal.PostingDate,
                    journal.StatusCode,
                    journal.DescriptionText,
                    journal.TotalAmount,
                    journal.Lines.Select(line => new GeneralLedgerJournalLineResponse(
                        line.GeneralLedgerJournalLineId,
                        line.LineNumber,
                        line.GeneralLedgerAccountId,
                        line.BranchId,
                        line.PartyId,
                        line.ItemId,
                        line.DebitAmount,
                        line.CreditAmount,
                        line.CurrencyCode,
                        line.FxRate,
                        line.LineDescription)).ToArray())).ToArray()))
            .ToArray();
}
