// EN: Therapists Controller - Spa/Beauty therapist management APIs.
// VI: Controller Therapists - APIs quản lý chuyên viên Spa/Beauty.

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

namespace BookingService.API.Controllers;

/// <summary>
/// EN: Controller for managing spa/beauty therapists.
/// VI: Controller quản lý chuyên viên spa/beauty.
/// </summary>
[ApiController]
[Route("api/v1/therapists")]
[Produces("application/json")]
[Authorize]
public class TherapistsController : ControllerBase
{
    private readonly IMediator _mediator;
    private readonly ILogger<TherapistsController> _logger;

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

    /// <summary>
    /// EN: Get therapists by shop with optional active filter.
    /// VI: Lấy danh sách chuyên viên theo shop với tùy chọn lọc hoạt động.
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(ApiResponse<List<TherapistDto>>), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<ActionResult<ApiResponse<List<TherapistDto>>>> GetTherapists(
        [FromQuery] Guid shopId,
        [FromQuery] bool? isActive = null,
        CancellationToken cancellationToken = default)
    {
        if (shopId == Guid.Empty)
        {
            return BadRequest(ApiResponse<List<TherapistDto>>.Fail(
                "Shop ID is required / Shop ID là bắt buộc"));
        }

        var query = new GetTherapistsQuery(shopId, isActive);
        var result = await _mediator.Send(query, cancellationToken);

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

    /// <summary>
    /// EN: Create a new therapist.
    /// VI: Tạo chuyên viên mới.
    /// </summary>
    [HttpPost]
    [ProducesResponseType(typeof(ApiResponse<TherapistDto>), StatusCodes.Status201Created)]
    [ProducesResponseType(StatusCodes.Status400BadRequest)]
    public async Task<ActionResult<ApiResponse<TherapistDto>>> CreateTherapist(
        [FromBody] CreateTherapistRequest request,
        CancellationToken cancellationToken = default)
    {
        var command = new CreateTherapistCommand(
            request.ShopId,
            request.Name,
            request.Specialties,
            request.WorkingHours
        );

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

        return CreatedAtAction(
            nameof(GetTherapists),
            new { shopId = result.ShopId },
            ApiResponse<TherapistDto>.Ok(result, "Therapist created successfully / Tạo chuyên viên thành công"));
    }

    /// <summary>
    /// EN: Update a therapist.
    /// VI: Cập nhật chuyên viên.
    /// </summary>
    [HttpPut("{id:guid}")]
    [ProducesResponseType(typeof(ApiResponse<TherapistDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<ApiResponse<TherapistDto>>> UpdateTherapist(
        Guid id,
        [FromBody] UpdateTherapistRequest request,
        CancellationToken cancellationToken = default)
    {
        var command = new UpdateTherapistCommand(
            id,
            request.Name,
            request.Specialties,
            request.WorkingHours
        );

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

        return Ok(ApiResponse<TherapistDto>.Ok(result, "Therapist updated successfully / Cập nhật chuyên viên thành công"));
    }

    /// <summary>
    /// EN: Deactivate a therapist (soft delete).
    /// VI: Vô hiệu hóa chuyên viên (xóa mềm).
    /// </summary>
    [HttpDelete("{id:guid}")]
    [ProducesResponseType(typeof(ApiResponse<bool>), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<ApiResponse<bool>>> DeactivateTherapist(
        Guid id,
        CancellationToken cancellationToken = default)
    {
        var command = new DeactivateTherapistCommand(id);
        var result = await _mediator.Send(command, cancellationToken);

        return Ok(ApiResponse<bool>.Ok(result, "Therapist deactivated successfully / Vô hiệu hóa chuyên viên thành công"));
    }
}
