using AssetManager.API.DTOs;
using AssetManager.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace AssetManager.API.Controllers;

[ApiController]
[Route("api/user")]
[Authorize]
public class UserController : ControllerBase
{
    private readonly ILogger<UserController> _logger;

    public UserController(ILogger<UserController> logger)
    {
        _logger = logger;
    }

    [HttpGet("info")]
    public ActionResult<ApiResponse<UserInfoResponse>> GetUserInfo()
    {
        try
        {
            _logger.LogInformation("Request to get user info");
            
            // 模拟返回用户信息
            var response = new UserInfoResponse
            {
                UserName = "Test User",
                MemberLevel = "高级会员",
                RunningDays = 180,
                Avatar = "https://example.com/avatar.jpg",
                Email = "test@example.com"
            };

            _logger.LogInformation("User info retrieved successfully");
            
            return Ok(new ApiResponse<UserInfoResponse>
            {
                Code = AssetManager.Models.StatusCodes.Success,
                Data = response,
                Message = "Success"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error retrieving user info");
            
            return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<UserInfoResponse>
            {
                Code = AssetManager.Models.StatusCodes.InternalServerError,
                Data = null,
                Message = ex.Message
            });
        }
    }

    [HttpGet("stats")]
    public ActionResult<ApiResponse<UserStatsResponse>> GetUserStats()
    {
        try
        {
            _logger.LogInformation("Request to get user stats");
            
            // 模拟返回用户统计数据
            var response = new UserStatsResponse
            {
                SignalsCaptured = 125,
                WinRate = 68.5,
                TotalTrades = 320,
                AverageProfit = 5.2
            };

            _logger.LogInformation("User stats retrieved successfully");
            
            return Ok(new ApiResponse<UserStatsResponse>
            {
                Code = AssetManager.Models.StatusCodes.Success,
                Data = response,
                Message = "Success"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error retrieving user stats");
            
            return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<UserStatsResponse>
            {
                Code = AssetManager.Models.StatusCodes.InternalServerError,
                Data = null,
                Message = ex.Message
            });
        }
    }

    [HttpPut("info")]
    public ActionResult<ApiResponse<UpdateUserResponse>> UpdateUserInfo([FromBody] UpdateUserRequest request)
    {
        try
        {
            _logger.LogInformation($"Request to update user info: {request.UserName}");
            
            // 模拟更新用户信息
            var response = new UpdateUserResponse
            {
                Status = "updated",
                UserName = request.UserName
            };

            _logger.LogInformation("User info updated successfully");
            
            return Ok(new ApiResponse<UpdateUserResponse>
            {
                Code = AssetManager.Models.StatusCodes.Success,
                Data = response,
                Message = "User info updated successfully"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error updating user info");
            
            return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<UpdateUserResponse>
            {
                Code = AssetManager.Models.StatusCodes.InternalServerError,
                Data = null,
                Message = ex.Message
            });
        }
    }
}