namespace Adapters.Driven.Persistence.Repositories;

using Adapters.Driven.Persistence.Context;
using Domain.Entities;
using Domain.Ports.Repositories;
using Microsoft.EntityFrameworkCore;
using System.Linq.Expressions;

public class ProfessorRepository(AppDbContext context) : IProfessorRepository
{
    public async Task<Professor?> GetByIdAsync(int id, CancellationToken ct = default) =>
        await context.Professors.FindAsync([id], ct);

    public async Task<IReadOnlyList<Professor>> GetAllAsync(CancellationToken ct = default) =>
        await context.Professors.AsNoTracking().ToListAsync(ct);

    public async Task<IReadOnlyList<Professor>> GetAllWithSubjectsAsync(CancellationToken ct = default) =>
        await context.Professors
            .Include(p => p.Subjects)
            .AsNoTracking()
            .ToListAsync(ct);

    // Direct projection - only SELECT needed columns
    public async Task<IReadOnlyList<TResult>> GetAllProjectedAsync<TResult>(
        Expression<Func<Professor, TResult>> selector,
        CancellationToken ct = default) =>
        await context.Professors
            .AsNoTracking()
            .Select(selector)
            .ToListAsync(ct);
}
