namespace Application.Students.Commands;

using Application.Students.DTOs;
using Domain.Entities;
using Domain.Ports.Repositories;
using Domain.ValueObjects;
using MediatR;

public record CreateStudentCommand(string Name, string Email) : IRequest<StudentDto>;

public class CreateStudentHandler(
    IStudentRepository studentRepository,
    IUnitOfWork unitOfWork)
    : IRequestHandler<CreateStudentCommand, StudentDto>
{
    public async Task<StudentDto> Handle(CreateStudentCommand request, CancellationToken ct)
    {
        var email = Email.Create(request.Email);
        var student = new Student(request.Name, email);

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

        return new StudentDto(student.Id, student.Name, student.Email.Value, 0, []);
    }
}
