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<IActionResult> 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<IActionResult> 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<IActionResult> RemoveHeight(int heightId, int tournamentEventLevelId)
    {
        await _highJumpService.RemoveHeightAsync(heightId);
        return RedirectToAction(nameof(Index), new { tournamentEventLevelId });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> RecordAttempt(HighJumpAttemptUpdateDto dto, int tournamentEventLevelId)
    {
        await _highJumpService.RecordAttemptAsync(dto);
        return RedirectToAction(nameof(Index), new { tournamentEventLevelId });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CalculateResults(int tournamentEventLevelId)
    {
        var recordedBy = User.Identity?.Name ?? "Unknown";
        var warning = await _highJumpService.CalculateResultsAsync(tournamentEventLevelId, recordedBy);
        if (warning != null)
            TempData["WarningMessage"] = warning;
        else
            TempData["SuccessMessage"] = "High jump results calculated.";
        return RedirectToAction(nameof(Index), new { tournamentEventLevelId });
    }
}
