using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using StorageService.API.Application.Queries;
using Swashbuckle.AspNetCore.Annotations;
using System.Security.Claims;
using Asp.Versioning;

namespace StorageService.API.Controllers;

/// <summary>
/// EN: Controller for storage quota operations.
/// VI: Controller cho các thao tác quota storage.
/// </summary>
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/quota")]
[SwaggerTag("Quota Management - View and manage storage quotas")]
public class QuotaController : ControllerBase
{
    private readonly IMediator _mediator;

    public QuotaController(IMediator mediator)
    {
        _mediator = mediator;
    }

    /// <summary>
    /// EN: Get current user's storage quota.
    /// VI: Lấy quota storage của user hiện tại.
    /// </summary>
    [HttpGet]
    [Authorize]
    [SwaggerOperation(Summary = "Get user quota", Description = "Get storage quota for current user")]
    [SwaggerResponse(200, "Quota retrieved successfully")]
    [SwaggerResponse(401, "Unauthorized")]
    public async Task<ActionResult<ApiResponse<QuotaDto>>> GetQuota(CancellationToken cancellationToken = default)
    {
        var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
        if (string.IsNullOrEmpty(userId))
            return Unauthorized(new ApiResponse<QuotaDto> { Success = false, Error = "User ID not found" });

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

        if (result == null)
            return NotFound(new ApiResponse<QuotaDto> { Success = false, Error = "Quota not found" });

        return Ok(new ApiResponse<QuotaDto> { Success = true, Data = result });
    }
}
