using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Nuuru.Server.Auth; using Nuuru.Server.DTOs.Admin; using Nuuru.Server.Models; using Nuuru.Server.Services; namespace Nuuru.Server.Controllers { [ApiController] [Route("api/booru/tag-categories")] public class TagCategoryController : ControllerBase { private readonly ITagCategoryService _categoryService; private readonly ILogger _logger; public TagCategoryController( ITagCategoryService categoryService, ILogger logger) { _categoryService = categoryService; _logger = logger; } /// /// Get all tag categories /// [HttpGet] [AllowAnonymous] public async Task GetCategories() { try { var categories = await _categoryService.GetAllCategoriesAsync(); return Ok(categories); } catch (Exception ex) { _logger.LogError(ex, "Error retrieving tag categories"); return StatusCode(500, new { error = "Failed to retrieve tag categories" }); } } /// /// Get a tag category by ID /// [HttpGet("{id:guid}")] [AllowAnonymous] public async Task GetCategory(Guid id) { try { var category = await _categoryService.GetCategoryByIdAsync(id); if (category == null) { return NotFound(new { error = "Tag category not found" }); } return Ok(category); } catch (Exception ex) { _logger.LogError(ex, "Error retrieving tag category {Id}", id); return StatusCode(500, new { error = "Failed to retrieve tag category" }); } } /// /// Get a tag category by slug /// [HttpGet("slug/{slug}")] [AllowAnonymous] public async Task GetCategoryBySlug(string slug) { try { var category = await _categoryService.GetCategoryBySlugAsync(slug); if (category == null) { return NotFound(new { error = "Tag category not found" }); } return Ok(category); } catch (Exception ex) { _logger.LogError(ex, "Error retrieving tag category by slug {Slug}", slug); return StatusCode(500, new { error = "Failed to retrieve tag category" }); } } /// /// Create a new tag category /// [HttpPost] [Authorize(Policy = Permissions.Admin.SystemSettings)] public async Task CreateCategory([FromBody] CreateTagCategoryRequest request) { try { var category = await _categoryService.CreateCategoryAsync(request); // Set target for audit log HttpContext.Items[AuditLog.TargetIdKey] = category.Id.ToString(); HttpContext.Items[AuditLog.TargetTypeKey] = "TagCategory"; return CreatedAtAction(nameof(GetCategory), new { id = category.Id }, category); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error creating tag category"); return StatusCode(500, new { error = "Failed to create tag category" }); } } /// /// Update an existing tag category /// [HttpPut("{id:guid}")] [Authorize(Policy = Permissions.Admin.SystemSettings)] public async Task UpdateCategory(Guid id, [FromBody] UpdateTagCategoryRequest request) { try { var category = await _categoryService.UpdateCategoryAsync(id, request); if (category == null) { return NotFound(new { error = "Tag category not found" }); } // Set target for audit log HttpContext.Items[AuditLog.TargetIdKey] = id.ToString(); HttpContext.Items[AuditLog.TargetTypeKey] = "TagCategory"; return Ok(category); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error updating tag category {Id}", id); return StatusCode(500, new { error = "Failed to update tag category" }); } } /// /// Delete a tag category (only if it has no tags) /// [HttpDelete("{id:guid}")] [Authorize(Policy = Permissions.Admin.SystemSettings)] public async Task DeleteCategory(Guid id) { try { var result = await _categoryService.DeleteCategoryAsync(id); if (!result) { return NotFound(new { error = "Tag category not found" }); } // Set target for audit log HttpContext.Items[AuditLog.TargetIdKey] = id.ToString(); HttpContext.Items[AuditLog.TargetTypeKey] = "TagCategory"; return Ok(new { message = "Tag category deleted successfully" }); } catch (InvalidOperationException ex) { return BadRequest(new { error = ex.Message }); } catch (Exception ex) { _logger.LogError(ex, "Error deleting tag category {Id}", id); return StatusCode(500, new { error = "Failed to delete tag category" }); } } } }