namespace Application.Students.Commands;

using Domain.Exceptions;
using Domain.Ports.Repositories;
using MediatR;

public record DeleteStudentCommand(int Id) : IRequest<bool>;

public class DeleteStudentHandler(
    IStudentRepository studentRepository,
    IUnitOfWork unitOfWork)
    : IRequestHandler<DeleteStudentCommand, bool>
{
    public async Task<bool> Handle(DeleteStudentCommand request, CancellationToken ct)
    {
        var student = await studentRepository.GetByIdAsync(request.Id, ct)
            ?? throw new StudentNotFoundException(request.Id);

        studentRepository.Delete(student);
        await unitOfWork.SaveChangesAsync(ct);

        return true;
    }
}
