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/[controller]s")]
[Authorize]
public class StrategyController : ControllerBase
{
    private readonly ILogger<StrategyController> _logger;

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

    [HttpGet]
    public ActionResult<ApiResponse<List<StrategyItem>>> GetStrategies()
    {
        try
        {
            _logger.LogInformation("Request to get strategies");
            
            // 模拟返回策略列表
            var strategies = new List<StrategyItem>
            {
                new StrategyItem
                {
                    Id = "1",
                    IconChar = "A",
                    Title = "均值回归策略",
                    Tag = "量化",
                    Desc = "基于价格均值回归原理的交易策略",
                    BgClass = "bg-blue-100",
                    TagClass = "bg-blue-500",
                    Tags = new[] { "量化", "均值回归", "中风险" },
                    BtnText = "应用",
                    BtnClass = "bg-blue-500"
                },
                new StrategyItem
                {
                    Id = "2",
                    IconChar = "B",
                    Title = "趋势跟踪策略",
                    Tag = "趋势",
                    Desc = "跟随市场趋势的交易策略",
                    BgClass = "bg-green-100",
                    TagClass = "bg-green-500",
                    Tags = new[] { "趋势", "中风险", "长线" },
                    BtnText = "应用",
                    BtnClass = "bg-green-500"
                },
                new StrategyItem
                {
                    Id = "3",
                    IconChar = "C",
                    Title = "动量策略",
                    Tag = "动量",
                    Desc = "基于价格动量效应的交易策略",
                    BgClass = "bg-yellow-100",
                    TagClass = "bg-yellow-500",
                    Tags = new[] { "动量", "中高风险", "短线" },
                    BtnText = "应用",
                    BtnClass = "bg-yellow-500"
                }
            };

            _logger.LogInformation("Strategies retrieved successfully");
            
            return Ok(new ApiResponse<List<StrategyItem>>
            {
                Code = AssetManager.Models.StatusCodes.Success,
                Data = strategies,
                Message = "Success"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error retrieving strategies");
            
            return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<List<StrategyItem>>
            {
                Code = AssetManager.Models.StatusCodes.InternalServerError,
                Data = null,
                Message = ex.Message
            });
        }
    }

    [HttpGet("{id}")]
    public ActionResult<ApiResponse<StrategyDetail>> GetStrategyById(string id)
    {
        try
        {
            _logger.LogInformation($"Request to get strategy by id: {id}");
            
            // 模拟返回策略详情
            var strategy = new StrategyDetail
            {
                Id = id,
                IconChar = "A",
                Title = "均值回归策略",
                Tag = "量化",
                Desc = "基于价格均值回归原理的交易策略",
                BgClass = "bg-blue-100",
                TagClass = "bg-blue-500",
                Tags = new[] { "量化", "均值回归", "中风险" },
                BtnText = "应用",
                BtnClass = "bg-blue-500",
                Parameters = new { period = 20, threshold = 2.0 },
                Backtest = new { annualReturn = 12.5, maxDrawdown = 15.2, sharpeRatio = 1.8 }
            };

            _logger.LogInformation($"Strategy {id} retrieved successfully");
            
            return Ok(new ApiResponse<StrategyDetail>
            {
                Code = AssetManager.Models.StatusCodes.Success,
                Data = strategy,
                Message = "Success"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, $"Error retrieving strategy {id}");
            
            return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<StrategyDetail>
            {
                Code = AssetManager.Models.StatusCodes.InternalServerError,
                Data = null,
                Message = ex.Message
            });
        }
    }

    [HttpPost]
    public ActionResult<ApiResponse<StrategyResponse>> CreateStrategy([FromBody] CreateStrategyRequest request)
    {
        try
        {
            _logger.LogInformation($"Request to create strategy: {request.Title}");
            
            // 模拟创建策略
            var response = new StrategyResponse
            {
                Id = Guid.NewGuid().ToString(),
                Title = request.Title,
                Status = "created"
            };

            _logger.LogInformation($"Strategy {response.Id} created successfully");
            
            return Ok(new ApiResponse<StrategyResponse>
            {
                Code = AssetManager.Models.StatusCodes.Success,
                Data = response,
                Message = "Strategy created successfully"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error creating strategy");
            
            return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<StrategyResponse>
            {
                Code = AssetManager.Models.StatusCodes.InternalServerError,
                Data = null,
                Message = ex.Message
            });
        }
    }

    [HttpPut("{id}")]
    public ActionResult<ApiResponse<StrategyResponse>> UpdateStrategy(string id, [FromBody] UpdateStrategyRequest request)
    {
        try
        {
            _logger.LogInformation($"Request to update strategy: {id}");
            
            // 模拟更新策略
            var response = new StrategyResponse
            {
                Id = id,
                Title = request.Title,
                Status = "updated"
            };

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

    [HttpDelete("{id}")]
    public ActionResult<ApiResponse<DeleteStrategyResponse>> DeleteStrategy(string id)
    {
        try
        {
            _logger.LogInformation($"Request to delete strategy: {id}");
            
            // 模拟删除策略
            var response = new DeleteStrategyResponse
            {
                Id = id,
                Status = "deleted"
            };

            _logger.LogInformation($"Strategy {id} deleted successfully");
            
            return Ok(new ApiResponse<DeleteStrategyResponse>
            {
                Code = AssetManager.Models.StatusCodes.Success,
                Data = response,
                Message = "Strategy deleted successfully"
            });
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, $"Error deleting strategy {id}");
            
            return StatusCode(AssetManager.Models.StatusCodes.InternalServerError, new ApiResponse<DeleteStrategyResponse>
            {
                Code = AssetManager.Models.StatusCodes.InternalServerError,
                Data = null,
                Message = ex.Message
            });
        }
    }
}