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

namespace PlotLine.Controllers;

[Authorize]
public sealed class ExportsController(IExportService exports, IProjectExportService projectExports, ILogger<ExportsController> logger) : Controller
{
    public async Task<IActionResult> StoryBible(int projectId, string format = "html") =>
        ExportResult(await exports.ExportStoryBibleAsync(projectId, format), format);

    public async Task<IActionResult> NarrativeTimeline(int projectId, string format = "html") =>
        ExportResult(await exports.ExportNarrativeTimelineAsync(projectId, format), format);

    public async Task<IActionResult> Character(int id, string format = "html") =>
        ExportResult(await exports.ExportCharacterReferenceAsync(id, format), format);

    public async Task<IActionResult> SceneBrief(int id, string format = "html") =>
        ExportResult(await exports.ExportSceneBriefAsync(id, format), format);

    public async Task<IActionResult> ChapterBrief(int id, string format = "html") =>
        ExportResult(await exports.ExportChapterBriefAsync(id, format), format);

    public async Task<IActionResult> WritingSession(int sceneId, string format = "html") =>
        ExportResult(await exports.ExportWriterSessionBriefAsync(sceneId, format), format);

    public async Task<IActionResult> ResearchPack(int projectId, string format = "html") =>
        ExportResult(await exports.ExportResearchPackAsync(projectId, format), format);

    public async Task<IActionResult> Search(int projectId, string q, string format = "html") =>
        ExportResult(await exports.ExportSearchResultsAsync(projectId, q, format), format);

    public async Task<IActionResult> ProjectBackup(int projectId)
    {
        try
        {
            var export = await projectExports.ExportAsync(projectId);
            if (export is null)
            {
                TempData["ProjectExportMessage"] = "Project backup could not be created.";
                return RedirectToAction("Index", "Projects");
            }

            return File(System.Text.Encoding.UTF8.GetBytes(export.Json), export.ContentType, export.FileName);
        }
        catch (Exception ex)
        {
            logger.LogError(ex, "Project backup export failed for project {ProjectID}.", projectId);
            TempData["ProjectExportMessage"] = "Project backup could not be created. Please try again, or check the application logs if the problem continues.";
            return RedirectToAction("Details", "Projects", new { id = projectId });
        }
    }

    private IActionResult ExportResult((string FileName, string ContentType, string Content) export, string format)
    {
        if (string.Equals(format, "html", StringComparison.OrdinalIgnoreCase))
        {
            return Content(export.Content, export.ContentType);
        }

        return File(System.Text.Encoding.UTF8.GetBytes(export.Content), export.ContentType, export.FileName);
    }
}
