using System.Text.Json.Serialization;
using HoneyBox.Model.Models;
using HoneyBox.Model.Models.Goods;
using HoneyBox.Model.Models.Order;
using Microsoft.AspNetCore.Mvc;

namespace HoneyBox.Model.Models.Lottery;

#region Request Models

/// <summary>
/// 抽奖结果查询请求
/// </summary>
public class PrizeOrderLogRequest
{
    /// <summary>
    /// 订单号
    /// </summary>
    [JsonPropertyName("order_num")]
    public string OrderNum { get; set; } = string.Empty;
}

/// <summary>
/// 无限赏中奖记录请求
/// </summary>
public class InfiniteShangLogRequest : PageRequest
{
    /// <summary>
    /// 商品ID
    /// </summary>
    [JsonPropertyName("goods_id")]
    [FromQuery(Name = "goods_id")]
    public int GoodsId { get; set; }

    /// <summary>
    /// 赏品等级ID（0表示全部）
    /// </summary>
    [JsonPropertyName("shang_id")]
    [FromQuery(Name = "shang_id")]
    public int? ShangId { get; set; }

    /// <summary>
    /// 是否秘宝池（0=否，1=是）
    /// </summary>
    [JsonPropertyName("is_mibao")]
    [FromQuery(Name = "is_mibao")]
    public int? IsMibao { get; set; }
}

/// <summary>
/// 每日抽奖记录请求
/// </summary>
public class DailyPrizeRecordsRequest : PageRequest
{
    /// <summary>
    /// 商品ID
    /// </summary>
    [JsonPropertyName("goods_id")]
    public int GoodsId { get; set; }
}

/// <summary>
/// 无限赏抽奖记录请求（用户自己的记录）
/// </summary>
public class InfinitePrizeRecordsRequest : PageRequest
{
    /// <summary>
    /// 商品ID
    /// </summary>
    [JsonPropertyName("goods_id")]
    public int GoodsId { get; set; }
}

/// <summary>
/// 道具卡抽奖请求
/// </summary>
public class ItemCardDrawRequest
{
    /// <summary>
    /// 商品ID
    /// </summary>
    [JsonPropertyName("goods_id")]
    public int GoodsId { get; set; }

    /// <summary>
    /// 要重抽的订单项ID列表（逗号分隔）
    /// </summary>
    [JsonPropertyName("order_list_ids")]
    public string OrderListIds { get; set; } = string.Empty;
}

#endregion

#region Response Models

/// <summary>
/// 无限赏中奖记录响应
/// </summary>
public class InfiniteShangLogResponseDto
{
    /// <summary>
    /// 赏品分类列表
    /// </summary>
    [JsonPropertyName("category")]
    public List<ShangCategoryDto> Category { get; set; } = new();

    /// <summary>
    /// 中奖记录列表
    /// </summary>
    [JsonPropertyName("data")]
    public List<InfiniteShangLogItemDto> Data { get; set; } = new();

    /// <summary>
    /// 最后一页
    /// </summary>
    [JsonPropertyName("last_page")]
    public int LastPage { get; set; }

    /// <summary>
    /// 总数量
    /// </summary>
    [JsonPropertyName("total")]
    public int Total { get; set; }
}

/// <summary>
/// 赏品分类DTO
/// </summary>
public class ShangCategoryDto
{
    /// <summary>
    /// 赏品等级ID
    /// </summary>
    [JsonPropertyName("shang_id")]
    public int ShangId { get; set; }

    /// <summary>
    /// 赏品等级标题
    /// </summary>
    [JsonPropertyName("shang_title")]
    public string ShangTitle { get; set; } = string.Empty;
}

/// <summary>
/// 无限赏中奖记录项DTO
/// </summary>
public class InfiniteShangLogItemDto
{
    /// <summary>
    /// 记录ID
    /// </summary>
    [JsonPropertyName("id")]
    public int Id { get; set; }

    /// <summary>
    /// 用户ID
    /// </summary>
    [JsonPropertyName("user_id")]
    public int UserId { get; set; }

    /// <summary>
    /// 奖品标题
    /// </summary>
    [JsonPropertyName("goodslist_title")]
    public string GoodslistTitle { get; set; } = string.Empty;

    /// <summary>
    /// 奖品图片
    /// </summary>
    [JsonPropertyName("goodslist_imgurl")]
    public string GoodslistImgurl { get; set; } = string.Empty;

    /// <summary>
    /// 奖品价格
    /// </summary>
    [JsonPropertyName("goodslist_price")]
    public string GoodslistPrice { get; set; } = "0.00";

    /// <summary>
    /// 回收金额
    /// </summary>
    [JsonPropertyName("goodslist_money")]
    public string GoodslistMoney { get; set; } = "0.00";

    /// <summary>
    /// 赏品等级ID
    /// </summary>
    [JsonPropertyName("shang_id")]
    public int ShangId { get; set; }

    /// <summary>
    /// 赏品等级标题
    /// </summary>
    [JsonPropertyName("shang_title")]
    public string? ShangTitle { get; set; }

    /// <summary>
    /// 赏品等级颜色
    /// </summary>
    [JsonPropertyName("shang_color")]
    public string? ShangColor { get; set; }

    /// <summary>
    /// 中奖数量
    /// </summary>
    [JsonPropertyName("prize_num")]
    public int PrizeNum { get; set; } = 1;

    /// <summary>
    /// 创建时间戳
    /// </summary>
    [JsonPropertyName("addtime")]
    public string Addtime { get; set; } = string.Empty;

    /// <summary>
    /// 幸运号码
    /// </summary>
    [JsonPropertyName("luck_no")]
    public int LuckNo { get; set; }

    /// <summary>
    /// 翻倍倍数
    /// </summary>
    [JsonPropertyName("doubling")]
    public int Doubling { get; set; }

    /// <summary>
    /// 是否灵珠
    /// </summary>
    [JsonPropertyName("is_lingzhu")]
    public int IsLingzhu { get; set; }

    /// <summary>
    /// 用户信息
    /// </summary>
    [JsonPropertyName("user_info")]
    public LotteryUserInfoDto? UserInfo { get; set; }

    /// <summary>
    /// 赏品等级信息
    /// </summary>
    [JsonPropertyName("shang_info")]
    public ShangInfoDto? ShangInfo { get; set; }
}

/// <summary>
/// 抽奖用户信息DTO
/// </summary>
public class LotteryUserInfoDto
{
    /// <summary>
    /// 用户昵称（脱敏后）
    /// </summary>
    [JsonPropertyName("nickname")]
    public string Nickname { get; set; } = string.Empty;

    /// <summary>
    /// 用户头像
    /// </summary>
    [JsonPropertyName("headimg")]
    public string HeadImg { get; set; } = string.Empty;
}

/// <summary>
/// 每日抽奖记录响应
/// </summary>
public class DailyPrizeRecordsResponseDto
{
    /// <summary>
    /// 记录列表
    /// </summary>
    [JsonPropertyName("data")]
    public List<DailyPrizeRecordItemDto> Data { get; set; } = new();
}

/// <summary>
/// 无限赏抽奖记录响应（用户自己的记录）
/// </summary>
public class InfinitePrizeRecordsResponseDto
{
    /// <summary>
    /// 记录列表
    /// </summary>
    [JsonPropertyName("data")]
    public List<InfinitePrizeRecordItemDto> Data { get; set; } = new();

    /// <summary>
    /// 当前页码
    /// </summary>
    [JsonPropertyName("current_page")]
    public int CurrentPage { get; set; }

    /// <summary>
    /// 最后一页
    /// </summary>
    [JsonPropertyName("last_page")]
    public int LastPage { get; set; }

    /// <summary>
    /// 每页数量
    /// </summary>
    [JsonPropertyName("per_page")]
    public int PerPage { get; set; }

    /// <summary>
    /// 总数量
    /// </summary>
    [JsonPropertyName("total")]
    public int Total { get; set; }
}

/// <summary>
/// 无限赏抽奖记录项DTO（用户自己的记录）
/// </summary>
public class InfinitePrizeRecordItemDto
{
    /// <summary>
    /// 用户ID
    /// </summary>
    [JsonPropertyName("user_id")]
    public int UserId { get; set; }

    /// <summary>
    /// 奖品标题
    /// </summary>
    [JsonPropertyName("goodslist_title")]
    public string GoodslistTitle { get; set; } = string.Empty;

    /// <summary>
    /// 奖品图片
    /// </summary>
    [JsonPropertyName("goodslist_imgurl")]
    public string GoodslistImgurl { get; set; } = string.Empty;

    /// <summary>
    /// 创建时间（格式化后的字符串）
    /// </summary>
    [JsonPropertyName("addtime")]
    public string Addtime { get; set; } = string.Empty;
}

/// <summary>
/// 每日抽奖记录项DTO
/// </summary>
public class DailyPrizeRecordItemDto
{
    /// <summary>
    /// 记录ID
    /// </summary>
    [JsonPropertyName("id")]
    public int Id { get; set; }

    /// <summary>
    /// 用户ID
    /// </summary>
    [JsonPropertyName("user_id")]
    public int UserId { get; set; }

    /// <summary>
    /// 奖品标题
    /// </summary>
    [JsonPropertyName("goodslist_title")]
    public string GoodslistTitle { get; set; } = string.Empty;

    /// <summary>
    /// 奖品图片
    /// </summary>
    [JsonPropertyName("goodslist_imgurl")]
    public string GoodslistImgurl { get; set; } = string.Empty;

    /// <summary>
    /// 赏品等级ID
    /// </summary>
    [JsonPropertyName("shang_id")]
    public int ShangId { get; set; }

    /// <summary>
    /// 赏品等级标题
    /// </summary>
    [JsonPropertyName("shang_title")]
    public string? ShangTitle { get; set; }

    /// <summary>
    /// 创建时间戳
    /// </summary>
    [JsonPropertyName("addtime")]
    public string Addtime { get; set; } = string.Empty;

    /// <summary>
    /// 用户信息
    /// </summary>
    [JsonPropertyName("user_info")]
    public LotteryUserInfoDto? UserInfo { get; set; }

    /// <summary>
    /// 赏品等级信息
    /// </summary>
    [JsonPropertyName("shang_info")]
    public ShangInfoDto? ShangInfo { get; set; }
}

/// <summary>
/// 道具卡抽奖响应
/// </summary>
public class ItemCardDrawResponseDto
{
    /// <summary>
    /// 状态：0=余额支付成功，1=需要微信支付
    /// </summary>
    [JsonPropertyName("status")]
    public int Status { get; set; }

    /// <summary>
    /// 订单号
    /// </summary>
    [JsonPropertyName("order_num")]
    public string OrderNum { get; set; } = string.Empty;

    /// <summary>
    /// 抽奖结果列表
    /// </summary>
    [JsonPropertyName("prizes")]
    public List<ItemCardDrawPrizeDto> Prizes { get; set; } = new();

    /// <summary>
    /// 剩余道具卡数量
    /// </summary>
    [JsonPropertyName("remaining_cards")]
    public int RemainingCards { get; set; }
}

/// <summary>
/// 道具卡抽奖奖品DTO
/// </summary>
public class ItemCardDrawPrizeDto
{
    /// <summary>
    /// 奖品ID
    /// </summary>
    [JsonPropertyName("id")]
    public int Id { get; set; }

    /// <summary>
    /// 奖品标题
    /// </summary>
    [JsonPropertyName("title")]
    public string Title { get; set; } = string.Empty;

    /// <summary>
    /// 奖品图片
    /// </summary>
    [JsonPropertyName("img_url")]
    public string ImgUrl { get; set; } = string.Empty;

    /// <summary>
    /// 赏品等级ID
    /// </summary>
    [JsonPropertyName("shang_id")]
    public int ShangId { get; set; }

    /// <summary>
    /// 赏品等级标题
    /// </summary>
    [JsonPropertyName("shang_title")]
    public string? ShangTitle { get; set; }

    /// <summary>
    /// 赏品等级颜色
    /// </summary>
    [JsonPropertyName("shang_color")]
    public string? ShangColor { get; set; }

    /// <summary>
    /// 奖品价格
    /// </summary>
    [JsonPropertyName("price")]
    public string Price { get; set; } = "0.00";

    /// <summary>
    /// 回收金额
    /// </summary>
    [JsonPropertyName("sc_money")]
    public string ScMoney { get; set; } = "0.00";

    /// <summary>
    /// 奖品编码
    /// </summary>
    [JsonPropertyName("prize_code")]
    public string PrizeCode { get; set; } = string.Empty;

    /// <summary>
    /// 幸运号码
    /// </summary>
    [JsonPropertyName("luck_no")]
    public int LuckNo { get; set; }
}

#endregion

#region Internal Engine Models

/// <summary>
/// 抽奖请求（内部使用）
/// </summary>
public class LotteryDrawRequest
{
    /// <summary>
    /// 用户ID
    /// </summary>
    public int UserId { get; set; }

    /// <summary>
    /// 商品ID
    /// </summary>
    public int GoodsId { get; set; }

    /// <summary>
    /// 箱号
    /// </summary>
    public int Num { get; set; }

    /// <summary>
    /// 订单ID
    /// </summary>
    public int OrderId { get; set; }

    /// <summary>
    /// 订单号
    /// </summary>
    public string OrderNum { get; set; } = string.Empty;

    /// <summary>
    /// 订单类型：1=一番赏, 2=无限赏, 3=擂台赏, 4=转转赏, 5=福利屋, 6=商城赏, 7=翻倍赏, 8=抽卡机
    /// </summary>
    public int OrderType { get; set; }

    /// <summary>
    /// 来源：0=购买获得, 1=抽奖获得
    /// </summary>
    public int Source { get; set; } = 1;
}

/// <summary>
/// 抽奖结果（内部使用）
/// </summary>
public class LotteryDrawResult
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 错误消息
    /// </summary>
    public string? ErrorMessage { get; set; }

    /// <summary>
    /// 商品奖品ID
    /// </summary>
    public int GoodsItemId { get; set; }

    /// <summary>
    /// 奖品标题
    /// </summary>
    public string Title { get; set; } = string.Empty;

    /// <summary>
    /// 奖品图片
    /// </summary>
    public string ImgUrl { get; set; } = string.Empty;

    /// <summary>
    /// 赏品等级ID
    /// </summary>
    public int ShangId { get; set; }

    /// <summary>
    /// 赏品等级标题
    /// </summary>
    public string ShangTitle { get; set; } = string.Empty;

    /// <summary>
    /// 赏品等级颜色
    /// </summary>
    public string ShangColor { get; set; } = string.Empty;

    /// <summary>
    /// 奖品价格
    /// </summary>
    public decimal Price { get; set; }

    /// <summary>
    /// 回收金额
    /// </summary>
    public decimal ScMoney { get; set; }

    /// <summary>
    /// 奖品编码
    /// </summary>
    public string PrizeCode { get; set; } = string.Empty;

    /// <summary>
    /// 幸运号码
    /// </summary>
    public int LuckNo { get; set; }

    /// <summary>
    /// 抽奖时间
    /// </summary>
    public DateTime DrawTime { get; set; }

    /// <summary>
    /// 订单详情ID
    /// </summary>
    public int OrderItemId { get; set; }

    /// <summary>
    /// 奖品类型：1=实物, 2=虚拟
    /// </summary>
    public int GoodsType { get; set; }

    /// <summary>
    /// 翻倍倍数
    /// </summary>
    public int Doubling { get; set; }

    /// <summary>
    /// 是否灵珠
    /// </summary>
    public int IsLingzhu { get; set; }

    /// <summary>
    /// 父商品奖品ID
    /// </summary>
    public int ParentGoodsListId { get; set; }

    /// <summary>
    /// 宝箱内抽取的奖品列表（当奖品类型为宝箱时）
    /// </summary>
    public List<LotteryDrawResult>? BoxPrizes { get; set; }
}

/// <summary>
/// 奖品概率（内部使用）
/// </summary>
public class PrizeProbability
{
    /// <summary>
    /// 商品奖品ID
    /// </summary>
    public int GoodsItemId { get; set; }

    /// <summary>
    /// 奖品标题
    /// </summary>
    public string Title { get; set; } = string.Empty;

    /// <summary>
    /// 奖品图片
    /// </summary>
    public string ImgUrl { get; set; } = string.Empty;

    /// <summary>
    /// 赏品等级ID
    /// </summary>
    public int ShangId { get; set; }

    /// <summary>
    /// 剩余库存
    /// </summary>
    public int SurplusStock { get; set; }

    /// <summary>
    /// 概率（百分比）
    /// </summary>
    public decimal Probability { get; set; }

    /// <summary>
    /// 权重（用于随机选择）
    /// </summary>
    public double Weight { get; set; }

    /// <summary>
    /// 奖品价格
    /// </summary>
    public decimal Price { get; set; }

    /// <summary>
    /// 回收金额
    /// </summary>
    public decimal ScMoney { get; set; }

    /// <summary>
    /// 奖品类型
    /// </summary>
    public int GoodsType { get; set; }

    /// <summary>
    /// 翻倍倍数
    /// </summary>
    public int Doubling { get; set; }

    /// <summary>
    /// 是否灵珠
    /// </summary>
    public int IsLingzhu { get; set; }

    /// <summary>
    /// 父商品奖品ID
    /// </summary>
    public int ParentGoodsListId { get; set; }

    /// <summary>
    /// 销售时间
    /// </summary>
    public DateTime? SaleTime { get; set; }

    /// <summary>
    /// 奖励ID（用于发放绑定奖励）
    /// </summary>
    public string? RewardId { get; set; }
}

#endregion

#region Constants

/// <summary>
/// 抽奖类型常量
/// </summary>
public static class LotteryOrderType
{
    /// <summary>
    /// 一番赏
    /// </summary>
    public const int YiFanShang = 1;

    /// <summary>
    /// 无限赏
    /// </summary>
    public const int WuXianShang = 2;

    /// <summary>
    /// 擂台赏
    /// </summary>
    public const int LeiTaiShang = 3;

    /// <summary>
    /// 转转赏
    /// </summary>
    public const int ZhuanZhuanShang = 4;

    /// <summary>
    /// 福利屋
    /// </summary>
    public const int FuLiWu = 5;

    /// <summary>
    /// 商城赏
    /// </summary>
    public const int ShangChengShang = 6;

    /// <summary>
    /// 翻倍赏
    /// </summary>
    public const int FanBeiShang = 7;

    /// <summary>
    /// 抽卡机
    /// </summary>
    public const int ChouKaJi = 8;
}

/// <summary>
/// 奖品来源常量
/// </summary>
public static class PrizeSource
{
    /// <summary>
    /// 购买获得
    /// </summary>
    public const int Purchase = 0;

    /// <summary>
    /// 抽奖获得
    /// </summary>
    public const int Lottery = 1;
}

#endregion
