using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SolutionErp.Application.Master.Catalogs;

namespace SolutionErp.Api.Controllers;

// Master catalogs API — 4 sub-resource (units, materials, services, work-items)
// dùng Mediator dispatch theo command/query type. Authorize = đăng nhập đủ
// (mọi role được đọc cho autocomplete; ghi cần Admin).

[ApiController]
[Route("api/catalogs")]
[Authorize]
public class CatalogsController(IMediator mediator) : ControllerBase
{
    // ===== Units =====
    [HttpGet("units")]
    public async Task<List<UnitOfMeasureDto>> ListUnits([FromQuery] string? q, CancellationToken ct)
        => await mediator.Send(new ListUnitsQuery(q), ct);

    [HttpPost("units")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> CreateUnit([FromBody] CreateUnitCommand body, CancellationToken ct)
    {
        var id = await mediator.Send(body, ct);
        return CreatedAtAction(nameof(ListUnits), new { id }, new { id });
    }

    [HttpPut("units/{id:guid}")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> UpdateUnit(Guid id, [FromBody] UpdateUnitCommand body, CancellationToken ct)
    {
        if (id != body.Id) return BadRequest("Id mismatch");
        await mediator.Send(body, ct);
        return NoContent();
    }

    [HttpDelete("units/{id:guid}")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> DeleteUnit(Guid id, CancellationToken ct)
    {
        await mediator.Send(new DeleteUnitCommand(id), ct);
        return NoContent();
    }

    // ===== Materials =====
    [HttpGet("materials")]
    public async Task<List<MaterialItemDto>> ListMaterials([FromQuery] string? q, [FromQuery] string? category, CancellationToken ct)
        => await mediator.Send(new ListMaterialsQuery(q, category), ct);

    [HttpPost("materials")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> CreateMaterial([FromBody] CreateMaterialCommand body, CancellationToken ct)
    {
        var id = await mediator.Send(body, ct);
        return CreatedAtAction(nameof(ListMaterials), new { id }, new { id });
    }

    [HttpPut("materials/{id:guid}")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> UpdateMaterial(Guid id, [FromBody] UpdateMaterialCommand body, CancellationToken ct)
    {
        if (id != body.Id) return BadRequest("Id mismatch");
        await mediator.Send(body, ct);
        return NoContent();
    }

    [HttpDelete("materials/{id:guid}")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> DeleteMaterial(Guid id, CancellationToken ct)
    {
        await mediator.Send(new DeleteMaterialCommand(id), ct);
        return NoContent();
    }

    // ===== Services =====
    [HttpGet("services")]
    public async Task<List<ServiceItemDto>> ListServices([FromQuery] string? q, [FromQuery] string? category, CancellationToken ct)
        => await mediator.Send(new ListServicesQuery(q, category), ct);

    [HttpPost("services")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> CreateService([FromBody] CreateServiceCommand body, CancellationToken ct)
    {
        var id = await mediator.Send(body, ct);
        return CreatedAtAction(nameof(ListServices), new { id }, new { id });
    }

    [HttpPut("services/{id:guid}")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> UpdateService(Guid id, [FromBody] UpdateServiceCommand body, CancellationToken ct)
    {
        if (id != body.Id) return BadRequest("Id mismatch");
        await mediator.Send(body, ct);
        return NoContent();
    }

    [HttpDelete("services/{id:guid}")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> DeleteService(Guid id, CancellationToken ct)
    {
        await mediator.Send(new DeleteServiceCommand(id), ct);
        return NoContent();
    }

    // ===== Work Items =====
    [HttpGet("work-items")]
    public async Task<List<WorkItemDto>> ListWorkItems([FromQuery] string? q, [FromQuery] string? category, CancellationToken ct)
        => await mediator.Send(new ListWorkItemsQuery(q, category), ct);

    [HttpPost("work-items")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> CreateWorkItem([FromBody] CreateWorkItemCommand body, CancellationToken ct)
    {
        var id = await mediator.Send(body, ct);
        return CreatedAtAction(nameof(ListWorkItems), new { id }, new { id });
    }

    [HttpPut("work-items/{id:guid}")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> UpdateWorkItem(Guid id, [FromBody] UpdateWorkItemCommand body, CancellationToken ct)
    {
        if (id != body.Id) return BadRequest("Id mismatch");
        await mediator.Send(body, ct);
        return NoContent();
    }

    [HttpDelete("work-items/{id:guid}")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> DeleteWorkItem(Guid id, CancellationToken ct)
    {
        await mediator.Send(new DeleteWorkItemCommand(id), ct);
        return NoContent();
    }
}
