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

namespace ERPCore.Controllers;

/// <summary>Production template endpoints (docs/30-BACKEND-PHASE2.md §D.1).</summary>
[Route("api/v1/production-templates")]
public sealed class ProductionTemplatesController : ApiControllerBase
{
    private readonly IProductionTemplateService _templates;

    public ProductionTemplatesController(IProductionTemplateService templates) => _templates = templates;

    [HttpGet]
    [ProducesResponseType(typeof(PagedResponse<TemplateSummaryDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<PagedResponse<TemplateSummaryDto>>> List(
        [FromQuery] PageQuery query, [FromQuery] EntityStatus? status, CancellationToken ct)
        => Ok(await _templates.ListAsync(query, status, ct));

    [HttpGet("{templateId:int}")]
    [ProducesResponseType(typeof(TemplateGraphDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<TemplateGraphDto>> GetById(int templateId, CancellationToken ct)
    {
        var result = await _templates.GetAsync(templateId, ct);
        if (result is null) return NotFound();

        SetETag(result.RowVersion);
        return Ok(result.Value);
    }

    [HttpPost]
    [ProducesResponseType(typeof(TemplateGraphDto), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status409Conflict)]
    [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
    public async Task<ActionResult<TemplateGraphDto>> Create(
        [FromBody] SaveTemplateRequest request, CancellationToken ct)
    {
        var result = await _templates.CreateAsync(request, ct);

        SetETag(result.RowVersion);
        return Created($"/api/v1/production-templates/{result.Value.TemplateId}", result.Value);
    }

    [HttpPut("{templateId:int}")]
    [ProducesResponseType(typeof(TemplateGraphDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status409Conflict)]
    [ProducesResponseType(StatusCodes.Status412PreconditionFailed)]
    [ProducesResponseType(StatusCodes.Status422UnprocessableEntity)]
    public async Task<ActionResult<TemplateGraphDto>> Update(
        int templateId, [FromBody] SaveTemplateRequest request, CancellationToken ct)
    {
        var expected = RequireIfMatch();
        var result = await _templates.UpdateAsync(templateId, request, expected, ct);

        SetETag(result.RowVersion);
        return Ok(result.Value);
    }

    [HttpPatch("{templateId:int}/status")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> SetStatus(
        int templateId, [FromBody] UpdateTemplateStatusRequest request, CancellationToken ct)
    {
        await _templates.SetStatusAsync(templateId, request.Status, ct);
        return NoContent();
    }
}
