using System.Text;
using System.Text.Json;
using System.Security.Cryptography;
using GestionaDenuncias.Shared.Models;
using Microsoft.AspNetCore.DataProtection;

namespace ApiDenuncias.Services;

public sealed class GlobalLeaksSessionStore
{
    private const string RootPath = @"C:\GestionaDenuncias\.gl-auth";
    private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web)
    {
        WriteIndented = false,
    };

    private readonly SemaphoreSlim _gate = new(1, 1);
    private readonly IDataProtector _protector;

    public GlobalLeaksSessionStore(IDataProtectionProvider dataProtectionProvider)
    {
        _protector = dataProtectionProvider.CreateProtector("GestionaDenunciasAN.GlobalLeaksSessionStore");
    }

    public async Task<GlobalLeaksStoredSession?> GetAsync(string username, CancellationToken cancellationToken = default)
    {
        if (string.IsNullOrWhiteSpace(username))
        {
            return null;
        }

        var path = GetFilePath(username);
        if (!File.Exists(path))
        {
            return null;
        }

        await _gate.WaitAsync(cancellationToken);
        try
        {
            var protectedBytes = await File.ReadAllBytesAsync(path, cancellationToken);
            var protectedBase64 = Encoding.UTF8.GetString(protectedBytes);
            var json = _protector.Unprotect(protectedBase64);
            return JsonSerializer.Deserialize<GlobalLeaksStoredSession>(json, JsonOptions);
        }
        finally
        {
            _gate.Release();
        }
    }

    public async Task SaveAsync(
        string username,
        string password,
        string sessionId,
        string? role,
        CancellationToken cancellationToken = default)
    {
        var data = new GlobalLeaksStoredSession
        {
            Username = username,
            Password = password,
            SessionId = sessionId,
            Role = role,
            UpdatedAt = DateTimeOffset.UtcNow,
        };

        await WriteAsync(data, cancellationToken);
    }

    public async Task UpdateSessionAsync(
        string username,
        string sessionId,
        string? role,
        CancellationToken cancellationToken = default)
    {
        var current = await GetAsync(username, cancellationToken)
            ?? throw new InvalidOperationException("No hay credenciales guardadas para este usuario.");

        current.SessionId = sessionId;
        current.Role = role;
        current.UpdatedAt = DateTimeOffset.UtcNow;

        await WriteAsync(current, cancellationToken);
    }

    public async Task ClearSessionAsync(string username, CancellationToken cancellationToken = default)
    {
        var current = await GetAsync(username, cancellationToken);
        if (current is null)
        {
            return;
        }

        current.SessionId = null;
        current.UpdatedAt = DateTimeOffset.UtcNow;
        await WriteAsync(current, cancellationToken);
    }

    public async Task DeleteAsync(string username, CancellationToken cancellationToken = default)
    {
        if (string.IsNullOrWhiteSpace(username))
        {
            return;
        }

        var path = GetFilePath(username);

        await _gate.WaitAsync(cancellationToken);
        try
        {
            if (File.Exists(path))
            {
                File.Delete(path);
            }
        }
        finally
        {
            _gate.Release();
        }
    }

    private async Task WriteAsync(GlobalLeaksStoredSession data, CancellationToken cancellationToken)
    {
        Directory.CreateDirectory(RootPath);

        var path = GetFilePath(data.Username);
        var json = JsonSerializer.Serialize(data, JsonOptions);
        var protectedValue = _protector.Protect(json);
        var protectedBytes = Encoding.UTF8.GetBytes(protectedValue);

        await _gate.WaitAsync(cancellationToken);
        try
        {
            await File.WriteAllBytesAsync(path, protectedBytes, cancellationToken);
        }
        finally
        {
            _gate.Release();
        }
    }

    private static string GetFilePath(string username)
    {
        Directory.CreateDirectory(RootPath);
        var normalized = username.Trim().ToLowerInvariant();
        var hash = SHA256.HashData(Encoding.UTF8.GetBytes(normalized));
        return Path.Combine(RootPath, $"{Convert.ToHexString(hash).ToLowerInvariant()}.bin");
    }
}
