using System.Text.Json;
using Microsoft.Data.Sqlite;
using ScadaLink.Commons.Entities.ExternalSystems;
using ScadaLink.Commons.Interfaces.Repositories;
using ScadaLink.SiteRuntime.Persistence;

namespace ScadaLink.SiteRuntime.Repositories;

/// <summary>
/// Site-side read-only implementation of <see cref="IExternalSystemRepository"/>
/// backed by the local SQLite database via <see cref="SiteStorageService"/>.
/// Write operations throw <see cref="NotSupportedException"/> because site-local
/// artifacts are managed exclusively through deployment from Central.
/// </summary>
public class SiteExternalSystemRepository : IExternalSystemRepository
{
    private readonly SiteStorageService _storage;

    public SiteExternalSystemRepository(SiteStorageService storage)
    {
        _storage = storage ?? throw new ArgumentNullException(nameof(storage));
    }

    // ── ExternalSystemDefinition (read) ──

    public async Task<IReadOnlyList<ExternalSystemDefinition>> GetAllExternalSystemsAsync(
        CancellationToken cancellationToken = default)
    {
        await using var connection = CreateConnection();
        await connection.OpenAsync(cancellationToken);

        await using var command = connection.CreateCommand();
        command.CommandText = @"
            SELECT name, endpoint_url, auth_type, auth_configuration
            FROM external_systems";

        var results = new List<ExternalSystemDefinition>();
        await using var reader = await command.ExecuteReaderAsync(cancellationToken);
        while (await reader.ReadAsync(cancellationToken))
        {
            results.Add(MapExternalSystem(reader));
        }

        return results;
    }

    public async Task<ExternalSystemDefinition?> GetExternalSystemByIdAsync(
        int id, CancellationToken cancellationToken = default)
    {
        // The SQLite table is keyed by name, not by integer ID.
        // Scan all rows and match on the synthetic ID derived from the name.
        var all = await GetAllExternalSystemsAsync(cancellationToken);
        return all.FirstOrDefault(e => e.Id == id);
    }

    /// <summary>
    /// ExternalSystemGateway-011: genuine name-keyed lookup. The <c>external_systems</c>
    /// table has <c>name</c> as its PRIMARY KEY, so this is a single indexed-row fetch
    /// rather than the previous fetch-all-then-filter.
    /// </summary>
    public async Task<ExternalSystemDefinition?> GetExternalSystemByNameAsync(
        string name, CancellationToken cancellationToken = default)
    {
        await using var connection = CreateConnection();
        await connection.OpenAsync(cancellationToken);

        await using var command = connection.CreateCommand();
        command.CommandText = @"
            SELECT name, endpoint_url, auth_type, auth_configuration
            FROM external_systems
            WHERE name = @name";
        command.Parameters.AddWithValue("@name", name);

        await using var reader = await command.ExecuteReaderAsync(cancellationToken);
        if (!await reader.ReadAsync(cancellationToken))
            return null;

        return MapExternalSystem(reader);
    }

    // ── ExternalSystemMethod (read) ──

    public async Task<IReadOnlyList<ExternalSystemMethod>> GetMethodsByExternalSystemIdAsync(
        int externalSystemId, CancellationToken cancellationToken = default)
    {
        // Find the parent system to get its name, then parse its method_definitions JSON.
        var system = await GetExternalSystemByIdAsync(externalSystemId, cancellationToken);
        if (system is null)
            return Array.Empty<ExternalSystemMethod>();

        await using var connection = CreateConnection();
        await connection.OpenAsync(cancellationToken);

        await using var command = connection.CreateCommand();
        command.CommandText = @"
            SELECT method_definitions
            FROM external_systems
            WHERE name = @name";
        command.Parameters.AddWithValue("@name", system.Name);

        var json = (string?)await command.ExecuteScalarAsync(cancellationToken);
        if (string.IsNullOrWhiteSpace(json))
            return Array.Empty<ExternalSystemMethod>();

        return ParseMethodDefinitions(json, externalSystemId);
    }

    /// <summary>
    /// ExternalSystemGateway-011: name-keyed method lookup. The site store keeps a
    /// system's methods in a single JSON column, so this fetches that one row and
    /// matches the named method in memory. The parent system is resolved via
    /// <see cref="GetMethodsByExternalSystemIdAsync"/>, which already performs a
    /// single keyed read of the <c>method_definitions</c> column — this is no more
    /// I/O than the gateway's previous fetch-all-methods-then-filter call, and on
    /// the SQL-backed Central repository it is a genuine indexed query.
    /// </summary>
    public async Task<ExternalSystemMethod?> GetMethodByNameAsync(
        int externalSystemId, string methodName, CancellationToken cancellationToken = default)
    {
        var methods = await GetMethodsByExternalSystemIdAsync(externalSystemId, cancellationToken);
        return methods.FirstOrDefault(
            m => m.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase));
    }

    public async Task<ExternalSystemMethod?> GetExternalSystemMethodByIdAsync(
        int id, CancellationToken cancellationToken = default)
    {
        // Scan all systems and their methods to find the matching synthetic ID.
        var systems = await GetAllExternalSystemsAsync(cancellationToken);
        foreach (var system in systems)
        {
            var methods = await GetMethodsByExternalSystemIdAsync(system.Id, cancellationToken);
            var match = methods.FirstOrDefault(m => m.Id == id);
            if (match is not null)
                return match;
        }

        return null;
    }

    // ── DatabaseConnectionDefinition (read) ──

    public async Task<IReadOnlyList<DatabaseConnectionDefinition>> GetAllDatabaseConnectionsAsync(
        CancellationToken cancellationToken = default)
    {
        await using var connection = CreateConnection();
        await connection.OpenAsync(cancellationToken);

        await using var command = connection.CreateCommand();
        command.CommandText = @"
            SELECT name, connection_string, max_retries, retry_delay_ms
            FROM database_connections";

        var results = new List<DatabaseConnectionDefinition>();
        await using var reader = await command.ExecuteReaderAsync(cancellationToken);
        while (await reader.ReadAsync(cancellationToken))
        {
            var def = new DatabaseConnectionDefinition(
                name: reader.GetString(0),
                connectionString: reader.GetString(1))
            {
                Id = GenerateSyntheticId(reader.GetString(0)),
                MaxRetries = reader.GetInt32(2),
                RetryDelay = TimeSpan.FromMilliseconds(reader.GetInt64(3))
            };
            results.Add(def);
        }

        return results;
    }

    public async Task<DatabaseConnectionDefinition?> GetDatabaseConnectionByIdAsync(
        int id, CancellationToken cancellationToken = default)
    {
        var all = await GetAllDatabaseConnectionsAsync(cancellationToken);
        return all.FirstOrDefault(d => d.Id == id);
    }

    /// <summary>
    /// ExternalSystemGateway-011: genuine name-keyed lookup. The
    /// <c>database_connections</c> table has <c>name</c> as its PRIMARY KEY, so this
    /// is a single indexed-row fetch rather than fetch-all-then-filter.
    /// </summary>
    public async Task<DatabaseConnectionDefinition?> GetDatabaseConnectionByNameAsync(
        string name, CancellationToken cancellationToken = default)
    {
        await using var connection = CreateConnection();
        await connection.OpenAsync(cancellationToken);

        await using var command = connection.CreateCommand();
        command.CommandText = @"
            SELECT name, connection_string, max_retries, retry_delay_ms
            FROM database_connections
            WHERE name = @name";
        command.Parameters.AddWithValue("@name", name);

        await using var reader = await command.ExecuteReaderAsync(cancellationToken);
        if (!await reader.ReadAsync(cancellationToken))
            return null;

        return new DatabaseConnectionDefinition(
            name: reader.GetString(0),
            connectionString: reader.GetString(1))
        {
            Id = GenerateSyntheticId(reader.GetString(0)),
            MaxRetries = reader.GetInt32(2),
            RetryDelay = TimeSpan.FromMilliseconds(reader.GetInt64(3))
        };
    }

    // ── Write operations (not supported on site) ──

    public Task AddExternalSystemAsync(ExternalSystemDefinition definition, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    public Task UpdateExternalSystemAsync(ExternalSystemDefinition definition, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    public Task DeleteExternalSystemAsync(int id, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    public Task AddExternalSystemMethodAsync(ExternalSystemMethod method, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    public Task UpdateExternalSystemMethodAsync(ExternalSystemMethod method, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    public Task DeleteExternalSystemMethodAsync(int id, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    public Task AddDatabaseConnectionAsync(DatabaseConnectionDefinition definition, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    public Task UpdateDatabaseConnectionAsync(DatabaseConnectionDefinition definition, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    public Task DeleteDatabaseConnectionAsync(int id, CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    public Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
        => throw new NotSupportedException("Managed via artifact deployment from Central");

    // ── Private helpers ──

    /// <summary>
    /// Creates a new SQLite connection against the site database via
    /// <see cref="SiteStorageService.CreateConnection"/> (SiteRuntime-006). The
    /// connection string is owned by <see cref="SiteStorageService"/>; the repository
    /// no longer reaches into its private state via reflection.
    /// </summary>
    private SqliteConnection CreateConnection() => _storage.CreateConnection();

    private static ExternalSystemDefinition MapExternalSystem(SqliteDataReader reader)
    {
        var name = reader.GetString(0);
        return new ExternalSystemDefinition(
            name: name,
            endpointUrl: reader.GetString(1),
            authType: reader.GetString(2))
        {
            Id = GenerateSyntheticId(name),
            AuthConfiguration = reader.IsDBNull(3) ? null : reader.GetString(3)
        };
    }

    private static IReadOnlyList<ExternalSystemMethod> ParseMethodDefinitions(
        string json, int externalSystemId)
    {
        try
        {
            var methods = JsonSerializer.Deserialize<List<MethodDefinitionDto>>(json,
                new JsonSerializerOptions { PropertyNameCaseInsensitive = true });

            if (methods is null)
                return Array.Empty<ExternalSystemMethod>();

            return methods.Select(m => new ExternalSystemMethod(
                name: m.Name ?? string.Empty,
                httpMethod: m.HttpMethod ?? "GET",
                path: m.Path ?? string.Empty)
            {
                Id = GenerateSyntheticId($"{externalSystemId}:{m.Name}"),
                ExternalSystemDefinitionId = externalSystemId,
                ParameterDefinitions = m.ParameterDefinitions,
                ReturnDefinition = m.ReturnDefinition
            }).ToList();
        }
        catch (JsonException)
        {
            return Array.Empty<ExternalSystemMethod>();
        }
    }

    /// <summary>
    /// Generates a stable positive integer ID from a string name (SiteRuntime-007).
    /// Uses a deterministic FNV-1a hash rather than <see cref="string.GetHashCode()"/>,
    /// which is randomized per process on .NET Core and would therefore change every
    /// time the process restarts — breaking any caller that stored an ID and later
    /// looks the entity up by that ID.
    /// </summary>
    private static int GenerateSyntheticId(string name) => SyntheticId.From(name);

    /// <summary>
    /// DTO for deserializing individual method entries from the method_definitions JSON column.
    /// </summary>
    private sealed class MethodDefinitionDto
    {
        public string? Name { get; set; }
        public string? HttpMethod { get; set; }
        public string? Path { get; set; }
        public string? ParameterDefinitions { get; set; }
        public string? ReturnDefinition { get; set; }
    }
}
