// EN: Resources Controller - Resource management APIs.
// VI: Controller Resources - APIs quản lý tài nguyên.

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

namespace BookingService.API.Controllers;

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

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

    /// <summary>
    /// EN: Get resources by shop.
    /// VI: Lấy danh sách tài nguyên theo shop.
    /// </summary>
    [HttpGet]
    [ProducesResponseType(typeof(ApiResponse<List<ResourceDto>>), StatusCodes.Status200OK)]
    public async Task<ActionResult<ApiResponse<List<ResourceDto>>>> GetResources(
        [FromQuery] Guid shopId,
        [FromQuery] bool? isActive = null,
        CancellationToken cancellationToken = default)
    {
        var query = new GetResourcesByShopQuery(shopId, isActive);
        var result = await _mediator.Send(query, cancellationToken);

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

    /// <summary>
    /// EN: Create a new resource.
    /// VI: Tạo tài nguyên mới.
    /// </summary>
    [HttpPost]
    [ProducesResponseType(typeof(ApiResponse<ResourceDto>), StatusCodes.Status201Created)]
    public async Task<ActionResult<ApiResponse<ResourceDto>>> CreateResource(
        [FromBody] CreateResourceRequest request,
        CancellationToken cancellationToken = default)
    {
        var command = new CreateResourceCommand(
            request.ShopId,
            request.Name,
            request.ResourceType,
            request.Capacity
        );

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

        return CreatedAtAction(
            nameof(GetResources),
            new { shopId = result.ShopId },
            ApiResponse<ResourceDto>.Ok(result, "Resource created successfully"));
    }

    /// <summary>
    /// EN: Update a resource.
    /// VI: Cập nhật tài nguyên.
    /// </summary>
    [HttpPut("{id:guid}")]
    [ProducesResponseType(typeof(ApiResponse<ResourceDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public async Task<ActionResult<ApiResponse<ResourceDto>>> UpdateResource(
        Guid id,
        [FromBody] UpdateResourceRequest request,
        CancellationToken cancellationToken = default)
    {
        var command = new UpdateResourceCommand(
            id,
            request.Name,
            request.Capacity,
            request.IsActive
        );

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

        return Ok(ApiResponse<ResourceDto>.Ok(result, "Resource updated successfully"));
    }
}
