namespace Books.Api.Saft.Models;

/// <summary>
/// Complete SAF-T DK audit file document
/// </summary>
public record SaftDocument(
    SaftHeader Header,
    SaftMasterFiles MasterFiles,
    SaftGeneralLedgerEntries Entries);

// =============================================================================
// HEADER SECTION
// =============================================================================

/// <summary>
/// SAF-T Header section - metadata about the audit file
/// </summary>
public record SaftHeader(
    string AuditFileVersion,
    string AuditFileDateCreated,
    string SoftwareCompanyName,
    string SoftwareID,
    string SoftwareVersion,
    SaftCompany Company,
    SaftSelectionCriteria SelectionCriteria);

/// <summary>
/// Company information for SAF-T Header
/// </summary>
public record SaftCompany(
    string RegistrationNumber,  // CVR number
    string Name,
    SaftAddress Address,
    SaftContact? Contact);

/// <summary>
/// Address structure used throughout SAF-T
/// </summary>
public record SaftAddress(
    string? StreetName,
    string? City,
    string? PostalCode,
    string Country);

/// <summary>
/// Contact information
/// </summary>
public record SaftContact(
    string? Telephone,
    string? Email,
    string? Website);

/// <summary>
/// Selection criteria defining the export period
/// </summary>
public record SaftSelectionCriteria(
    string PeriodStart,
    string PeriodEnd);

// =============================================================================
// MASTER FILES SECTION
// =============================================================================

/// <summary>
/// Master files containing reference data
/// </summary>
public record SaftMasterFiles(
    List<SaftAccount> GeneralLedgerAccounts,
    List<SaftCustomer> Customers,
    List<SaftSupplier> Suppliers);

/// <summary>
/// General ledger account for MasterFiles
/// </summary>
public record SaftAccount(
    string AccountID,
    string AccountDescription,
    string? StandardAccountID,   // Erhvervsstyrelsen mapping
    string AccountType,          // Asset, Liability, Equity, Income, Expense
    decimal OpeningDebitBalance,
    decimal OpeningCreditBalance,
    decimal ClosingDebitBalance,
    decimal ClosingCreditBalance);

/// <summary>
/// Customer master data
/// </summary>
public record SaftCustomer(
    string CustomerID,
    string? AccountID,           // Associated GL account
    string Name,
    string? RegistrationNumber,  // CVR for B2B customers
    SaftAddress? Address,
    SaftContact? Contact);

/// <summary>
/// Supplier master data
/// </summary>
public record SaftSupplier(
    string SupplierID,
    string? AccountID,           // Associated GL account
    string Name,
    string? RegistrationNumber,  // CVR for B2B suppliers
    SaftAddress? Address,
    SaftContact? Contact);

// =============================================================================
// GENERAL LEDGER ENTRIES SECTION
// =============================================================================

/// <summary>
/// Container for all general ledger entries
/// </summary>
public record SaftGeneralLedgerEntries(
    int NumberOfEntries,
    decimal TotalDebit,
    decimal TotalCredit,
    List<SaftJournal> Journals);

/// <summary>
/// Journal grouping transactions
/// </summary>
public record SaftJournal(
    string JournalID,
    string Description,
    string Type,
    List<SaftTransaction> Transactions);

/// <summary>
/// Individual transaction/posting
/// </summary>
public record SaftTransaction(
    string TransactionID,
    string Period,              // MM format (01-12)
    string TransactionDate,     // YYYY-MM-DD
    string Description,
    string SystemEntryDate,     // When entered in system
    string GLPostingDate,       // When posted to GL
    List<SaftTransactionLine> Lines);

/// <summary>
/// Individual line within a transaction
/// </summary>
public record SaftTransactionLine(
    string RecordID,
    string AccountID,
    string? Description,
    decimal? DebitAmount,
    decimal? CreditAmount,
    string? CustomerID,
    string? SupplierID,
    SaftTaxInformation? TaxInfo);

/// <summary>
/// Tax/VAT information for a transaction line
/// </summary>
public record SaftTaxInformation(
    string TaxCode,
    decimal? TaxPercentage,
    decimal? TaxBase,
    decimal TaxAmount);

// =============================================================================
// EXPORT RESULT
// =============================================================================

/// <summary>
/// Result of SAF-T export operation
/// </summary>
public record SaftExportResult(
    bool Success,
    string? XmlContent,
    string? FileName,
    string? ErrorCode,
    string? ErrorMessage)
{
    public static SaftExportResult Ok(string xmlContent, string fileName) =>
        new(true, xmlContent, fileName, null, null);

    public static SaftExportResult Error(string errorCode, string errorMessage) =>
        new(false, null, null, errorCode, errorMessage);
}
