using Meezi.Core.Constants;
using Meezi.Core.Enums;
using StackExchange.Redis;

namespace Meezi.API.Services;

public record TerminalInfoDto(string TerminalId, DateTime? LastSeenUtc);

public interface ITerminalRegistryService
{
    Task<(bool Allowed, string? ErrorCode, string? Message)> RegisterAsync(
        string cafeId,
        PlanTier tier,
        string terminalId,
        CancellationToken cancellationToken = default);

    Task<IReadOnlyList<TerminalInfoDto>> ListAsync(string cafeId, CancellationToken cancellationToken = default);

    Task RevokeAsync(string cafeId, string terminalId, CancellationToken cancellationToken = default);
}

public class TerminalRegistryService : ITerminalRegistryService
{
    private static readonly TimeSpan TerminalTtl = TimeSpan.FromDays(90);
    private readonly IConnectionMultiplexer _redis;

    public TerminalRegistryService(IConnectionMultiplexer redis) => _redis = redis;

    public async Task<(bool Allowed, string? ErrorCode, string? Message)> RegisterAsync(
        string cafeId,
        PlanTier tier,
        string terminalId,
        CancellationToken cancellationToken = default)
    {
        if (string.IsNullOrWhiteSpace(terminalId))
            return (false, "TERMINAL_ID_REQUIRED", "Terminal id is required.");

        terminalId = terminalId.Trim();
        var db = _redis.GetDatabase();
        var setKey = $"terminals:{cafeId}";
        var max = PlanLimits.MaxTerminals(tier);

        if (max == int.MaxValue)
        {
            await db.SetAddAsync(setKey, terminalId);
            await db.KeyExpireAsync(setKey, TerminalTtl);
            return (true, null, null);
        }

        var members = await db.SetMembersAsync(setKey);
        var known = members.Select(m => m.ToString()).ToHashSet(StringComparer.Ordinal);
        if (known.Contains(terminalId))
        {
            await db.KeyExpireAsync(setKey, TerminalTtl);
            return (true, null, null);
        }

        if (known.Count >= max)
            return (false, "PLAN_LIMIT_REACHED", "Terminal limit reached for your plan. Please upgrade.");

        await db.SetAddAsync(setKey, terminalId);
        await db.KeyExpireAsync(setKey, TerminalTtl);
        return (true, null, null);
    }

    public async Task<IReadOnlyList<TerminalInfoDto>> ListAsync(
        string cafeId,
        CancellationToken cancellationToken = default)
    {
        var db = _redis.GetDatabase();
        var setKey = $"terminals:{cafeId}";
        var members = await db.SetMembersAsync(setKey);
        return members
            .Select(m => m.ToString())
            .Where(id => !string.IsNullOrEmpty(id))
            .Select(id => new TerminalInfoDto(id!, null))
            .OrderBy(t => t.TerminalId)
            .ToList();
    }

    public async Task RevokeAsync(
        string cafeId,
        string terminalId,
        CancellationToken cancellationToken = default)
    {
        if (string.IsNullOrWhiteSpace(terminalId)) return;
        var db = _redis.GetDatabase();
        await db.SetRemoveAsync($"terminals:{cafeId}", terminalId.Trim());
    }
}
