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

namespace Indotalent.Features.Pipeline.SalesTeam.Cqrs;

public record DeleteSalesTeamByIdRequest(string Id);

public record DeleteSalesTeamByIdCommand(DeleteSalesTeamByIdRequest Data) : IRequest<bool>;

public class DeleteSalesTeamByIdHandler : IRequestHandler<DeleteSalesTeamByIdCommand, bool>
{
    private readonly AppDbContext _context;

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

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

        if (entity == null) return false;

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