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

namespace PlotLine.Controllers;

[ApiController]
[Authorize]
[Route("api/word-companion")]
public sealed class WordCompanionController(IWordCompanionService wordCompanion) : ControllerBase
{
    [HttpGet("projects")]
    public Task<WordCompanionProjectsResponse> GetProjects() => wordCompanion.ListProjectsAsync();

    [HttpGet("projects/{projectId:int}/books")]
    public async Task<IActionResult> GetBooks(int projectId)
    {
        var response = await wordCompanion.ListBooksAsync(projectId);
        return response is null ? BadRequest() : Ok(response);
    }

    [HttpGet("books/{bookId:int}/structure")]
    public async Task<IActionResult> GetBookStructure(int bookId)
    {
        var response = await wordCompanion.GetBookStructureAsync(bookId);
        return response is null ? NotFound() : Ok(response);
    }

    [HttpGet("books/{bookId:int}/chapters")]
    public async Task<IActionResult> GetChapters(int bookId)
    {
        var response = await wordCompanion.ListChaptersAsync(bookId);
        return response is null ? BadRequest() : Ok(response);
    }

    [HttpPost("books/{bookId:int}/resolve-scene")]
    public async Task<IActionResult> ResolveScene(int bookId, WordCompanionResolveSceneRequest request)
    {
        var response = await wordCompanion.ResolveSceneAsync(bookId, request);
        if (response is null)
        {
            return BadRequest();
        }

        return response.BookAccessible ? Ok(response) : NotFound(response);
    }

    [HttpGet("scenes/{sceneId:int}/companion")]
    public async Task<IActionResult> GetSceneCompanion(int sceneId)
    {
        var response = await wordCompanion.GetSceneCompanionAsync(sceneId);
        return response is null ? NotFound() : Ok(response);
    }

    [HttpPost("books/{bookId:int}/chapters")]
    public async Task<IActionResult> CreateChapter(int bookId, WordCompanionCreateChapterRequest request)
    {
        var response = await wordCompanion.CreateChapterAsync(bookId, request);
        return response is null ? BadRequest() : Ok(response);
    }

    [HttpPost("books/{bookId:int}/chapters/{chapterId:int}/scenes")]
    public async Task<IActionResult> CreateScene(int bookId, int chapterId, WordCompanionCreateSceneRequest request)
    {
        var response = await wordCompanion.CreateSceneAsync(bookId, chapterId, request);
        return response is null ? BadRequest() : Ok(response);
    }

    [HttpPost("books/{bookId:int}/sync-scene")]
    public async Task<IActionResult> SyncScene(int bookId, WordCompanionSyncSceneRequest request)
    {
        var response = await wordCompanion.SyncSceneAsync(bookId, request);
        return response is null ? BadRequest() : Ok(response);
    }

    [HttpPost("books/{bookId:int}/sync-manuscript")]
    public async Task<IActionResult> SyncManuscript(int bookId, WordCompanionSyncManuscriptRequest request)
    {
        var response = await wordCompanion.SyncManuscriptAsync(bookId, request);
        return response is null ? BadRequest() : Ok(response);
    }

    [HttpPut("scenes/{sceneId:int}/links")]
    public async Task<IActionResult> UpdateSceneLinks(int sceneId, WordCompanionUpdateSceneLinksRequest request)
    {
        var response = await wordCompanion.UpdateSceneLinksAsync(sceneId, request);
        return response is null ? BadRequest() : Ok(response);
    }

    [HttpPatch("scenes/{sceneId:int}/word-count")]
    public async Task<IActionResult> UpdateWordCount(int sceneId, WordCompanionUpdateWordCountRequest request)
    {
        var response = await wordCompanion.UpdateWordCountAsync(sceneId, request);
        return response is null ? BadRequest() : Ok(response);
    }
}
