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/user")]
    
    public class UserController : BaseController
    {
        private readonly IPersonalSpaceService _personalSpaceService;
        private readonly ILogger<UserController> _logger;
        
        public UserController(IPersonalSpaceService personalSpaceService, ILogger<UserController> logger)
        {
            _personalSpaceService = personalSpaceService;
            _logger = logger;
        }
        
        /// <summary>
        /// 获取用户订阅信息
        /// </summary>
        /// <returns>用户订阅信息</returns>
        [HttpGet("subscription")]
        public async Task<IActionResult> GetSubscription()
        {
            try
            {
                var userId = GetCurrentUserId();
                if (userId <= 0)
                {
                    return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
                }
                
                var result = await _personalSpaceService.GetSubscriptionAsync(userId);
                
                if (result.Success)
                {
                    return Ok(result);
                }
                
                return BadRequest(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取订阅信息时发生错误");
                return StatusCode(500, ApiResponse<SubscriptionResponseDto>.ErrorResult("服务器内部错误"));
            }
        }
        
        /// <summary>
        /// 获取用户资料
        /// </summary>
        /// <returns>用户资料</returns>
        [HttpGet("profile")]
        public async Task<IActionResult> GetUserProfile()
        {
            try
            {
                var userId = GetCurrentUserId();
                if (userId <= 0)
                {
                    return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
                }
                
                var result = await _personalSpaceService.GetUserProfileAsync(userId);
                
                if (result.Success)
                {
                    return Ok(result);
                }
                
                return BadRequest(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取用户资料时发生错误");
                return StatusCode(500, ApiResponse<UserProfileResponseDto>.ErrorResult("服务器内部错误"));
            }
        }
    }
}