using ERPCore.Domain.Enums;
using ERPCore.Dtos.Common;
using ERPCore.Dtos.Reference;
using ERPCore.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace ERPCore.Controllers;

/// <summary>Reason-code reference endpoints (docs/11 §6).</summary>
[Route("api/v1/reason-codes")]
public sealed class ReasonCodesController : ApiControllerBase
{
    private readonly IReasonCodeService _codes;

    public ReasonCodesController(IReasonCodeService codes) => _codes = codes;

    [HttpGet]
    [ProducesResponseType(typeof(PagedResponse<ReasonCodeDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<PagedResponse<ReasonCodeDto>>> List(
        [FromQuery] ReasonContext? context, [FromQuery] PageQuery query, CancellationToken ct)
        => Ok(await _codes.ListAsync(context, query, ct));

    [HttpPost]
    [ProducesResponseType(typeof(ReasonCodeDto), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status409Conflict)]
    public async Task<ActionResult<ReasonCodeDto>> Create([FromBody] CreateReasonCodeRequest request, CancellationToken ct)
    {
        var dto = await _codes.CreateAsync(request, ct);
        return Created($"/api/v1/reason-codes/{dto.ReasonCodeId}", dto);
    }
}
