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

namespace Indotalent.Features.Utilities.BookingResource.Cqrs;

public class DeleteBookingResourceByIdRequest
{
    public DeleteBookingResourceByIdRequest(string id) => Id = id;
    public string Id { get; set; }
}

public record DeleteBookingResourceByIdCommand(DeleteBookingResourceByIdRequest Data) : IRequest<bool>;

public class DeleteBookingResourceByIdHandler : IRequestHandler<DeleteBookingResourceByIdCommand, bool>
{
    private readonly AppDbContext _context;

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

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

        if (entity == null)
        {
            return false;
        }

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