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

namespace ScadaLink.ConfigurationDatabase.Repositories;

public class NotificationRepository : INotificationRepository
{
    private readonly ScadaLinkDbContext _context;

    public NotificationRepository(ScadaLinkDbContext context)
    {
        _context = context;
    }

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

    public async Task<IReadOnlyList<NotificationList>> GetAllNotificationListsAsync(CancellationToken cancellationToken = default)
        => await _context.Set<NotificationList>().Include(n => n.Recipients).ToListAsync(cancellationToken);

    public async Task<NotificationList?> GetListByNameAsync(string name, CancellationToken cancellationToken = default)
        => await _context.Set<NotificationList>().FirstOrDefaultAsync(l => l.Name == name, cancellationToken);

    public async Task AddNotificationListAsync(NotificationList list, CancellationToken cancellationToken = default)
        => await _context.Set<NotificationList>().AddAsync(list, cancellationToken);

    public Task UpdateNotificationListAsync(NotificationList list, CancellationToken cancellationToken = default)
    { _context.Set<NotificationList>().Update(list); return Task.CompletedTask; }

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

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

    public async Task<IReadOnlyList<NotificationRecipient>> GetRecipientsByListIdAsync(int notificationListId, CancellationToken cancellationToken = default)
        => await _context.Set<NotificationRecipient>().Where(r => r.NotificationListId == notificationListId).ToListAsync(cancellationToken);

    public async Task AddRecipientAsync(NotificationRecipient recipient, CancellationToken cancellationToken = default)
        => await _context.Set<NotificationRecipient>().AddAsync(recipient, cancellationToken);

    public Task UpdateRecipientAsync(NotificationRecipient recipient, CancellationToken cancellationToken = default)
    { _context.Set<NotificationRecipient>().Update(recipient); return Task.CompletedTask; }

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

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

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

    public async Task AddSmtpConfigurationAsync(SmtpConfiguration configuration, CancellationToken cancellationToken = default)
        => await _context.Set<SmtpConfiguration>().AddAsync(configuration, cancellationToken);

    public Task UpdateSmtpConfigurationAsync(SmtpConfiguration configuration, CancellationToken cancellationToken = default)
    { _context.Set<SmtpConfiguration>().Update(configuration); return Task.CompletedTask; }

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

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