using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Domain.Entities.Members;

public class RefreshToken
{
    [ForeignKey(nameof(MemberID))]
    public virtual Member? Member { get; private set; }

    [Key]
    public long ID { get; private set; }

    public int MemberID { get; private set; }

    public string Token { get; private set; } = default!;

    public DateTime ExpiresAt { get; private set; }

    public DateTime CreatedAt { get; private set; } = DateTime.UtcNow;

    public bool IsRevoked { get; private set; } = false;

    public DateTime? RevokedAt { get; private set; }

    private RefreshToken() { }

    public static RefreshToken Create(
        int memberID,
        string token,
        DateTime expiresAt
    ) {
        return new RefreshToken
        {
            MemberID = memberID,
            Token = token,
            ExpiresAt = expiresAt
        };
    }

    public void Revoke()
    {
        IsRevoked = true;
        RevokedAt = DateTime.UtcNow;
    }

    public bool IsExpired => DateTime.UtcNow >= ExpiresAt;
    public bool IsActive => !IsRevoked && !IsExpired;
}