using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using FutureMailAPI.Services;
using FutureMailAPI.DTOs;
using System.Security.Claims;

namespace FutureMailAPI.Controllers
{
    [ApiController]
    [Route("api/v1/statistics")]
    
    public class StatisticsController : BaseController
    {
        private readonly IPersonalSpaceService _personalSpaceService;
        private readonly ILogger<StatisticsController> _logger;
        
        public StatisticsController(IPersonalSpaceService personalSpaceService, ILogger<StatisticsController> logger)
        {
            _personalSpaceService = personalSpaceService;
            _logger = logger;
        }
        
        /// <summary>
        /// 获取用户统计数据
        /// </summary>
        /// <returns>用户统计数据</returns>
        [HttpGet]
        public async Task<IActionResult> GetStatistics()
        {
            try
            {
                var userId = GetCurrentUserId();
                if (userId <= 0)
                {
                    return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
                }
                
                var result = await _personalSpaceService.GetStatisticsAsync(userId);
                
                if (result.Success)
                {
                    return Ok(result);
                }
                
                return BadRequest(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取统计数据时发生错误");
                return StatusCode(500, ApiResponse<StatisticsResponseDto>.ErrorResult("服务器内部错误"));
            }
        }
    }
}