using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using PlotLine.Services;
using PlotLine.ViewModels;

namespace PlotLine.Controllers;

[Authorize]
public sealed class SceneMetricsController(ISceneMetricValueService sceneMetrics) : Controller
{
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> UpdateSceneMetricValue([FromBody] SceneMetricValueUpdateViewModel? model)
    {
        if (model is null || model.SceneID <= 0 || (model.MetricTypeID <= 0 && model.MetricID <= 0))
        {
            return BadRequest(new { success = false, message = "Missing scene or metric details." });
        }

        try
        {
            var value = await sceneMetrics.UpdateSceneMetricValueAsync(model);
            return Json(new { success = true, value });
        }
        catch (InvalidOperationException ex)
        {
            return BadRequest(new { success = false, message = ex.Message });
        }
    }
}
