namespace CashLineERP.Domain.Procurement;

public sealed class RequestForQuotationSupplier
{
    private const string InvitedStatus = "INVITED";
    private const string AcceptedStatus = "ACCEPTED";
    private const string DeclinedStatus = "DECLINED";

    public long Id { get; private set; }

    public long RequestForQuotationId { get; private set; }

    public long SupplierId { get; private set; }

    public string InviteStatus { get; private set; } = InvitedStatus;

    public DateTime? InvitedAtUtc { get; private set; } = DateTime.UtcNow;

    public DateTime? RespondedAtUtc { get; private set; }

    public string? DeclineReason { get; private set; }

    public RequestForQuotationSupplier(long requestForQuotationId, long supplierId)
    {
        RequestForQuotationId = requestForQuotationId;
        SupplierId = supplierId;
    }

    private RequestForQuotationSupplier()
    {
    }

    public void Accept()
    {
        if (string.Equals(InviteStatus, AcceptedStatus, StringComparison.OrdinalIgnoreCase))
        {
            return;
        }

        InviteStatus = AcceptedStatus;
        RespondedAtUtc = DateTime.UtcNow;
        DeclineReason = null;
    }

    public void Decline(string? declineReason)
    {
        if (string.IsNullOrWhiteSpace(declineReason))
        {
            throw new InvalidOperationException("RFQ decline requires a decline reason.");
        }

        InviteStatus = DeclinedStatus;
        RespondedAtUtc = DateTime.UtcNow;
        DeclineReason = declineReason.Trim();
    }
}
