using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SportsDivision.Application.DTOs;
using SportsDivision.Application.Interfaces;

namespace SportsDivision.Web.Controllers;

[Authorize(Roles = "Admin")]
public class ScoringConfigController : Controller
{
    private readonly IScoringService _scoringService;

    public ScoringConfigController(IScoringService scoringService)
    {
        _scoringService = scoringService;
    }

    [HttpGet]
    public async Task<IActionResult> Index()
    {
        var constants = await _scoringService.GetScoringConstantsAsync();
        var placementConfigs = await _scoringService.GetPlacementPointConfigsAsync();
        ViewBag.ScoringConstants = constants;
        ViewBag.PlacementPointConfigs = placementConfigs;
        return View(constants);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> UpdateScoringConstant(ScoringConstantUpdateDto dto)
    {
        if (!ModelState.IsValid)
        {
            return RedirectToAction(nameof(Index));
        }

        await _scoringService.UpdateScoringConstantAsync(dto);
        return RedirectToAction(nameof(Index));
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> UpdatePlacementPointConfig(PlacementPointConfigDto dto)
    {
        if (!ModelState.IsValid)
        {
            return RedirectToAction(nameof(Index));
        }

        await _scoringService.UpdatePlacementPointConfigAsync(dto);
        return RedirectToAction(nameof(Index));
    }
}
