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;
    }

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

    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);

    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);
}
