using System.Security.Cryptography;
using Microsoft.Extensions.Options;
using OmsorgCore.Application.Abstractions;

namespace OmsorgCore.Infrastructure.Security;

public class RefreshTokenGenerator : IRefreshTokenGenerator
{
    private readonly RefreshTokenOptions _options;

    public RefreshTokenGenerator(IOptions<RefreshTokenOptions> options)
    {
        _options = options.Value;
    }

    public (string RawToken, string TokenHash, DateTime ExpiresAt) GenerateToken()
    {
        var rawToken = Convert.ToBase64String(RandomNumberGenerator.GetBytes(64));
        var expiresAt = DateTime.UtcNow.AddDays(_options.ExpiryDays);
        return (rawToken, HashToken(rawToken), expiresAt);
    }

    public string HashToken(string rawToken)
        => Convert.ToHexString(SHA256.HashData(System.Text.Encoding.UTF8.GetBytes(rawToken)));
}
