// EN: Slots Controller - Available slot finding APIs.
// VI: Controller Slots - APIs tìm slot thời gian khả dụng.

using Asp.Versioning;
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.Mvc;

namespace BookingService.API.Controllers;

[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/slots")]
[Produces("application/json")]
public class SlotsController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<SlotsController> _logger;

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

    /// <summary>
    /// EN: Find available time slots based on staff schedule, resource availability, and existing appointments.
    /// VI: Tìm các slot thời gian khả dụng dựa trên lịch nhân viên, tài nguyên và cuộc hẹn hiện tại.
    /// </summary>
    [HttpPost("find")]
    [ProducesResponseType(typeof(ApiResponse<List<TimeSlotDto>>), StatusCodes.Status200OK)]
    public async Task<ActionResult<ApiResponse<List<TimeSlotDto>>>> FindAvailableSlots(
        [FromBody] FindSlotsRequest request,
        CancellationToken cancellationToken = default)
    {
        var query = new FindAvailableSlotsQuery(
            request.ShopId,
            request.ServiceId,
            request.Date,
            request.ServiceDurationMinutes,
            request.StaffId,
            request.ResourceId
        );

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

        return Ok(ApiResponse<List<TimeSlotDto>>.Ok(
            result,
            $"Found {result.Count} available slots"));
    }
}
