using Books.Api.Domain.JournalEntryDrafts;

namespace Books.Api.EventFlow.ReadModels;

/// <summary>
/// DTO for reading journal entry draft data from the database.
/// </summary>
public class JournalEntryDraftReadModelDto
{
    public string Id { get; set; } = string.Empty;
    public string CompanyId { get; set; } = string.Empty;
    public string Name { get; set; } = string.Empty;
    /// <summary>
    /// Bilagsnummer - unique document number required by Bogføringsloven § 7, Stk. 4
    /// </summary>
    public string VoucherNumber { get; set; } = string.Empty;
    /// <summary>
    /// Bilagsdato - the date of the transaction/document (e.g., invoice date)
    /// </summary>
    public DateTime? DocumentDate { get; set; }
    public string? Description { get; set; }
    public string? FiscalYearId { get; set; }
    public string Lines { get; set; } = "[]";
    /// <summary>
    /// JSON array of attachment IDs (bilag references)
    /// </summary>
    public string AttachmentIds { get; set; } = "[]";
    public string Status { get; set; } = "active";
    public string? TransactionId { get; set; }
    /// <summary>
    /// The exact timestamp when the draft was posted to the ledger.
    /// </summary>
    public DateTimeOffset? PostedAt { get; set; }
    public string CreatedBy { get; set; } = string.Empty;
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
    /// <summary>
    /// Full AI extraction data stored as JSON string.
    /// Contains vendor CVR, amounts, VAT, due date, payment reference, line items, etc.
    /// </summary>
    public string? ExtractionData { get; set; }

    /// <summary>
    /// Deserialize lines from JSON to list of DraftLine objects.
    /// </summary>
    public List<DraftLine> GetLines()
    {
        if (string.IsNullOrEmpty(Lines))
            return [];

        return System.Text.Json.JsonSerializer.Deserialize<List<DraftLine>>(Lines) ?? [];
    }

    /// <summary>
    /// Deserialize attachment IDs from JSON.
    /// </summary>
    public List<string> GetAttachmentIds()
    {
        if (string.IsNullOrEmpty(AttachmentIds))
            return [];

        return System.Text.Json.JsonSerializer.Deserialize<List<string>>(AttachmentIds) ?? [];
    }
}
