using System.Security.Claims;

using HoneyBox.Core.Interfaces;
using HoneyBox.Model.Base;
using HoneyBox.Model.Models;
using HoneyBox.Model.Models.Goods;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace HoneyBox.Api.Controllers;

/// <summary>
/// 商品控制器 - 处理商品列表、详情、奖品等查询
/// </summary>
/// <remarks>
/// 提供商品列表查询、商品详情、子奖品列表、箱号管理、中奖记录等功能
/// </remarks>
[ApiController]
[Route("api")]
public class GoodsController : ControllerBase
{
    private readonly IGoodsService _goodsService;
    private readonly IPrizeService _prizeService;
    private readonly ILogger<GoodsController> _logger;

    public GoodsController(
        IGoodsService goodsService,
        IPrizeService prizeService,
        ILogger<GoodsController> logger)
    {
        _goodsService = goodsService;
        _prizeService = prizeService;
        _logger = logger;
    }

    /// <summary>
    /// 获取商品列表
    /// </summary>
    /// <remarks>
    /// POST /api/goods_list
    /// 
    /// 根据类型、分页参数获取商品列表
    /// 支持未登录访问，未登录用户只能查看解锁金额为0的商品
    /// Requirements: 1.1-1.6
    /// </remarks>
    /// <param name="request">商品列表请求参数，包含类型、分页信息</param>
    /// <returns>商品列表分页数据</returns>
    [HttpGet("goods_list")]
    [ProducesResponseType(typeof(ApiResponse<PageResponse<GoodsListDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<PageResponse<GoodsListDto>>> GetGoodsList([FromQuery] GoodsListRequest? request)
    {
        // 获取用户ID，未登录时为0
        var userId = GetCurrentUserId() ?? 0;

        try
        {
            request ??= new GoodsListRequest();
            if (request.Page < 1) request.Page = 1;
            if (request.PageSize < 1) request.PageSize = 15;

            var result = await _goodsService.GetGoodsListAsync(request, userId);
            return ApiResponse<PageResponse<GoodsListDto>>.Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get goods list: UserId={UserId}", userId);
            return ApiResponse<PageResponse<GoodsListDto>>.Fail("获取商品列表失败");
        }
    }

    /// <summary>
    /// 获取商品详情
    /// </summary>
    /// <remarks>
    /// GET /api/goods_detail
    /// 
    /// 根据商品ID获取商品详细信息，包含奖品列表、价格等
    /// 支持未登录访问，收藏状态请调用独立接口 POST /api/goods_collection_status 获取
    /// Requirements: 2.1-2.7
    /// </remarks>
    /// <param name="goods_id">商品ID</param>
    /// <param name="goods_num">箱号 (0表示自动选择)</param>
    /// <returns>商品详情数据</returns>
    [HttpGet("goods_detail")]
    [ProducesResponseType(typeof(ApiResponse<GoodsDetailResponseDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<GoodsDetailResponseDto>> GetGoodsDetail(
        [FromQuery(Name = "goods_id")] int goods_id = 0,
        [FromQuery(Name = "goods_num")] int goods_num = 0)
    {
        // 获取用户ID，未登录时为0
        var userId = GetCurrentUserId() ?? 0;

        try
        {
            if (goods_id <= 0)
            {
                return ApiResponse<GoodsDetailResponseDto>.Fail("商品ID不能为空");
            }

            var result = await _goodsService.GetGoodsDetailAsync(goods_id, goods_num, userId);
            return ApiResponse<GoodsDetailResponseDto>.Success(result);
        }
        catch (InvalidOperationException ex)
        {
            _logger.LogWarning("Get goods detail failed: GoodsId={GoodsId}, Error={Error}", goods_id, ex.Message);
            return ApiResponse<GoodsDetailResponseDto>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get goods detail: GoodsId={GoodsId}", goods_id);
            return ApiResponse<GoodsDetailResponseDto>.Fail("获取商品详情失败");
        }
    }

    /// <summary>
    /// 获取商品子奖品列表
    /// POST /api/goods_children
    /// Requirements: 3.1-3.3
    /// </summary>
    [HttpPost("goods_children")]
    [Authorize]
    public async Task<ApiResponse<List<GoodsChildrenDto>>> GetGoodsChildren([FromBody] GoodsChildrenRequest? request)
    {
        var userId = GetCurrentUserId();
        if (userId == null)
        {
            return ApiResponse<List<GoodsChildrenDto>>.Unauthorized();
        }

        try
        {
            if (request == null || request.GoodsId <= 0 || request.GoodsListId <= 0)
            {
                return ApiResponse<List<GoodsChildrenDto>>.Fail("参数不能为空");
            }

            var result = await _goodsService.GetGoodsChildrenAsync(request.GoodsId, request.GoodsNum, request.GoodsListId);
            return ApiResponse<List<GoodsChildrenDto>>.Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get goods children: GoodsId={GoodsId}, GoodsListId={GoodsListId}",
                request?.GoodsId, request?.GoodsListId);
            return ApiResponse<List<GoodsChildrenDto>>.Fail("获取子奖品列表失败");
        }
    }

    /// <summary>
    /// 获取商品扩展配置
    /// POST /api/goods_extend
    /// Requirements: 4.1-4.3
    /// </summary>
    [HttpPost("goods_extend")]
    [Authorize]
    public async Task<ApiResponse<GoodsExtendDto>> GetGoodsExtend([FromBody] GoodsExtendRequest? request)
    {
        var userId = GetCurrentUserId();
        if (userId == null)
        {
            return ApiResponse<GoodsExtendDto>.Unauthorized();
        }

        try
        {
            if (request == null || request.GoodsId <= 0)
            {
                return ApiResponse<GoodsExtendDto>.Fail("商品ID不能为空");
            }

            var result = await _goodsService.GetGoodsExtendAsync(request.GoodsId, request.GoodsType);
            return ApiResponse<GoodsExtendDto>.Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get goods extend: GoodsId={GoodsId}", request?.GoodsId);
            return ApiResponse<GoodsExtendDto>.Fail("获取商品扩展配置失败");
        }
    }

    /// <summary>
    /// 获取箱号列表
    /// POST /api/goods_num_list
    /// Requirements: 5.1
    /// </summary>
    [HttpPost("goods_num_list")]
    [Authorize]
    public async Task<ApiResponse<List<BoxGroupDto>>> GetBoxList([FromBody] BoxListRequest? request)
    {
        var userId = GetCurrentUserId();
        if (userId == null)
        {
            return ApiResponse<List<BoxGroupDto>>.Unauthorized();
        }

        try
        {
            if (request == null || request.GoodsId <= 0)
            {
                return ApiResponse<List<BoxGroupDto>>.Fail("商品ID不能为空");
            }

            var result = await _goodsService.GetBoxListAsync(request.GoodsId);
            return ApiResponse<List<BoxGroupDto>>.Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get box list: GoodsId={GoodsId}", request?.GoodsId);
            return ApiResponse<List<BoxGroupDto>>.Fail("获取箱号列表失败");
        }
    }

    /// <summary>
    /// 获取箱号详情
    /// POST /api/goods_num_detail
    /// Requirements: 5.2-5.4
    /// </summary>
    [HttpPost("goods_num_detail")]
    [Authorize]
    public async Task<ApiResponse<List<BoxDetailDto>>> GetBoxDetail([FromBody] BoxDetailRequest? request)
    {
        var userId = GetCurrentUserId();
        if (userId == null)
        {
            return ApiResponse<List<BoxDetailDto>>.Unauthorized();
        }

        try
        {
            if (request == null || request.GoodsId <= 0)
            {
                return ApiResponse<List<BoxDetailDto>>.Fail("商品ID不能为空");
            }

            var result = await _goodsService.GetBoxDetailAsync(request.GoodsId, request.PageNo, request.Sort);
            return ApiResponse<List<BoxDetailDto>>.Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get box detail: GoodsId={GoodsId}", request?.GoodsId);
            return ApiResponse<List<BoxDetailDto>>.Fail("获取箱号详情失败");
        }
    }

    /// <summary>
    /// 获取奖品数量统计
    /// POST /api/goods_prize_count
    /// Requirements: 7.1
    /// </summary>
    [HttpPost("goods_prize_count")]
    [Authorize]
    public async Task<ApiResponse<PrizeCountResponseDto>> GetPrizeCount([FromBody] PrizeCountRequest? request)
    {
        var userId = GetCurrentUserId();
        if (userId == null)
        {
            return ApiResponse<PrizeCountResponseDto>.Unauthorized();
        }

        try
        {
            if (request == null || request.GoodsId <= 0)
            {
                return ApiResponse<PrizeCountResponseDto>.Fail("商品ID不能为空");
            }

            var result = await _prizeService.GetPrizeCountAsync(request.GoodsId);
            return ApiResponse<PrizeCountResponseDto>.Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get prize count: GoodsId={GoodsId}", request?.GoodsId);
            return ApiResponse<PrizeCountResponseDto>.Fail("获取奖品统计失败");
        }
    }

    /// <summary>
    /// 获取奖品内容
    /// POST /api/goods_prize_content
    /// Requirements: 7.2
    /// </summary>
    [HttpPost("goods_prize_content")]
    [Authorize]
    public async Task<ApiResponse<PrizeContentResponseDto>> GetPrizeContent([FromBody] PrizeContentRequest? request)
    {
        var userId = GetCurrentUserId();
        if (userId == null)
        {
            return ApiResponse<PrizeContentResponseDto>.Unauthorized();
        }

        try
        {
            if (request == null || request.GoodsId <= 0)
            {
                return ApiResponse<PrizeContentResponseDto>.Fail("商品ID不能为空");
            }

            var result = await _prizeService.GetPrizeContentAsync(request.GoodsId, request.Num);
            return ApiResponse<PrizeContentResponseDto>.Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get prize content: GoodsId={GoodsId}", request?.GoodsId);
            return ApiResponse<PrizeContentResponseDto>.Fail("获取奖品内容失败");
        }
    }

    /// <summary>
    /// 获取中奖记录
    /// </summary>
    /// <remarks>
    /// POST /api/goods_prize_logs
    /// 
    /// 获取指定商品的中奖记录列表，支持分页
    /// Requirements: 8.1-8.4
    /// </remarks>
    /// <param name="request">中奖记录请求参数，包含商品ID、箱号、分页信息</param>
    /// <returns>中奖记录分页数据</returns>
    [HttpGet("goods_prize_logs")]
    [ProducesResponseType(typeof(ApiResponse<PrizeLogsResponseDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(typeof(ApiResponse<PrizeLogsResponseDto>), StatusCodes.Status401Unauthorized)]
    public async Task<ApiResponse<PrizeLogsResponseDto>> GetPrizeLogs([FromQuery] PrizeLogsRequest? request)
    {

        try
        {
            if (request == null || request.GoodsId <= 0)
            {
                return ApiResponse<PrizeLogsResponseDto>.Fail("商品ID不能为空");
            }

            var page = request.Page < 1 ? 1 : request.Page;
            var pageSize = request.PageSize < 1 ? 15 : request.PageSize;

            var result = await _prizeService.GetPrizeLogsAsync(
                request.GoodsId,
                request.GoodsNum,
                request.ShangId ?? 0,
                page,
                pageSize);
            return ApiResponse<PrizeLogsResponseDto>.Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get prize logs: GoodsId={GoodsId}", request?.GoodsId);
            return ApiResponse<PrizeLogsResponseDto>.Fail("获取中奖记录失败");
        }
    }

    #region Private Helper Methods

    /// <summary>
    /// 获取当前登录用户ID
    /// </summary>
    private int? GetCurrentUserId()
    {
        var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier);
        if (userIdClaim == null || !int.TryParse(userIdClaim.Value, out var userId))
        {
            return null;
        }
        return userId;
    }

    #endregion

    #region Legacy Box List Endpoints (兼容旧版前端)

    /// <summary>
    /// 获取箱号分页列表（兼容旧版前端）
    /// </summary>
    /// <remarks>
    /// GET /api/goodslist_count
    /// 
    /// 返回箱号分页列表，用于一番赏换箱功能
    /// 例如：商品有50箱，返回 ["1-10", "11-20", "21-30", "31-40", "41-50"]
    /// </remarks>
    /// <param name="goods_id">商品ID</param>
    /// <returns>箱号分页列表</returns>
    [HttpGet("goodslist_count")]
    [ProducesResponseType(typeof(ApiResponse<List<BoxGroupDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<List<BoxGroupDto>>> GetGoodsListCount(
        [FromQuery(Name = "goods_id")] int goods_id = 0)
    {
        try
        {
            if (goods_id <= 0)
            {
                return ApiResponse<List<BoxGroupDto>>.Fail("商品ID不能为空");
            }

            var result = await _goodsService.GetBoxListAsync(goods_id);
            return ApiResponse<List<BoxGroupDto>>.Success(result);
        }
        catch (InvalidOperationException ex)
        {
            _logger.LogWarning("Get goodslist_count failed: GoodsId={GoodsId}, Error={Error}", goods_id, ex.Message);
            return ApiResponse<List<BoxGroupDto>>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get goodslist_count: GoodsId={GoodsId}", goods_id);
            return ApiResponse<List<BoxGroupDto>>.Fail("获取箱号列表失败");
        }
    }

    /// <summary>
    /// 获取箱号详情列表（兼容旧版前端）
    /// </summary>
    /// <remarks>
    /// GET /api/goodslist_content
    /// 
    /// 返回指定页码范围内的箱子详情，包含每个箱子的奖品列表和剩余库存
    /// </remarks>
    /// <param name="goods_id">商品ID</param>
    /// <param name="page_no">页码（从0开始）</param>
    /// <param name="sort">排序方式：0=箱号升序, 1=箱号降序, 2=余量降序</param>
    /// <returns>箱号详情列表</returns>
    [HttpGet("goodslist_content")]
    [ProducesResponseType(typeof(ApiResponse<List<BoxDetailDto>>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<List<BoxDetailDto>>> GetGoodsListContent(
        [FromQuery(Name = "goods_id")] int goods_id = 0,
        [FromQuery(Name = "page_no")] int page_no = 0,
        [FromQuery(Name = "sort")] int sort = 1)
    {
        try
        {
            if (goods_id <= 0)
            {
                return ApiResponse<List<BoxDetailDto>>.Fail("商品ID不能为空");
            }

            var result = await _goodsService.GetBoxDetailAsync(goods_id, page_no, sort);
            return ApiResponse<List<BoxDetailDto>>.Success(result);
        }
        catch (InvalidOperationException ex)
        {
            _logger.LogWarning("Get goodslist_content failed: GoodsId={GoodsId}, Error={Error}", goods_id, ex.Message);
            return ApiResponse<List<BoxDetailDto>>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get goodslist_content: GoodsId={GoodsId}", goods_id);
            return ApiResponse<List<BoxDetailDto>>.Fail("获取箱号详情失败");
        }
    }

    #endregion

    #region Infinite Goods Detail Endpoints

    /// <summary>
    /// 获取无限赏商品详情
    /// </summary>
    /// <remarks>
    /// GET /api/infinite_goodsdetail
    /// 
    /// 获取无限赏类型商品的详细信息，包含赏池分类、奖品列表、限购信息等
    /// Requirements: 5.1
    /// </remarks>
    /// <param name="goods_id">商品ID</param>
    /// <returns>无限赏商品详情</returns>
    [HttpGet("infinite_goodsdetail")]
    [ProducesResponseType(typeof(ApiResponse<InfiniteGoodsDetailResponseDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<InfiniteGoodsDetailResponseDto>> GetInfiniteGoodsDetail(
        [FromQuery(Name = "goods_id")] int goods_id = 0)
    {
        var userId = GetCurrentUserId() ?? 0;

        try
        {
            if (goods_id <= 0)
            {
                return ApiResponse<InfiniteGoodsDetailResponseDto>.Fail("商品ID不能为空");
            }

            var result = await _goodsService.GetInfiniteGoodsDetailAsync(goods_id, userId);
            return ApiResponse<InfiniteGoodsDetailResponseDto>.Success(result);
        }
        catch (InvalidOperationException ex)
        {
            _logger.LogWarning("Get infinite goods detail failed: GoodsId={GoodsId}, Error={Error}", goods_id, ex.Message);
            return ApiResponse<InfiniteGoodsDetailResponseDto>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get infinite goods detail: GoodsId={GoodsId}", goods_id);
            return ApiResponse<InfiniteGoodsDetailResponseDto>.Fail("获取无限赏商品详情失败");
        }
    }

    /// <summary>
    /// 获取一番赏商品详情 (POST)
    /// </summary>
    /// <remarks>
    /// POST /api/goodsdetail
    /// 
    /// 获取一番赏类型商品的详细信息 (POST方式，兼容旧版前端)
    /// Requirements: 5.2
    /// </remarks>
    /// <param name="request">商品详情请求</param>
    /// <returns>商品详情</returns>
    [HttpPost("goodsdetail")]
    [ProducesResponseType(typeof(ApiResponse<GoodsDetailResponseDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<GoodsDetailResponseDto>> GetGoodsDetailPost([FromBody] GoodsDetailRequest? request)
    {
        var userId = GetCurrentUserId() ?? 0;

        try
        {
            if (request == null || request.GoodsId <= 0)
            {
                return ApiResponse<GoodsDetailResponseDto>.Fail("商品ID不能为空");
            }

            var result = await _goodsService.GetGoodsDetailAsync(request.GoodsId, request.GoodsNum, userId);
            return ApiResponse<GoodsDetailResponseDto>.Success(result);
        }
        catch (InvalidOperationException ex)
        {
            _logger.LogWarning("Get goods detail (POST) failed: GoodsId={GoodsId}, Error={Error}", request?.GoodsId, ex.Message);
            return ApiResponse<GoodsDetailResponseDto>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get goods detail (POST): GoodsId={GoodsId}", request?.GoodsId);
            return ApiResponse<GoodsDetailResponseDto>.Fail("获取商品详情失败");
        }
    }

    /// <summary>
    /// 获取连击赏商品详情
    /// </summary>
    /// <remarks>
    /// GET /api/infinite_goodsdetail2
    /// 
    /// 获取连击赏类型商品的详细信息，包含普通池和秘宝池
    /// Requirements: 5.3
    /// </remarks>
    /// <param name="goods_id">商品ID</param>
    /// <param name="type">类型: 0=普通池, 1=秘宝池</param>
    /// <returns>连击赏商品详情</returns>
    [HttpGet("infinite_goodsdetail2")]
    [Authorize]
    [ProducesResponseType(typeof(ApiResponse<ComboGoodsDetailResponseDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<ComboGoodsDetailResponseDto>> GetComboGoodsDetail(
        [FromQuery(Name = "goods_id")] int goods_id = 0,
        [FromQuery(Name = "type")] int type = 1)
    {
        var userId = GetCurrentUserId();
        if (userId == null)
        {
            return ApiResponse<ComboGoodsDetailResponseDto>.Unauthorized();
        }

        try
        {
            if (goods_id <= 0)
            {
                return ApiResponse<ComboGoodsDetailResponseDto>.Fail("商品ID不能为空");
            }

            var result = await _goodsService.GetComboGoodsDetailAsync(goods_id, type, userId.Value);
            return ApiResponse<ComboGoodsDetailResponseDto>.Success(result);
        }
        catch (InvalidOperationException ex)
        {
            _logger.LogWarning("Get combo goods detail failed: GoodsId={GoodsId}, Error={Error}", goods_id, ex.Message);
            return ApiResponse<ComboGoodsDetailResponseDto>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get combo goods detail: GoodsId={GoodsId}", goods_id);
            return ApiResponse<ComboGoodsDetailResponseDto>.Fail("获取连击赏商品详情失败");
        }
    }

    /// <summary>
    /// 获取一番赏中奖记录
    /// </summary>
    /// <remarks>
    /// GET /api/shang_log
    /// 
    /// 获取指定商品的中奖记录列表，支持按赏品分类筛选
    /// Requirements: 5.4
    /// </remarks>
    /// <param name="goods_id">商品ID</param>
    /// <param name="goods_num">箱号</param>
    /// <param name="shang_id">赏品分类ID (0表示全部)</param>
    /// <param name="page">页码</param>
    /// <param name="page_size">每页数量</param>
    /// <returns>中奖记录</returns>
    [HttpGet("shang_log")]
    [ProducesResponseType(typeof(ApiResponse<ShangLogResponseDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<ShangLogResponseDto>> GetShangLog(
        [FromQuery(Name = "goods_id")] int goods_id = 0,
        [FromQuery(Name = "goods_num")] int goods_num = 0,
        [FromQuery(Name = "shang_id")] int shang_id = 0,
        [FromQuery(Name = "page")] int page = 1,
        [FromQuery(Name = "page_size")] int page_size = 100)
    {
        try
        {
            if (goods_id <= 0)
            {
                return ApiResponse<ShangLogResponseDto>.Fail("商品ID不能为空");
            }

            if (page < 1) page = 1;
            if (page_size < 1) page_size = 100;

            var result = await _goodsService.GetShangLogAsync(goods_id, goods_num, shang_id, page, page_size);
            return ApiResponse<ShangLogResponseDto>.Success(result);
        }
        catch (InvalidOperationException ex)
        {
            _logger.LogWarning("Get shang log failed: GoodsId={GoodsId}, Error={Error}", goods_id, ex.Message);
            return ApiResponse<ShangLogResponseDto>.Fail(ex.Message);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get shang log: GoodsId={GoodsId}", goods_id);
            return ApiResponse<ShangLogResponseDto>.Fail("获取中奖记录失败");
        }
    }

    /// <summary>
    /// 获取领主王数据
    /// </summary>
    /// <remarks>
    /// GET /api/ling_zhu_king
    /// 
    /// 获取领主王活动数据，包含当前领主、挑战者列表或领主记录
    /// Requirements: 5.5
    /// </remarks>
    /// <param name="goods_id">商品ID</param>
    /// <param name="type">类型: 1=挑战人数, 2=领主记录</param>
    /// <param name="page">页码</param>
    /// <param name="page_size">每页数量</param>
    /// <returns>领主王数据</returns>
    [HttpGet("ling_zhu_king")]
    [ProducesResponseType(typeof(ApiResponse<LingZhuKingResponseDto>), StatusCodes.Status200OK)]
    public async Task<ApiResponse<LingZhuKingResponseDto>> GetLingZhuKing(
        [FromQuery(Name = "goods_id")] int goods_id = 0,
        [FromQuery(Name = "type")] int type = 1,
        [FromQuery(Name = "page")] int page = 1,
        [FromQuery(Name = "page_size")] int page_size = 20)
    {
        try
        {
            if (goods_id <= 0)
            {
                return ApiResponse<LingZhuKingResponseDto>.Fail("参数错误");
            }

            if (page < 1) page = 1;
            if (page_size < 1) page_size = 20;

            var result = await _goodsService.GetLingZhuKingAsync(goods_id, type, page, page_size);
            return ApiResponse<LingZhuKingResponseDto>.Success(result);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Failed to get ling zhu king: GoodsId={GoodsId}", goods_id);
            return ApiResponse<LingZhuKingResponseDto>.Fail("获取领主王数据失败");
        }
    }

    #endregion
}
