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 TrackEventController : Controller
{
    private readonly IHeatManagementService _heatManagementService;
    private readonly ITournamentService _tournamentService;
    private readonly IScoringService _scoringService;

    public TrackEventController(
        IHeatManagementService heatManagementService,
        ITournamentService tournamentService,
        IScoringService scoringService)
    {
        _heatManagementService = heatManagementService;
        _tournamentService = tournamentService;
        _scoringService = scoringService;
    }

    [HttpGet]
    public async Task<IActionResult> Index(int tournamentEventLevelId)
    {
        var rounds = await _heatManagementService.GetRoundsAsync(tournamentEventLevelId);
        ViewBag.TournamentEventLevelId = tournamentEventLevelId;
        return View(rounds);
    }

    [HttpGet]
    public async Task<IActionResult> ManageRound(int roundId)
    {
        var round = await _heatManagementService.GetRoundWithHeatsAsync(roundId);
        if (round == null)
        {
            return NotFound();
        }

        return View(round);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CreateRound(RoundCreateDto dto)
    {
        if (!ModelState.IsValid)
        {
            return RedirectToAction(nameof(Index), new { tournamentEventLevelId = dto.TournamentEventLevelId });
        }

        await _heatManagementService.CreateRoundAsync(dto);
        return RedirectToAction(nameof(Index), new { tournamentEventLevelId = dto.TournamentEventLevelId });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> SeedHeats(int roundId, SeedingMethod method, int lanesPerHeat = 8)
    {
        await _heatManagementService.SeedHeatsAsync(roundId, method, lanesPerHeat);
        return RedirectToAction(nameof(ManageRound), new { roundId });
    }

    [HttpPost]
    public async Task<IActionResult> SaveTimes(int heatId, [FromBody] List<HeatLaneUpdateDto> lanes)
    {
        var recordedBy = User.Identity?.Name ?? "Unknown";
        await _heatManagementService.SaveHeatTimesAsync(heatId, lanes, recordedBy);
        return Json(new { success = true });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CalculateAdvancement(int roundId)
    {
        await _heatManagementService.CalculateAdvancementAsync(roundId);
        return RedirectToAction(nameof(ManageRound), new { roundId });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> PopulateNextRound(int roundId)
    {
        await _heatManagementService.PopulateNextRoundAsync(roundId);
        return RedirectToAction(nameof(ManageRound), new { roundId });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CompleteHeat(int heatId)
    {
        await _heatManagementService.CompleteHeatAsync(heatId);
        return RedirectToAction(nameof(ManageRound), new { roundId = heatId });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> CompleteRound(int roundId)
    {
        await _heatManagementService.CompleteRoundAsync(roundId);
        return RedirectToAction(nameof(ManageRound), new { roundId });
    }
}
