using Monitoring.Core.Domain.UserPrimitives;

namespace Monitoring.Core.Domain;

public class RefreshToken
{
	public Guid Id { get; private set; }
	public string Token { get; private set; } = null!;
	public DateTime ExpiryDate { get; private set; }
	public bool IsUsed { get; private set; }
	public bool IsRevoked { get; private set; }
	public UserId UserId { get; private set; }
	public User User { get; private set; } = null!;

	private RefreshToken() { }

	public static RefreshToken Create(string token, DateTime expiryDate, UserId userId)
	{
		return new RefreshToken
		{
			Id = Guid.NewGuid(),
			Token = token,
			ExpiryDate = expiryDate,
			UserId = userId,
			IsUsed = false,
			IsRevoked = false
		};
	}

	public void MarkAsUsed()
	{
		IsUsed = true;
	}

	public void Revoke()
	{
		IsRevoked = true;
	}

	public bool IsActive => !IsRevoked && !IsUsed && ExpiryDate > DateTime.UtcNow;
}
