using Books.Api.EventFlow.Repositories;

namespace Books.Api.AiBookkeeper;

/// <summary>
/// Provides chart of accounts data for AI Bookkeeper processing.
/// Fetches accounts from repository and filters to expense-type accounts.
/// </summary>
public class ChartOfAccountsProvider(IAccountRepository accountRepository) : IChartOfAccountsProvider
{
    public async Task<ChartOfAccountsDto> GetChartOfAccountsAsync(
        string companyId, CancellationToken cancellationToken = default)
    {
        var accounts = await accountRepository.GetActiveByCompanyIdAsync(companyId, cancellationToken);

        // Filter to expense-type accounts that AI can suggest
        var expenseAccounts = accounts
            .Where(a => a.AccountType is "expense" or "cogs" or "personnel" or "financial")
            .OrderBy(a => a.AccountNumber)
            .Select(a => new AiAccountDto
            {
                AccountNumber = a.AccountNumber,
                Name = a.Name,
                AccountType = a.AccountType,
                VatCodeId = a.VatCodeId,
                StandardAccountNumber = a.StandardAccountNumber
            })
            .ToList();

        return new ChartOfAccountsDto
        {
            CompanyId = companyId,
            Accounts = expenseAccounts
        };
    }
}
