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 EventController : Controller
{
    private readonly IEventService _eventService;

    public EventController(IEventService eventService)
    {
        _eventService = eventService;
    }

    public async Task<IActionResult> Index(EventCategory? category)
    {
        IEnumerable<EventDto> events;

        if (category.HasValue)
        {
            events = await _eventService.GetByCategoryAsync(category.Value);
        }
        else
        {
            events = await _eventService.GetAllAsync();
        }

        ViewBag.SelectedCategory = category;

        return View(events);
    }

    [HttpGet]
    public IActionResult Create()
    {
        return View(new EventCreateDto());
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Create(EventCreateDto dto)
    {
        if (!ModelState.IsValid)
        {
            return View(dto);
        }

        try
        {
            await _eventService.CreateAsync(dto);
            TempData["SuccessMessage"] = "Event created successfully.";
            return RedirectToAction(nameof(Index));
        }
        catch (InvalidOperationException ex)
        {
            TempData["ErrorMessage"] = ex.Message;
            return View(dto);
        }
    }

    [HttpGet]
    public async Task<IActionResult> Edit(int id)
    {
        try
        {
            var ev = await _eventService.GetByIdAsync(id);
            if (ev == null)
            {
                return NotFound();
            }

            var dto = new EventUpdateDto
            {
                EventId = ev.EventId,
                Name = ev.Name,
                Category = ev.Category,
                IsRelay = ev.IsRelay,
                PrimarySchool = ev.PrimarySchool,
                SecondarySchool = ev.SecondarySchool,
                IsActive = ev.IsActive,
                EventLevelIds = ev.EventLevels.Select(el => el.EventLevelId).ToList()
            };

            return View(dto);
        }
        catch (KeyNotFoundException)
        {
            return NotFound();
        }
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Edit(EventUpdateDto dto)
    {
        if (!ModelState.IsValid)
        {
            return View(dto);
        }

        try
        {
            await _eventService.UpdateAsync(dto);
            TempData["SuccessMessage"] = "Event updated successfully.";
            return RedirectToAction(nameof(Index));
        }
        catch (KeyNotFoundException)
        {
            return NotFound();
        }
        catch (InvalidOperationException ex)
        {
            TempData["ErrorMessage"] = ex.Message;
            return View(dto);
        }
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Delete(int id)
    {
        try
        {
            await _eventService.DeleteAsync(id);
            TempData["SuccessMessage"] = "Event deleted successfully.";
        }
        catch (KeyNotFoundException)
        {
            return NotFound();
        }
        catch (InvalidOperationException ex)
        {
            TempData["ErrorMessage"] = ex.Message;
        }

        return RedirectToAction(nameof(Index));
    }
}
