namespace Books.Api.EventFlow.ReadModels;

/// <summary>
/// DTO for bank transactions synced from Enable Banking.
/// Used for display in Hurtig Bogføring and tracking booking status.
/// </summary>
public class BankTransactionDto
{
    public string Id { get; set; } = string.Empty;
    public string CompanyId { get; set; } = string.Empty;
    public string BankConnectionId { get; set; } = string.Empty;
    public string BankAccountId { get; set; } = string.Empty;
    public string ExternalId { get; set; } = string.Empty;

    public decimal Amount { get; set; }
    public string Currency { get; set; } = "DKK";

    public DateTime TransactionDate { get; set; }
    public DateTime? BookingDate { get; set; }
    public DateTime? ValueDate { get; set; }

    public string? Description { get; set; }
    public string? CounterpartyName { get; set; }
    public string? CounterpartyAccount { get; set; }
    public string? Reference { get; set; }
    public string? CreditorName { get; set; }
    public string? DebtorName { get; set; }

    /// <summary>
    /// Status: pending | booked | ignored
    /// </summary>
    public string Status { get; set; } = "pending";

    /// <summary>
    /// Reference to journal_entry_draft when booked.
    /// </summary>
    public string? JournalEntryDraftId { get; set; }

    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }

    /// <summary>
    /// Helper to get display name for counterparty.
    /// </summary>
    public string DisplayCounterparty =>
        CounterpartyName ?? CreditorName ?? DebtorName ?? "Ukendt";

    /// <summary>
    /// Helper to determine if this is an income (positive) or expense (negative).
    /// </summary>
    public bool IsIncome => Amount > 0;
}

/// <summary>
/// Result of a bank transaction sync operation.
/// </summary>
public class BankTransactionSyncResult
{
    public int TotalConnections { get; set; }
    public int TotalAccounts { get; set; }
    public int NewTransactions { get; set; }
    public int SkippedDuplicates { get; set; }
    public int Errors { get; set; }
    public List<string> ErrorMessages { get; set; } = [];
}
