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

namespace SolutionErp.Api.Controllers;

// HRM Config API — 4 sub-resource (leave-types, holidays, shifts, ot-policies)
// dùng Mediator dispatch theo command/query type. Authorize = đăng nhập đủ
// (mọi role được đọc cho dropdown; ghi cần Admin).
// Pattern 12-bis mirror Master/Catalogs/CatalogsController.cs (S29 cumulative 2×).

[ApiController]
[Route("api/hrm-configs")]
[Authorize]
public class HrmConfigsController(IMediator mediator) : ControllerBase
{
    // ===== Leave Types =====
    [HttpGet("leave-types")]
    public async Task<List<LeaveTypeDto>> ListLeaveTypes([FromQuery] string? q, [FromQuery] bool? isActive, CancellationToken ct)
        => await mediator.Send(new ListLeaveTypesQuery(q, isActive), ct);

    [HttpPost("leave-types")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> CreateLeaveType([FromBody] CreateLeaveTypeCommand body, CancellationToken ct)
    {
        var id = await mediator.Send(body, ct);
        return CreatedAtAction(nameof(ListLeaveTypes), new { id }, new { id });
    }

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

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

    // ===== Holidays =====
    [HttpGet("holidays")]
    public async Task<List<HolidayDto>> ListHolidays([FromQuery] string? q, [FromQuery] int? year, CancellationToken ct)
        => await mediator.Send(new ListHolidaysQuery(q, year), ct);

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

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

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

    // ===== Shifts =====
    [HttpGet("shifts")]
    public async Task<List<ShiftPatternDto>> ListShifts([FromQuery] string? q, [FromQuery] bool? isActive, CancellationToken ct)
        => await mediator.Send(new ListShiftsQuery(q, isActive), ct);

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

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

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

    // ===== OT Policies =====
    [HttpGet("ot-policies")]
    public async Task<List<OtPolicyDto>> ListOtPolicies([FromQuery] string? q, [FromQuery] bool? isActive, CancellationToken ct)
        => await mediator.Send(new ListOtPoliciesQuery(q, isActive), ct);

    [HttpPost("ot-policies")]
    [Authorize(Roles = "Admin")]
    public async Task<IActionResult> CreateOtPolicy([FromBody] CreateOtPolicyCommand body, CancellationToken ct)
    {
        var id = await mediator.Send(body, ct);
        return CreatedAtAction(nameof(ListOtPolicies), new { id }, new { id });
    }

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

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

    // ===== Vehicles (Mig 44 P11-C — S51) =====
    [HttpGet("vehicles")]
    public async Task<List<VehicleDto>> ListVehicles([FromQuery] string? q, [FromQuery] bool? isActive, CancellationToken ct)
        => await mediator.Send(new ListVehiclesQuery(q, isActive), ct);

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

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

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

    // ===== Drivers (Mig 44 P11-C — S51) =====
    [HttpGet("drivers")]
    public async Task<List<DriverDto>> ListDrivers([FromQuery] string? q, [FromQuery] bool? isActive, CancellationToken ct)
        => await mediator.Send(new ListDriversQuery(q, isActive), ct);

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

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

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