using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SolutionErp.Application.Office;

namespace SolutionErp.Api.Controllers;

// Phase 10.2 G-O2 (S36) — Phòng họp catalog API.
// Class-level [Authorize]: mọi user đăng nhập được List (FE Booking page cần load room dropdown).
// Per-action [Authorize(Roles = "Admin")] cho Create/Update/Delete: chỉ admin quản lý catalog.
// Pattern 12-bis cross-module mirror HrmConfigsController.cs (S35 cumulative 10×).

[ApiController]
[Route("api/meeting-rooms")]
[Authorize]
public class MeetingRoomsController(IMediator mediator) : ControllerBase
{
    [HttpGet]
    public async Task<List<MeetingRoomDto>> List(
        [FromQuery] string? search,
        [FromQuery] bool? isActiveOnly,
        CancellationToken ct)
        => await mediator.Send(new GetMeetingRoomsQuery(search, isActiveOnly), ct);

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

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

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