using ERPCore.Dtos.Procurement;
using ERPCore.Services.Interfaces;
using Microsoft.AspNetCore.Mvc;

namespace ERPCore.Controllers;

/// <summary>Purchase-return endpoints (docs/11 §3.4).</summary>
[Route("api/v1/purchase-returns")]
public sealed class PurchaseReturnsController : ApiControllerBase
{
    private readonly IPurchaseReturnService _returns;

    public PurchaseReturnsController(IPurchaseReturnService returns) => _returns = returns;

    /// <summary>Create + auto-post a return (outbound movement). 409 if return exceeds available stock.</summary>
    [HttpPost]
    [ProducesResponseType(typeof(PurchaseReturnDto), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    [ProducesResponseType(StatusCodes.Status409Conflict)]
    [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
    public async Task<ActionResult<PurchaseReturnDto>> Create([FromBody] CreatePurchaseReturnRequest request, CancellationToken ct)
    {
        var dto = await _returns.CreateAsync(request, ct);
        return Created($"/api/v1/purchase-returns/{dto.ReturnId}", dto);
    }
}
