using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using FutureMailAPI.Services;
using FutureMailAPI.DTOs;
using FutureMailAPI.Helpers;

namespace FutureMailAPI.Controllers
{
    [ApiController]
    [Route("api/v1/[controller]")]
    
    public class PersonalSpaceController : BaseController
    {
        private readonly IPersonalSpaceService _personalSpaceService;
        private readonly ILogger<PersonalSpaceController> _logger;
        
        public PersonalSpaceController(IPersonalSpaceService personalSpaceService, ILogger<PersonalSpaceController> logger)
        {
            _personalSpaceService = personalSpaceService;
            _logger = logger;
        }
        
        /// <summary>
        /// 获取用户时间线
        /// </summary>
        /// <param name="type">时间线类型</param>
        /// <param name="startDate">开始日期</param>
        /// <param name="endDate">结束日期</param>
        /// <returns>用户时间线</returns>
        [HttpGet("timeline")]
        public async Task<IActionResult> GetTimeline(
            [FromQuery] TimelineType type = TimelineType.ALL,
            [FromQuery] DateTime? startDate = null,
            [FromQuery] DateTime? endDate = null)
        {
            try
            {
                var userId = GetCurrentUserId();
                if (userId <= 0)
                {
                    return Unauthorized(ApiResponse<object>.ErrorResult("无效的用户令牌"));
                }
                
                var query = new TimelineQueryDto
                {
                    Type = type,
                    StartDate = startDate,
                    EndDate = endDate
                };
                
                var result = await _personalSpaceService.GetTimelineAsync(userId, query);
                
                if (result.Success)
                {
                    return Ok(result);
                }
                
                return BadRequest(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取时间线时发生错误");
                return StatusCode(500, ApiResponse<TimelineResponseDto>.ErrorResult("服务器内部错误"));
            }
        }
        
        /// <summary>
        /// 获取用户统计数据
        /// </summary>
        /// <returns>用户统计数据</returns>
        [HttpGet("statistics")]
        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("服务器内部错误"));
            }
        }
        
        /// <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("服务器内部错误"));
            }
        }
    }
}