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

namespace PlotLine.Controllers;

public sealed class WarningsController(IContinuityValidationService validation) : Controller
{
    public async Task<IActionResult> Index(
        int projectId,
        int? bookId,
        int? sceneId,
        int? warningTypeId,
        int? warningSeverityId,
        string? entityType,
        bool includeDismissed = false,
        bool includeIntentional = false)
    {
        var model = await validation.GetDashboardAsync(projectId, bookId, sceneId, warningTypeId, warningSeverityId, entityType, includeDismissed, includeIntentional);
        return model is null ? NotFound() : View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> ValidateProject(int projectId, int? bookId)
    {
        var summary = await validation.ValidateProjectAsync(projectId);
        var model = await validation.GetDashboardAsync(projectId, bookId, null, null, null, null, false, false, summary);
        return model is null ? NotFound() : View("Index", model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> ValidateBook(int projectId, int bookId)
    {
        var summary = await validation.ValidateBookAsync(bookId);
        var model = await validation.GetDashboardAsync(projectId, bookId, null, null, null, null, false, false, summary);
        return model is null ? NotFound() : View("Index", model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> ValidateScene(int projectId, int? bookId, int sceneId, bool returnToTimeline = false)
    {
        await validation.ValidateSceneAsync(sceneId);
        if (returnToTimeline)
        {
            return RedirectToAction("Index", "Timeline", new { projectId, bookId, selectedSceneId = sceneId });
        }

        return RedirectToAction(nameof(Index), new { projectId, bookId, sceneId });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Dismiss(int id, int projectId, int? bookId, int? sceneId, bool returnToTimeline = false)
    {
        await validation.DismissAsync(id);
        return RedirectAfterAction(projectId, bookId, sceneId, returnToTimeline);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> MarkIntentional(int id, int projectId, int? bookId, int? sceneId, bool returnToTimeline = false)
    {
        await validation.MarkIntentionalAsync(id);
        return RedirectAfterAction(projectId, bookId, sceneId, returnToTimeline);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Restore(int id, int projectId, int? bookId, int? sceneId)
    {
        await validation.RestoreAsync(id);
        return RedirectToAction(nameof(Index), new { projectId, bookId, sceneId, includeDismissed = true, includeIntentional = true });
    }

    private IActionResult RedirectAfterAction(int projectId, int? bookId, int? sceneId, bool returnToTimeline)
    {
        if (returnToTimeline && sceneId.HasValue)
        {
            return RedirectToAction("Index", "Timeline", new { projectId, bookId, selectedSceneId = sceneId.Value });
        }

        return RedirectToAction(nameof(Index), new { projectId, bookId, sceneId });
    }
}
