#nullable enable
using Baya.Domain.Common;

namespace Baya.Domain.Entities.PartnerCenters;

/// <summary>
/// A licensed home-nursing center (مرکز مشاوره و ارائه مراقبت‌های پرستاری در منزل) that <b>sponsors</b> nurses
/// and, at launch, is plausibly the <b>merchant-of-record</b> (the Asanism go-to-market model). When
/// <see cref="IsMerchantOfRecord"/> is set it — <b>not</b> Balinyaar and <b>not</b> the nurse — is the legal
/// invoice issuer and the IPG settlement target: invoices and settlement follow the center, resolved once via
/// <c>GetCenterForBooking</c>. Distinct from the future (DEFERRED) <c>organizations</c> employer model — a
/// center sponsors *for legality*, it does not *employ*. Marked <see cref="IAuditable"/> so every admin state
/// change appends an immutable <c>audit_logs</c> row (the encrypted <see cref="SettlementIban"/> is redacted in
/// the diff).
/// </summary>
public class PartnerCenter : BaseEntity<long>, IAuditable
{
    public string Name { get; set; } = string.Empty;

    public string? LegalEntityType { get; set; }

    /// <summary>پروانه تأسیس — the MoH establishment permit number that makes the operation legal.</summary>
    public string MohEstablishmentPermitNo { get; set; } = string.Empty;

    /// <summary>مسئول فنی — the technical director nurse's <c>users.id</c>. Nullable.</summary>
    public int? TechnicalDirectorNurseUserId { get; set; }

    public string? TechnicalDirectorLicenseNo { get; set; }

    /// <summary>نماد اعتماد الکترونیکی — the eNamad trust seal code (clears the BNPL onboarding gate).</summary>
    public string? EnamadCode { get; set; }

    /// <summary>The center's settlement IBAN — <b>only</b> when merchant-of-record. Encrypted at rest via
    /// <c>IFieldEncryptor</c> (converter wired in <c>ApplicationDbContext</c>); never stored, logged, or
    /// projected in plaintext; masked (last 4) in any read. Redacted from the audit diff.</summary>
    [AuditRedacted]
    public string? SettlementIban { get; set; }

    public bool IsMerchantOfRecord { get; set; }

    /// <summary>The center's own cut (fraction, 0–1), separate from <c>platform_fee_rate</c>. Nullable.</summary>
    public decimal? CommissionRate { get; set; }

    /// <summary>The <c>users.id</c> that owns the center's dashboard account.</summary>
    public int AdminUserId { get; set; }

    /// <summary>Guarded — flipped only through <see cref="Verify"/>/<see cref="SetActive"/>.</summary>
    public bool IsActive { get; private set; }

    public DateTimeOffset? VerifiedAt { get; private set; }

    public DateTimeOffset? DeletedAt { get; set; }

    /// <summary>Records the (manual, at MVP) licensing approval and activates the center.</summary>
    public void Verify(DateTimeOffset now)
    {
        VerifiedAt = now;
        IsActive = true;
    }

    public void SetActive(bool active) => IsActive = active;
}
