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

namespace SportsDivision.Infrastructure.Repositories;

public class TournamentRepository : Repository<Tournament>, ITournamentRepository
{
    public TournamentRepository(ApplicationDbContext context) : base(context) { }
    public override async Task<IEnumerable<Tournament>> GetAllAsync() => await _dbSet.Include(t => t.Zone).Include(t => t.TournamentEventLevels).OrderByDescending(t => t.StartDate).ToListAsync();
    public async Task<IEnumerable<Tournament>> GetActiveAsync() => await _dbSet.Where(t => !t.IsArchived).Include(t => t.Zone).Include(t => t.TournamentEventLevels).OrderByDescending(t => t.StartDate).ToListAsync();
    public async Task<Tournament?> GetWithEventLevelsAsync(int tournamentId) => await _dbSet.Include(t => t.TournamentEventLevels).ThenInclude(te => te.Event).Include(t => t.TournamentEventLevels).ThenInclude(te => te.EventLevel).Include(t => t.Zone).FirstOrDefaultAsync(t => t.TournamentId == tournamentId);
    public async Task<Tournament?> GetFullTournamentAsync(int tournamentId) => await _dbSet.Include(t => t.Zone).Include(t => t.TournamentEventLevels).ThenInclude(te => te.Event).Include(t => t.TournamentEventLevels).ThenInclude(te => te.EventLevel).Include(t => t.TournamentEventLevels).ThenInclude(te => te.Registrations).ThenInclude(r => r.Student).ThenInclude(s => s!.School).FirstOrDefaultAsync(t => t.TournamentId == tournamentId);
}
