using FlatRender.ContentSvc.Application.Services;
using FlatRender.ContentSvc.Models.Requests;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace FlatRender.ContentSvc.Controllers;

[ApiController]
[Route("v1")]
public class TaxonomyController(TaxonomyService svc) : ControllerBase
{
    // ── Categories ────────────────────────────────────────────────────────────

    [HttpGet("categories")]
    public async Task<IActionResult> GetCategories() =>
        Ok(await svc.GetCategoryTreeAsync());

    [Authorize(Roles = "Admin")]
    [HttpPost("categories")]
    public async Task<IActionResult> CreateCategory([FromBody] CreateCategoryRequest req) =>
        Ok(await svc.CreateCategoryAsync(req));

    [Authorize(Roles = "Admin")]
    [HttpPut("categories/{id:guid}")]
    public async Task<IActionResult> UpdateCategory(Guid id, [FromBody] UpdateCategoryRequest req) =>
        Ok(await svc.UpdateCategoryAsync(id, req));

    [Authorize(Roles = "Admin")]
    [HttpDelete("categories/{id:guid}")]
    public async Task<IActionResult> DeleteCategory(Guid id)
    {
        await svc.DeleteCategoryAsync(id);
        return NoContent();
    }

    // ── Tags ──────────────────────────────────────────────────────────────────

    [HttpGet("tags")]
    public async Task<IActionResult> GetTags(
        [FromQuery] int page = 1, [FromQuery] int pageSize = 50,
        [FromQuery] string? search = null) =>
        Ok(await svc.GetTagsAsync(page, pageSize, search));

    [Authorize(Roles = "Admin")]
    [HttpPost("tags")]
    public async Task<IActionResult> CreateTag([FromBody] CreateTagRequest req) =>
        Ok(await svc.CreateTagAsync(req));

    [Authorize(Roles = "Admin")]
    [HttpPut("tags/{id:guid}")]
    public async Task<IActionResult> UpdateTag(Guid id, [FromBody] UpdateTagRequest req) =>
        Ok(await svc.UpdateTagAsync(id, req));

    [Authorize(Roles = "Admin")]
    [HttpDelete("tags/{id:guid}")]
    public async Task<IActionResult> DeleteTag(Guid id)
    {
        await svc.DeleteTagAsync(id);
        return NoContent();
    }

    // ── Fonts ─────────────────────────────────────────────────────────────────

    [HttpGet("fonts")]
    public async Task<IActionResult> GetFonts(
        [FromQuery] int page = 1, [FromQuery] int pageSize = 50,
        [FromQuery] string? search = null, [FromQuery] string? direction = null) =>
        Ok(await svc.GetFontsAsync(page, pageSize, search, direction));

    [Authorize(Roles = "Admin")]
    [HttpPost("fonts")]
    public async Task<IActionResult> CreateFont([FromBody] CreateFontRequest req) =>
        Ok(await svc.CreateFontAsync(req));

    [Authorize(Roles = "Admin")]
    [HttpPut("fonts/{id:guid}")]
    public async Task<IActionResult> UpdateFont(Guid id, [FromBody] UpdateFontRequest req) =>
        Ok(await svc.UpdateFontAsync(id, req));

    [Authorize(Roles = "Admin")]
    [HttpDelete("fonts/{id:guid}")]
    public async Task<IActionResult> DeleteFont(Guid id)
    {
        await svc.DeleteFontAsync(id);
        return NoContent();
    }

    // ── Music Tracks ──────────────────────────────────────────────────────────

    [HttpGet("music")]
    public async Task<IActionResult> GetMusicTracks(
        [FromQuery] int page = 1, [FromQuery] int pageSize = 50,
        [FromQuery] string? search = null) =>
        Ok(await svc.GetMusicTracksAsync(page, pageSize, search));

    [Authorize(Roles = "Admin")]
    [HttpPost("music")]
    public async Task<IActionResult> CreateMusicTrack([FromBody] CreateMusicTrackRequest req) =>
        Ok(await svc.CreateMusicTrackAsync(req));

    [Authorize(Roles = "Admin")]
    [HttpDelete("music/{id:guid}")]
    public async Task<IActionResult> DeleteMusicTrack(Guid id)
    {
        await svc.DeleteMusicTrackAsync(id);
        return NoContent();
    }
}
