using MediatR;
using Microsoft.AspNetCore.Mvc;
using SmartStack.Api.Authorization;
{{#if navRoute}}
using SmartStack.Api.Routing;
{{/if}}
using SmartStack.Application.Common.Authorization;

namespace {{namespace}};

/// <summary>
/// API controller for {{name}} operations.
/// </summary>
[ApiController]
{{#if navRoute}}
[NavRoute("{{navRoute}}"{{#if navRouteSuffix}}, Suffix = "{{navRouteSuffix}}"{{/if}}{{#if customSegment}}, CustomSegment = "{{customSegment}}"{{/if}})]
{{else}}
[Route("api/[controller]")]
{{/if}}
[Microsoft.AspNetCore.Authorization.Authorize]
[Produces("application/json")]
[Tags("{{namePlural}}")]
public class {{name}}Controller : ControllerBase
{
    private readonly ISender _mediator;

    public {{name}}Controller(ISender mediator) => _mediator = mediator;

    /// <summary>
    /// Get all {{namePlural}} with pagination
    /// </summary>
    [HttpGet]
{{#if navRoute}}
    [RequirePermission(Permissions.{{module}}.{{namePlural}}.View)]
{{/if}}
    [ProducesResponseType(typeof(List<{{name}}ListDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status403Forbidden)]
    public async Task<ActionResult<List<{{name}}ListDto>>> GetAll(CancellationToken ct)
    {
        var result = await _mediator.Send(new Get{{namePlural}}Query(), ct);
        return Ok(result);
    }

    /// <summary>
    /// Get {{nameCamel}} by ID
    /// </summary>
    [HttpGet("{id:guid}")]
{{#if navRoute}}
    [RequirePermission(Permissions.{{module}}.{{namePlural}}.View)]
{{/if}}
    [ProducesResponseType(typeof({{name}}DetailDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status403Forbidden)]
    public async Task<ActionResult<{{name}}DetailDto>> GetById(
        Guid id, CancellationToken ct)
    {
        var result = await _mediator.Send(new Get{{name}}ByIdQuery(id), ct);
        if (result == null) return NotFound(new { message = "{{name}} not found" });
        return Ok(result);
    }

    /// <summary>
    /// Create new {{nameCamel}}
    /// </summary>
    [HttpPost]
{{#if navRoute}}
    [RequirePermission(Permissions.{{module}}.{{namePlural}}.Create)]
{{/if}}
    [ProducesResponseType(typeof({{name}}DetailDto), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status403Forbidden)]
    public async Task<ActionResult<{{name}}DetailDto>> Create(
        [FromBody] Create{{name}}Request request, CancellationToken ct)
    {
        var result = await _mediator.Send(new Create{{name}}Command(request), ct);
        return CreatedAtAction(nameof(GetById), new { id = result.Id }, result);
    }

    /// <summary>
    /// Update {{nameCamel}}
    /// </summary>
    [HttpPut("{id:guid}")]
{{#if navRoute}}
    [RequirePermission(Permissions.{{module}}.{{namePlural}}.Update)]
{{/if}}
    [ProducesResponseType(typeof({{name}}DetailDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status403Forbidden)]
    public async Task<ActionResult<{{name}}DetailDto>> Update(
        Guid id, [FromBody] Update{{name}}Request request, CancellationToken ct)
    {
        var result = await _mediator.Send(new Update{{name}}Command(id, request), ct);
        if (result.IsNotFound) return NotFound(new { message = "{{name}} not found" });
        return Ok(result.Data);
    }

    /// <summary>
    /// Delete {{nameCamel}}
    /// </summary>
    [HttpDelete("{id:guid}")]
{{#if navRoute}}
    [RequirePermission(Permissions.{{module}}.{{namePlural}}.Delete)]
{{/if}}
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status401Unauthorized)]
    [ProducesResponseType(StatusCodes.Status403Forbidden)]
    public async Task<IActionResult> Delete(Guid id, CancellationToken ct)
    {
        var success = await _mediator.Send(new Delete{{name}}Command(id), ct);
        return success ? NoContent() : NotFound(new { message = "{{name}} not found" });
    }
}
