using Baya.Domain.Common;

namespace Baya.Domain.Entities.Identity;

/// <summary>
/// A nurse's payout destination (IBAN/Sheba) — the single place real money will one day leave the
/// platform. Hardened: the IBAN is encrypted, a deterministic <see cref="IbanHash"/> carries a UNIQUE
/// constraint so one IBAN can't silently serve two nurses, and an automated استعلام شبا ownership
/// inquiry records whether the IBAN owner matches the nurse's national id
/// (<see cref="MatchedNationalId"/>) — the first-payout gate (b13), not an admin's eyeballs.
/// </summary>
public class NurseBankAccount : BaseEntity<long>
{
    public long NurseId { get; set; }
    public NurseProfile Nurse { get; set; }

    public string BankName { get; set; }

    /// <summary>Encrypted at rest.</summary>
    public string AccountHolderName { get; set; }

    /// <summary>Encrypted at rest. Non-deterministic ciphertext — never equality-queried; lookups go
    /// through <see cref="IbanHash"/>.</summary>
    public string Iban { get; set; }

    /// <summary>Deterministic keyed hash of the normalized IBAN (via <c>IFieldEncryptor.Hash</c>) —
    /// carries the UNIQUE index, since the ciphertext itself can't be uniquely indexed.</summary>
    public string IbanHash { get; set; }

    public bool IsPrimary { get; set; }

    /// <summary>Result of the استعلام شبا IBAN-owner ↔ national-id inquiry. NULL until the inquiry runs;
    /// the first payout (b13) is gated on <c>true</c>.</summary>
    public bool? MatchedNationalId { get; set; }

    /// <summary>The account-holder name the bank returned by the ownership inquiry — a snapshot.</summary>
    public string AccountHolderFromBank { get; set; }

    /// <summary>The ownership-inquiry vendor transaction id, kept for audit.</summary>
    public string OwnershipVendorRef { get; set; }

    public bool IsVerified { get; set; }
    public int? VerifiedByAdminId { get; set; }
    public DateTimeOffset? VerifiedAt { get; set; }

    /// <summary>Records the outcome of an استعلام شبا ownership inquiry against this account.</summary>
    public void ApplyOwnershipInquiry(bool matchedNationalId, string accountHolderFromBank, string vendorRef)
    {
        MatchedNationalId = matchedNationalId;
        AccountHolderFromBank = accountHolderFromBank;
        OwnershipVendorRef = vendorRef;
    }
}
