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

namespace ERPCore.Controllers;

/// <summary>
/// Item type master endpoints (docs/11-BACKEND-PHASE1.md §2.7) — the Color/Size/Material
/// dimension names. <c>GET</c> is the reason this master exists: it populates the item
/// builder's dropdown. Items never reference an item type; the chosen values are encoded
/// into the client-generated SKU (docs/10 Part C.9).
/// </summary>
[Route("api/v1/item-types")]
public sealed class ItemTypesController : ApiControllerBase
{
    private readonly IItemTypeService _itemTypes;

    public ItemTypesController(IItemTypeService itemTypes) => _itemTypes = itemTypes;

    /// <summary>Feeds the frontend item-builder dropdown; filter <c>status=Active</c> for selectable rows.</summary>
    [HttpGet]
    [ProducesResponseType(typeof(PagedResponse<ItemTypeDto>), StatusCodes.Status200OK)]
    public async Task<ActionResult<PagedResponse<ItemTypeDto>>> List(
        [FromQuery] PageQuery query, [FromQuery] EntityStatus? status, CancellationToken ct)
        => Ok(await _itemTypes.ListAsync(query, status, ct));

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

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

    [HttpPost]
    [ProducesResponseType(typeof(ItemTypeDto), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status409Conflict)]
    public async Task<ActionResult<ItemTypeDto>> Create([FromBody] CreateItemTypeRequest request, CancellationToken ct)
    {
        var result = await _itemTypes.CreateAsync(request, ct);
        SetETag(result.RowVersion);
        return Created($"/api/v1/item-types/{result.Value.ItemTypeId}", result.Value);
    }

    [HttpPut("{itemTypeId:int}")]
    [ProducesResponseType(typeof(ItemTypeDto), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    [ProducesResponseType(StatusCodes.Status409Conflict)]
    [ProducesResponseType(StatusCodes.Status412PreconditionFailed)]
    public async Task<ActionResult<ItemTypeDto>> Update(int itemTypeId, [FromBody] UpdateItemTypeRequest request, CancellationToken ct)
    {
        var expected = RequireIfMatch();
        var result = await _itemTypes.UpdateAsync(itemTypeId, request, expected, ct);
        SetETag(result.RowVersion);
        return Ok(result.Value);
    }

    /// <summary>Deactivate/reactivate. Masters are never hard-deleted (FR-MD-08).</summary>
    [HttpPatch("{itemTypeId:int}/status")]
    [ProducesResponseType(StatusCodes.Status204NoContent)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<IActionResult> SetStatus(int itemTypeId, [FromBody] UpdateItemTypeStatusRequest request, CancellationToken ct)
    {
        await _itemTypes.SetStatusAsync(itemTypeId, request.Status, ct);
        return NoContent();
    }
}
