using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using SportsDivision.Application.DTOs; using SportsDivision.Application.Interfaces; using SportsDivision.Domain.Enums; namespace SportsDivision.Web.Controllers; [Authorize] public class HighJumpController : Controller { private readonly IHighJumpService _highJumpService; private readonly IRegistrationService _registrationService; private readonly ITournamentService _tournamentService; private readonly IScoringService _scoringService; public HighJumpController( IHighJumpService highJumpService, IRegistrationService registrationService, ITournamentService tournamentService, IScoringService scoringService) { _highJumpService = highJumpService; _registrationService = registrationService; _tournamentService = tournamentService; _scoringService = scoringService; } [HttpGet] public async Task Index(int tournamentEventLevelId) { if (tournamentEventLevelId <= 0) { ViewBag.ScoringTargets = await _tournamentService.GetEventLevelsByCategoryAsync(EventCategory.HighJump); ViewBag.ScoringController = "HighJump"; ViewBag.ScoringTitle = "High Jump Scoring"; return View("SelectEventLevel"); } var heights = await _highJumpService.GetHeightsAsync(tournamentEventLevelId); var registrations = await _registrationService.GetByTournamentEventLevelAsync(tournamentEventLevelId); ViewBag.TournamentEventLevelId = tournamentEventLevelId; ViewBag.Registrations = registrations; return View(heights); } [HttpPost] [ValidateAntiForgeryToken] public async Task AddHeight(HighJumpHeightCreateDto dto) { if (!ModelState.IsValid) { return RedirectToAction(nameof(Index), new { tournamentEventLevelId = dto.TournamentEventLevelId }); } await _highJumpService.AddHeightAsync(dto); return RedirectToAction(nameof(Index), new { tournamentEventLevelId = dto.TournamentEventLevelId }); } [HttpPost] [ValidateAntiForgeryToken] public async Task RemoveHeight(int heightId, int tournamentEventLevelId) { await _highJumpService.RemoveHeightAsync(heightId); return RedirectToAction(nameof(Index), new { tournamentEventLevelId }); } [HttpPost] [ValidateAntiForgeryToken] public async Task RecordAttempt(HighJumpAttemptUpdateDto dto, int tournamentEventLevelId) { await _highJumpService.RecordAttemptAsync(dto); return RedirectToAction(nameof(Index), new { tournamentEventLevelId }); } [HttpPost] [ValidateAntiForgeryToken] public async Task CalculateResults(int tournamentEventLevelId) { var recordedBy = User.Identity?.Name ?? "Unknown"; await _highJumpService.CalculateResultsAsync(tournamentEventLevelId, recordedBy); return RedirectToAction(nameof(Index), new { tournamentEventLevelId }); } }