#nullable enable
using Baya.Domain.Common;

namespace Baya.Domain.Entities.Invoices;

/// <summary>
/// The minimal official receipt per booking. <b>VAT applies to the platform commission line only</b>
/// (<see cref="PlatformCommissionIrr"/>) — never the nurse's earnings — following the Snapp/Tapsi precedent
/// that the nurse is the taxable seller of the care service and the platform's commission is its own taxable
/// revenue. <see cref="VatIrr"/> is computed integer-only from a <b>config-driven</b> <see cref="VatRate"/>
/// (default 0.10); a <c>vat_rate = 0</c> exemption yields <see cref="VatIrr"/> = 0.
/// <para>
/// <see cref="InvoiceNumber"/> is UNIQUE and <b>sequential</b>, drawn from a concurrency-safe counter row
/// (never random/timestamp-derived). One issued invoice per booking (idempotent).
/// </para>
/// </summary>
public class Invoice : BaseEntity<long>
{
    public long BookingId { get; set; }

    /// <summary>Official sequential number (UNIQUE) — drawn from <see cref="InvoiceNumberSequence"/>.</summary>
    public string InvoiceNumber { get; set; } = null!;

    /// <summary>An <see cref="InvoiceIssuingEntityType"/> code.</summary>
    public string IssuingEntityType { get; set; } = InvoiceIssuingEntityType.Platform;

    /// <summary>The licensed center that is merchant-of-record, when the issuer is a partner center (b15).</summary>
    public long? PartnerCenterId { get; set; }

    public long GrossIrr { get; set; }

    /// <summary>The VAT-relevant line — VAT is computed on this, never on the nurse payout.</summary>
    public long PlatformCommissionIrr { get; set; }

    public long? BnplCommissionIrr { get; set; }

    /// <summary>Config-driven VAT rate snapshot (default 0.10) frozen at issue time.</summary>
    public decimal VatRate { get; set; }

    /// <summary>round(<see cref="PlatformCommissionIrr"/> × <see cref="VatRate"/>) — integer-only, no float path.</summary>
    public long VatIrr { get; set; }

    /// <summary>The سامانه مودیان 22-digit reference when registered; null until then.</summary>
    public string? MoadianReferenceNumber { get; set; }

    /// <summary>A <see cref="MoadianStatus"/> code.</summary>
    public string? MoadianStatus { get; set; }

    /// <summary>An <c>IObjectStorage</c> key for the optional invoice PDF.</summary>
    public string? PdfStorageKey { get; set; }

    public DateTime IssuedAt { get; set; }

    public DateTimeOffset? DeletedAt { get; set; }

    /// <summary>Records the outcome of a مودیان submission (mock: pending/no-ref; forced: registered/ref).</summary>
    public void ApplyMoadianResult(string status, string? referenceNumber)
    {
        MoadianStatus = status;
        MoadianReferenceNumber = referenceNumber;
    }
}
