// EN: Staff Schedules Controller - Staff schedule management APIs.
// VI: Controller StaffSchedules - APIs quản lý lịch làm việc nhân viên.

using BookingService.API.Application.Commands;
using BookingService.API.Application.DTOs;
using BookingService.API.Application.Queries;
using BookingService.API.Models.Requests;
using BookingService.API.Models.Responses;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace BookingService.API.Controllers;

[ApiController]
[Route("api/v1/staff/{staffId:guid}/schedule")]
[Produces("application/json")]
[Authorize]
public class StaffSchedulesController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<StaffSchedulesController> _logger;

    public StaffSchedulesController(IMediator mediator, ILogger<StaffSchedulesController> logger)
    {
        _mediator = mediator ?? throw new ArgumentNullException(nameof(mediator));
        _logger = logger ?? throw new ArgumentNullException(nameof(logger));
    }

    /// <summary>
    /// EN: Get staff schedule.
    /// VI: Lấy lịch làm việc của nhân viên.
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(ApiResponse<List<StaffScheduleDto>>), StatusCodes.Status200OK)]
    public async Task<ActionResult<ApiResponse<List<StaffScheduleDto>>>> GetSchedule(
        Guid staffId,
        [FromQuery] Guid shopId,
        CancellationToken cancellationToken = default)
    {
        var query = new GetStaffScheduleQuery(staffId, shopId);
        var result = await _mediator.Send(query, cancellationToken);

        return Ok(ApiResponse<List<StaffScheduleDto>>.Ok(result));
    }

    /// <summary>
    /// EN: Update staff schedule.
    /// VI: Cập nhật lịch làm việc nhân viên.
    /// </summary>
    [HttpPut]
    [ProducesResponseType(typeof(ApiResponse<List<StaffScheduleDto>>), StatusCodes.Status200OK)]
    public async Task<ActionResult<ApiResponse<List<StaffScheduleDto>>>> UpdateSchedule(
        Guid staffId,
        [FromBody] UpdateScheduleRequest request,
        CancellationToken cancellationToken = default)
    {
        var command = new UpdateStaffScheduleCommand(
            staffId,
            request.ShopId,
            request.Schedule
        );

        var result = await _mediator.Send(command, cancellationToken);

        return Ok(ApiResponse<List<StaffScheduleDto>>.Ok(result,  "Schedule updated successfully"));
    }
}
