using Femto.Common.Domain;

namespace Femto.Modules.Auth.Models;

public class SignupCode
{
    private static readonly TimeSpan ExpiryTime = TimeSpan.FromDays(14);
    public string Code { get; private set; }
    
    /// <summary>
    /// The email of the intended recipient
    /// </summary>
    public string RecipientEmail { get; private set; }
    
    /// <summary>
    /// The name of the intended recipient
    /// </summary>
    public string RecipientName { get; private set; }
    public DateTimeOffset CreatedAt { get; private set; }
    public DateTimeOffset? ExpiresAt { get; private set; }
    public Guid? RedeemingUserId { get; private set; }

    private SignupCode() { }
    
    public SignupCode(string recipientEmail, string recipientName, string code)
    {
        this.Code = code;
        this.RecipientEmail = recipientEmail;
        this.RecipientName = recipientName;
        this.CreatedAt = DateTimeOffset.UtcNow;
        this.ExpiresAt = this.CreatedAt + ExpiryTime;
    }

    public void Redeem(Guid userGuid)
    {
        if (this.RedeemingUserId is not null) 
            throw new DomainError("invalid signup code");
        
        this.RedeemingUserId = userGuid;
    }
}
