using Microsoft.EntityFrameworkCore;
using SportsDivision.Domain.Entities;
using SportsDivision.Domain.Interfaces;
using SportsDivision.Infrastructure.Data;

namespace SportsDivision.Infrastructure.Repositories;

public class ScoreRepository : Repository<Score>, IScoreRepository
{
    public ScoreRepository(ApplicationDbContext context) : base(context) { }
    public async Task<IEnumerable<Score>> GetByTournamentEventLevelAsync(int tournamentEventLevelId) => await _dbSet.Where(s => s.EventRegistration.TournamentEventLevelId == tournamentEventLevelId).Include(s => s.EventRegistration).ThenInclude(r => r.Student).ThenInclude(s => s!.School).OrderBy(s => s.Placement).ToListAsync();
    public async Task<Score?> GetByRegistrationAsync(int eventRegistrationId) => await _dbSet.FirstOrDefaultAsync(s => s.EventRegistrationId == eventRegistrationId);
    public async Task<IEnumerable<Score>> GetBySchoolAndTournamentAsync(int schoolId, int tournamentId) => await _dbSet.Where(s => s.EventRegistration.Student != null && s.EventRegistration.Student.SchoolId == schoolId && s.EventRegistration.TournamentEventLevel.TournamentId == tournamentId).Include(s => s.EventRegistration).ThenInclude(r => r.Student).Include(s => s.EventRegistration).ThenInclude(r => r.TournamentEventLevel).ThenInclude(t => t.Event).OrderBy(s => s.Placement).ToListAsync();
}
