using PowderCoating.Core.Enums;

namespace PowderCoating.Core.Entities;

public class GiftCertificate : BaseEntity
{
    /// <summary>Certificate code shown on the physical/emailed certificate. Format: GC-YYMM-####</summary>
    public string CertificateCode { get; set; } = string.Empty;

    public decimal OriginalAmount { get; set; }
    public decimal RedeemedAmount { get; set; }
    public decimal RemainingBalance => OriginalAmount - RedeemedAmount;

    // Who it's for (optional — may be given to an unknown recipient)
    public int? RecipientCustomerId { get; set; }
    public string? RecipientName { get; set; }       // Free-text name for non-customers
    public string? RecipientEmail { get; set; }

    // How it was issued
    public GiftCertificateIssuedReason IssuedReason { get; set; } = GiftCertificateIssuedReason.Sold;

    // If sold: what the buyer paid (may be less than face value for a promotional sale)
    public decimal? PurchasePrice { get; set; }
    public int? PurchasingCustomerId { get; set; }

    public GiftCertificateStatus Status { get; set; } = GiftCertificateStatus.Active;
    public DateTime IssueDate { get; set; } = DateTime.UtcNow;
    public DateTime? ExpiryDate { get; set; }
    public string? Notes { get; set; }
    public string? IssuedById { get; set; }

    /// <summary>Set when this GC was sold via an invoice line item.</summary>
    public int? SourceInvoiceItemId { get; set; }

    /// <summary>Groups all certificates created in a single bulk run. Null for individually issued certs.</summary>
    public Guid? BatchId { get; set; }

    // Navigation
    public virtual Customer? RecipientCustomer { get; set; }
    public virtual Customer? PurchasingCustomer { get; set; }
    public virtual ApplicationUser? IssuedBy { get; set; }
    public virtual ICollection<GiftCertificateRedemption> Redemptions { get; set; } = new List<GiftCertificateRedemption>();
}

public class GiftCertificateRedemption : BaseEntity
{
    public int GiftCertificateId { get; set; }
    public int InvoiceId { get; set; }
    public decimal AmountRedeemed { get; set; }
    public DateTime RedeemedDate { get; set; } = DateTime.UtcNow;
    public string? RedeemedById { get; set; }

    // Navigation
    public virtual GiftCertificate GiftCertificate { get; set; } = null!;
    public virtual Invoice Invoice { get; set; } = null!;
    public virtual ApplicationUser? RedeemedBy { get; set; }
}
