using CashLineERP.Contracts.Finance;

namespace CashLineERP.Application.Finance;

public sealed class CashAccountService : ICashAccountService
{
    private readonly ICashAccountRepository _repository;

    public CashAccountService(ICashAccountRepository repository)
    {
        _repository = repository;
    }

    public IReadOnlyCollection<CashAccountResponse> GetCashAccounts() =>
        _repository.GetCashAccounts().Select(Map).ToArray();

    public CashAccountResponse? GetCashAccount(long cashAccountId)
    {
        var account = _repository.GetCashAccount(cashAccountId);
        return account is null ? null : Map(account);
    }

    public CashAccountResponse CreateCashAccount(CreateCashAccountRequest request) =>
        Map(_repository.Create(new CreateCashAccountCommand(
            request.AccountCode,
            request.AccountName,
            request.AccountType,
            request.CurrencyCode,
            request.BranchId,
            request.OpeningBalanceAmount)));

    private static CashAccountResponse Map(CashAccountView account) =>
        new(
            account.CashAccountId,
            account.AccountCode,
            account.AccountName,
            account.AccountType,
            account.CurrencyCode,
            account.BranchId,
            account.IsActive,
            account.OpeningBalanceAmount,
            account.CurrentBalanceAmount);
}
