using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace Indotalent.Features.Setting.Currency.Cqrs;

public record DeleteCurrencyByIdRequest(string Id);

public class DeleteCurrencyByIdHandler : IRequestHandler<DeleteCurrencyByIdCommand, bool>
{
    private readonly AppDbContext _context;

    public DeleteCurrencyByIdHandler(AppDbContext context) => _context = context;

    public async Task<bool> Handle(DeleteCurrencyByIdCommand request, CancellationToken cancellationToken)
    {
        var entity = await _context.Currency
            .FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken);

        if (entity == null) return false;

        _context.Currency.Remove(entity);
        return await _context.SaveChangesAsync(cancellationToken) > 0;
    }
}

public record DeleteCurrencyByIdCommand(DeleteCurrencyByIdRequest Data) : IRequest<bool>;