using Microsoft.EntityFrameworkCore;
using ScadaLink.Commons.Entities.ExternalSystems;
using ScadaLink.Commons.Interfaces.Repositories;

namespace ScadaLink.ConfigurationDatabase.Repositories;

public class ExternalSystemRepository : IExternalSystemRepository
{
    private readonly ScadaLinkDbContext _context;

    public ExternalSystemRepository(ScadaLinkDbContext context)
    {
        _context = context ?? throw new ArgumentNullException(nameof(context));
    }

    public async Task<ExternalSystemDefinition?> GetExternalSystemByIdAsync(int id, CancellationToken cancellationToken = default)
        => await _context.Set<ExternalSystemDefinition>().FindAsync(new object[] { id }, cancellationToken);

    // ExternalSystemGateway-011: genuine name-keyed query (server-side WHERE) so the
    // gateway's hot-path resolution does not fetch every system and filter in memory.
    public async Task<ExternalSystemDefinition?> GetExternalSystemByNameAsync(string name, CancellationToken cancellationToken = default)
        => await _context.Set<ExternalSystemDefinition>()
            .FirstOrDefaultAsync(s => s.Name == name, cancellationToken);

    public async Task<IReadOnlyList<ExternalSystemDefinition>> GetAllExternalSystemsAsync(CancellationToken cancellationToken = default)
        => await _context.Set<ExternalSystemDefinition>().ToListAsync(cancellationToken);

    public async Task AddExternalSystemAsync(ExternalSystemDefinition definition, CancellationToken cancellationToken = default)
        => await _context.Set<ExternalSystemDefinition>().AddAsync(definition, cancellationToken);

    public Task UpdateExternalSystemAsync(ExternalSystemDefinition definition, CancellationToken cancellationToken = default)
    { _context.Set<ExternalSystemDefinition>().Update(definition); return Task.CompletedTask; }

    public async Task DeleteExternalSystemAsync(int id, CancellationToken cancellationToken = default)
    {
        var entity = await GetExternalSystemByIdAsync(id, cancellationToken);
        if (entity != null) _context.Set<ExternalSystemDefinition>().Remove(entity);
    }

    public async Task<ExternalSystemMethod?> GetExternalSystemMethodByIdAsync(int id, CancellationToken cancellationToken = default)
        => await _context.Set<ExternalSystemMethod>().FindAsync(new object[] { id }, cancellationToken);

    // ExternalSystemGateway-011: genuine name-keyed query scoped to the parent system.
    public async Task<ExternalSystemMethod?> GetMethodByNameAsync(int externalSystemId, string methodName, CancellationToken cancellationToken = default)
        => await _context.Set<ExternalSystemMethod>()
            .FirstOrDefaultAsync(
                m => m.ExternalSystemDefinitionId == externalSystemId && m.Name == methodName,
                cancellationToken);

    public async Task<IReadOnlyList<ExternalSystemMethod>> GetMethodsByExternalSystemIdAsync(int externalSystemId, CancellationToken cancellationToken = default)
        => await _context.Set<ExternalSystemMethod>().Where(m => m.ExternalSystemDefinitionId == externalSystemId).ToListAsync(cancellationToken);

    public async Task AddExternalSystemMethodAsync(ExternalSystemMethod method, CancellationToken cancellationToken = default)
        => await _context.Set<ExternalSystemMethod>().AddAsync(method, cancellationToken);

    public Task UpdateExternalSystemMethodAsync(ExternalSystemMethod method, CancellationToken cancellationToken = default)
    { _context.Set<ExternalSystemMethod>().Update(method); return Task.CompletedTask; }

    public async Task DeleteExternalSystemMethodAsync(int id, CancellationToken cancellationToken = default)
    {
        var entity = await GetExternalSystemMethodByIdAsync(id, cancellationToken);
        if (entity != null) _context.Set<ExternalSystemMethod>().Remove(entity);
    }

    public async Task<DatabaseConnectionDefinition?> GetDatabaseConnectionByIdAsync(int id, CancellationToken cancellationToken = default)
        => await _context.Set<DatabaseConnectionDefinition>().FindAsync(new object[] { id }, cancellationToken);

    // ExternalSystemGateway-011: genuine name-keyed query (server-side WHERE).
    public async Task<DatabaseConnectionDefinition?> GetDatabaseConnectionByNameAsync(string name, CancellationToken cancellationToken = default)
        => await _context.Set<DatabaseConnectionDefinition>()
            .FirstOrDefaultAsync(c => c.Name == name, cancellationToken);

    public async Task<IReadOnlyList<DatabaseConnectionDefinition>> GetAllDatabaseConnectionsAsync(CancellationToken cancellationToken = default)
        => await _context.Set<DatabaseConnectionDefinition>().ToListAsync(cancellationToken);

    public async Task AddDatabaseConnectionAsync(DatabaseConnectionDefinition definition, CancellationToken cancellationToken = default)
        => await _context.Set<DatabaseConnectionDefinition>().AddAsync(definition, cancellationToken);

    public Task UpdateDatabaseConnectionAsync(DatabaseConnectionDefinition definition, CancellationToken cancellationToken = default)
    { _context.Set<DatabaseConnectionDefinition>().Update(definition); return Task.CompletedTask; }

    public async Task DeleteDatabaseConnectionAsync(int id, CancellationToken cancellationToken = default)
    {
        var entity = await GetDatabaseConnectionByIdAsync(id, cancellationToken);
        if (entity != null) _context.Set<DatabaseConnectionDefinition>().Remove(entity);
    }

    public async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
        => await _context.SaveChangesAsync(cancellationToken);
}
