namespace Application.Professors.Queries;

using Application.Professors.DTOs;
using Domain.Ports.Repositories;
using MediatR;

public record GetProfessorsQuery : IRequest<IReadOnlyList<ProfessorDto>>;

public class GetProfessorsHandler(IProfessorRepository professorRepository)
    : IRequestHandler<GetProfessorsQuery, IReadOnlyList<ProfessorDto>>
{
    public async Task<IReadOnlyList<ProfessorDto>> Handle(GetProfessorsQuery request, CancellationToken ct)
    {
        // Direct projection - only SELECT needed columns
        return await professorRepository.GetAllProjectedAsync(
            p => new ProfessorDto(
                p.Id,
                p.Name,
                p.Subjects.Select(s => new ProfessorSubjectDto(s.Id, s.Name, s.Credits)).ToList()
            ), ct);
    }
}
