using CashLineERP.Domain.Common;
using CashLineERP.SharedKernel.Workflow;

namespace CashLineERP.Domain.Finance;

public sealed class GeneralLedgerBatch : Entity
{
    public long Id { get; private set; }

    public string BatchNumber { get; private set; } = string.Empty;

    public string BatchType { get; private set; } = string.Empty;

    public long? BranchId { get; private set; }

    public long PeriodId { get; private set; }

    public long StatusId { get; private set; } = WorkflowStatusSeedIds.Posted;

    public string? DescriptionText { get; private set; }

    public string? SourceModule { get; private set; }

    public long? SourceDocumentId { get; private set; }

    public decimal BatchTotalAmount { get; private set; }

    public List<GeneralLedgerJournal> Journals { get; private set; } = [];

    public GeneralLedgerBatch(
        string batchNumber,
        string batchType,
        long? branchId,
        long periodId,
        string? descriptionText,
        string? sourceModule,
        long? sourceDocumentId)
    {
        BatchNumber = batchNumber;
        BatchType = batchType;
        BranchId = branchId;
        PeriodId = periodId;
        DescriptionText = descriptionText;
        SourceModule = sourceModule;
        SourceDocumentId = sourceDocumentId;
    }

    private GeneralLedgerBatch()
    {
    }

    public void AddJournal(GeneralLedgerJournal journal)
    {
        Journals.Add(journal);
        BatchTotalAmount = Journals.Sum(entry => entry.TotalAmount);
    }
}
