using Microsoft.EntityFrameworkCore;
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Security;
using ZB.MOM.WW.ScadaBridge.Commons.Interfaces.Repositories;

namespace ZB.MOM.WW.ScadaBridge.ConfigurationDatabase.Repositories;

public class SecurityRepository : ISecurityRepository
{
    private readonly ScadaBridgeDbContext _context;

    /// <summary>
    /// Initializes a new instance of the SecurityRepository.
    /// </summary>
    /// <param name="context">The database context.</param>
    public SecurityRepository(ScadaBridgeDbContext context)
    {
        _context = context ?? throw new ArgumentNullException(nameof(context));
    }

    // LdapGroupMapping

    /// <inheritdoc />
    public async Task<LdapGroupMapping?> GetMappingByIdAsync(int id, CancellationToken cancellationToken = default)
    {
        return await _context.LdapGroupMappings.FindAsync(new object[] { id }, cancellationToken);
    }

    /// <inheritdoc />
    public async Task<IReadOnlyList<LdapGroupMapping>> GetAllMappingsAsync(CancellationToken cancellationToken = default)
    {
        return await _context.LdapGroupMappings.ToListAsync(cancellationToken);
    }

    /// <inheritdoc />
    public async Task<IReadOnlyList<LdapGroupMapping>> GetMappingsByRoleAsync(string role, CancellationToken cancellationToken = default)
    {
        return await _context.LdapGroupMappings
            .Where(m => m.Role == role)
            .ToListAsync(cancellationToken);
    }

    /// <inheritdoc />
    public async Task AddMappingAsync(LdapGroupMapping mapping, CancellationToken cancellationToken = default)
    {
        await _context.LdapGroupMappings.AddAsync(mapping, cancellationToken);
    }

    /// <inheritdoc />
    public Task UpdateMappingAsync(LdapGroupMapping mapping, CancellationToken cancellationToken = default)
    {
        _context.LdapGroupMappings.Update(mapping);
        return Task.CompletedTask;
    }

    /// <inheritdoc />
    public async Task DeleteMappingAsync(int id, CancellationToken cancellationToken = default)
    {
        var mapping = await _context.LdapGroupMappings.FindAsync(new object[] { id }, cancellationToken);
        if (mapping != null)
        {
            _context.LdapGroupMappings.Remove(mapping);
        }
    }

    // SiteScopeRule

    /// <inheritdoc />
    public async Task<SiteScopeRule?> GetScopeRuleByIdAsync(int id, CancellationToken cancellationToken = default)
    {
        return await _context.SiteScopeRules.FindAsync(new object[] { id }, cancellationToken);
    }

    /// <inheritdoc />
    public async Task<IReadOnlyList<SiteScopeRule>> GetScopeRulesForMappingAsync(int ldapGroupMappingId, CancellationToken cancellationToken = default)
    {
        return await _context.SiteScopeRules
            .Where(r => r.LdapGroupMappingId == ldapGroupMappingId)
            .ToListAsync(cancellationToken);
    }

    /// <inheritdoc />
    public async Task AddScopeRuleAsync(SiteScopeRule rule, CancellationToken cancellationToken = default)
    {
        await _context.SiteScopeRules.AddAsync(rule, cancellationToken);
    }

    /// <inheritdoc />
    public Task UpdateScopeRuleAsync(SiteScopeRule rule, CancellationToken cancellationToken = default)
    {
        _context.SiteScopeRules.Update(rule);
        return Task.CompletedTask;
    }

    /// <inheritdoc />
    public async Task DeleteScopeRuleAsync(int id, CancellationToken cancellationToken = default)
    {
        var rule = await _context.SiteScopeRules.FindAsync(new object[] { id }, cancellationToken);
        if (rule != null)
        {
            _context.SiteScopeRules.Remove(rule);
        }
    }

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