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

namespace PlotLine.Controllers;

[Authorize]
public sealed class ProjectsController(IProjectService projects, IProjectActivityService activity) : Controller
{
    public async Task<IActionResult> Index() => View(await projects.ListAsync());

    public async Task<IActionResult> Details(int id)
    {
        var model = await projects.GetDetailAsync(id);
        return model is null ? NotFound() : View(model);
    }

    public async Task<IActionResult> Create() => View("Edit", await projects.GetCreateAsync());

    public async Task<IActionResult> Activity(int id)
    {
        var model = await activity.GetActivityAsync(id);
        return model is null ? NotFound() : View(model);
    }

    public async Task<IActionResult> Edit(int id)
    {
        var model = await projects.GetEditAsync(id);
        return model is null ? NotFound() : View(model);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Save(ProjectEditViewModel model)
    {
        if (!ModelState.IsValid)
        {
            if (model.ProjectID == 0)
            {
                await projects.PopulateGenrePresetOptionsAsync(model);
            }

            return View("Edit", model);
        }

        var projectId = await projects.SaveAsync(model);
        if (projectId == 0)
        {
            return NotFound();
        }

        return RedirectToAction(nameof(Details), new { id = projectId });
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Archive(int id)
    {
        var archived = await projects.ArchiveAsync(id);
        if (!archived)
        {
            return NotFound();
        }

        TempData["ArchiveMessage"] = "Project archived. You can restore it from Archived Projects.";
        return RedirectToAction(nameof(Index));
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> Restore(int id)
    {
        var restored = await projects.RestoreAsync(id);
        if (!restored)
        {
            return NotFound();
        }

        TempData["ArchiveMessage"] = "Project restored.";
        return RedirectToAction(nameof(Index));
    }

    [HttpGet]
    public IActionResult HardDeleteArchivedProject()
    {
        TempData["ArchiveError"] = "The project could not be deleted. Please try again.";
        return RedirectToAction(nameof(Index));
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> HardDeleteArchivedProject(int projectId, string? confirmationName)
    {
        var project = await projects.GetArchivedOwnerProjectAsync(projectId);
        if (project is null)
        {
            TempData["ArchiveError"] = "Only an archived project owner can permanently delete a project.";
            return RedirectToAction(nameof(Index));
        }

        if (!string.Equals(confirmationName?.Trim(), project.ProjectName, StringComparison.Ordinal))
        {
            TempData["ArchiveError"] = "Type the project name exactly to permanently delete it.";
            return RedirectToAction(nameof(Index));
        }

        var result = await projects.HardDeleteAsync(projectId);
        if (result.Succeeded)
        {
            TempData["ArchiveMessage"] = "Project permanently deleted.";
        }
        else
        {
            TempData["ArchiveError"] = result.NotArchived
                ? "Only archived projects can be permanently deleted."
                : result.Message;
        }

        return RedirectToAction(nameof(Index));
    }
}
