namespace CashLineERP.Application.Reporting;

internal static class ReportingSnapshotCatalog
{
    public static (string ReportCode, string ReportTitle) Normalize(string reportCode)
    {
        var normalizedCode = reportCode?.Trim().ToUpperInvariant()
            ?? throw new InvalidOperationException("Reporting snapshot report code is required.");

        return normalizedCode switch
        {
            "CORE_FINANCE" => ("CORE_FINANCE", "Core Finance Report"),
            "CORE_OPERATIONS" => ("CORE_OPERATIONS", "Core Operations Report"),
            "AP_AGING" => ("AP_AGING", "Accounts Payable Aging Report"),
            "AR_AGING" => ("AR_AGING", "Accounts Receivable Aging Report"),
            _ => throw new InvalidOperationException($"Unsupported reporting snapshot report code '{reportCode}'.")
        };
    }

    public static void ValidateWindow(string normalizedReportCode, DateOnly? dateFrom, DateOnly? dateTo, DateOnly? asOfDate)
    {
        if (dateFrom.HasValue && dateTo.HasValue && dateFrom.Value > dateTo.Value)
        {
            throw new InvalidOperationException("Reporting snapshot date range is invalid because dateFrom is after dateTo.");
        }

        switch (normalizedReportCode)
        {
            case "CORE_FINANCE":
            case "CORE_OPERATIONS":
                return;
            case "AP_AGING":
            case "AR_AGING":
                if (dateFrom.HasValue || dateTo.HasValue)
                {
                    throw new InvalidOperationException("Aging report snapshots use asOfDate and do not accept dateFrom/dateTo filters.");
                }

                return;
            default:
                throw new InvalidOperationException($"Unsupported reporting snapshot report code '{normalizedReportCode}'.");
        }
    }
}
