namespace CashLineERP.Application.Reporting;

public static class FinancialStatementCatalog
{
    public const string BalanceSheet = "BALANCE_SHEET";
    public const string IncomeStatement = "INCOME_STATEMENT";
    public const string CashFlow = "CASH_FLOW";
    public const string TrialBalance = "TRIAL_BALANCE";
    public const string AccountingEntries = "ACCOUNTING_ENTRIES";

    public static string NormalizeStatementCode(string statementCode) =>
        statementCode?.Trim().ToUpperInvariant() switch
        {
            BalanceSheet => BalanceSheet,
            IncomeStatement => IncomeStatement,
            CashFlow => CashFlow,
            TrialBalance => TrialBalance,
            AccountingEntries => AccountingEntries,
            _ => throw new InvalidOperationException($"Unsupported financial statement code '{statementCode}'.")
        };

    public static string NormalizeFormatCode(string formatCode) =>
        formatCode?.Trim().ToUpperInvariant() switch
        {
            "CSV" => "CSV",
            "EXCEL" => "EXCEL",
            "PDF" => "PDF",
            _ => throw new InvalidOperationException($"Unsupported financial statement export format '{formatCode}'.")
        };

    public static string GetTitle(string statementCode) =>
        NormalizeStatementCode(statementCode) switch
        {
            BalanceSheet => "Balance Sheet",
            IncomeStatement => "Income Statement",
            CashFlow => "Cash Flow Statement",
            TrialBalance => "Trial Balance",
            AccountingEntries => "Accounting Entries",
            _ => "Financial Statement"
        };

    public static IReadOnlyCollection<string> GetColumns(string statementCode) =>
        NormalizeStatementCode(statementCode) switch
        {
            BalanceSheet => ["LineLabel", "ClosingAmount"],
            IncomeStatement => ["LineLabel", "NetAmount"],
            CashFlow => ["LineLabel", "NetAmount"],
            TrialBalance => ["LineLabel", "OpeningAmount", "DebitAmount", "CreditAmount", "ClosingAmount"],
            AccountingEntries => ["ActivityDate", "BatchNumber", "JournalNumber", "DocumentType", "ReferenceNumber", "GeneralLedgerAccountCode", "LineLabel", "DebitAmount", "CreditAmount", "DescriptionText"],
            _ => ["LineLabel", "ClosingAmount"]
        };

    public static string BuildFileName(string statementCode, string formatCode, DateOnly asOfDate)
    {
        var normalizedStatementCode = NormalizeStatementCode(statementCode).ToLowerInvariant();
        var extension = NormalizeFormatCode(formatCode) switch
        {
            "CSV" => "csv",
            "EXCEL" => "xls",
            "PDF" => "pdf",
            _ => "dat"
        };

        return $"{normalizedStatementCode}-{asOfDate:yyyyMMdd}.{extension}";
    }
}
