using Microsoft.Extensions.Logging;
using Volo.Abp.Domain.Services;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.SqlSugarCore.Abstractions;

namespace Yi.Framework.AiHub.Domain.Managers;

/// <summary>
/// 翻牌管理器 - 负责翻牌核心业务逻辑
/// </summary>
public class CardFlipManager : DomainService
{
    private readonly ISqlSugarRepository<CardFlipTaskAggregateRoot> _cardFlipTaskRepository;
    private readonly ISqlSugarRepository<InvitationRecordAggregateRoot> _invitationRecordRepository;
    private readonly InviteCodeManager _inviteCodeManager;
    private readonly ILogger<CardFlipManager> _logger;

    // 翻牌规则配置
    public const int MAX_FREE_FLIPS = 7;        // 免费翻牌次数
    public const int MAX_INVITE_FLIPS = 3;      // 邀请解锁翻牌次数
    public const int TOTAL_MAX_FLIPS = 10;      // 总最大翻牌次数

    private const int EIGHTH_FLIP = 8;           // 第8次翻牌
    private const int NINTH_FLIP = 9;            // 第9次翻牌
    private const int TENTH_FLIP = 10;           // 第10次翻牌

    // 前7次免费翻牌奖励配置
    private const long FREE_MIN_REWARD = 10000;       // 前7次最小奖励 1w
    private const long FREE_MAX_REWARD = 30000;       // 前7次最大奖励 3w
    private const double FREE_WIN_RATE = 0.5;         // 前7次中奖概率 50%

    private const long EIGHTH_MIN_REWARD = 1000000;   // 第8次最小奖励 100w
    private const long EIGHTH_MAX_REWARD = 3000000;   // 第8次最大奖励 300w
    private const long NINTH_MIN_REWARD = 1000000;    // 第9次最小奖励 100w
    private const long NINTH_MAX_REWARD = 5000000;    // 第9次最大奖励 500w
    private const long TENTH_MIN_REWARD = 1000000;    // 第10次最小奖励 100w
    private const long TENTH_MAX_REWARD = 10000000;   // 第10次最大奖励 1000w

    public CardFlipManager(
        ISqlSugarRepository<CardFlipTaskAggregateRoot> cardFlipTaskRepository,
        ISqlSugarRepository<InvitationRecordAggregateRoot> invitationRecordRepository,
        InviteCodeManager inviteCodeManager,
        ILogger<CardFlipManager> logger)
    {
        _cardFlipTaskRepository = cardFlipTaskRepository;
        _invitationRecordRepository = invitationRecordRepository;
        _inviteCodeManager = inviteCodeManager;
        _logger = logger;
    }

    /// <summary>
    /// 获取或创建本周任务
    /// </summary>
    public async Task<CardFlipTaskAggregateRoot?> GetOrCreateWeeklyTaskAsync(
        Guid userId,
        DateTime weekStart,
        bool createIfNotExists)
    {
        var task = await _cardFlipTaskRepository._DbQueryable
            .Where(x => x.UserId == userId && x.WeekStartDate == weekStart)
            .FirstAsync();

        if (task == null && createIfNotExists)
        {
            task = new CardFlipTaskAggregateRoot(userId, weekStart);
            await _cardFlipTaskRepository.InsertAsync(task);
        }

        return task;
    }

    /// <summary>
    /// 获取已翻牌的顺序列表
    /// </summary>
    public List<int> GetFlippedOrder(CardFlipTaskAggregateRoot task)
    {
        return task.FlippedOrder ?? new List<int>();
    }

    /// <summary>
    /// 执行翻牌逻辑
    /// </summary>
    /// <param name="userId">用户ID</param>
    /// <param name="flipNumber">翻牌序号</param>
    /// <param name="weekStart">本周开始日期</param>
    /// <returns>翻牌结果</returns>
    public async Task<FlipResult> ExecuteFlipAsync(Guid userId, int flipNumber, DateTime weekStart)
    {
        // 验证翻牌序号
        if (flipNumber < 1 || flipNumber > TOTAL_MAX_FLIPS)
        {
            throw new UserFriendlyException($"翻牌序号必须在1-{TOTAL_MAX_FLIPS}之间");
        }

        // 获取或创建本周任务
        var task = await GetOrCreateWeeklyTaskAsync(userId, weekStart, createIfNotExists: true);

        // 验证翻牌次数
        if (task.TotalFlips >= TOTAL_MAX_FLIPS)
        {
            throw new UserFriendlyException("本周翻牌次数已用完,请下周再来!");
        }

        // 验证该牌是否已经翻过
        var flippedOrder = GetFlippedOrder(task);
        if (flippedOrder.Contains(flipNumber))
        {
            throw new UserFriendlyException($"第 {flipNumber} 号牌已经翻过了!");
        }

        // 判断翻牌类型
        var flipType = DetermineFlipType(task);

        // 验证是否有足够的次数
        if (!CanUseFlipType(task, flipType))
        {
            throw new UserFriendlyException(GetFlipTypeErrorMessage(flipType));
        }

        // 如果是邀请类型翻牌，必须验证用户本周的邀请记录数量足够（包括填写别人的邀请码和别人填写我的邀请码）
        if (flipType == FlipType.Invite)
        {
            // 查询本周作为邀请人或被邀请人的记录数量（双方都会增加翻牌次数）
            var weeklyInviteRecordCount = await _invitationRecordRepository._DbQueryable
                .Where(x => x.InviterId == userId || x.InvitedUserId == userId)
                .Where(x => x.InvitationTime >= weekStart)
                .CountAsync();

            // 本周邀请记录数量必须 >= 即将使用的邀请翻牌次数
            // 例如: 要翻第8次(InviteFlipsUsed=0->1), 需要至少有1条邀请记录（我邀请别人或别人邀请我）
            //      要翻第9次(InviteFlipsUsed=1->2), 需要至少有2条邀请记录
            //      要翻第10次(InviteFlipsUsed=2->3), 需要至少有3条邀请记录
            var requiredInviteRecordCount = task.InviteFlipsUsed + 1;
            if (weeklyInviteRecordCount < requiredInviteRecordCount)
            {
                throw new UserFriendlyException($"需本周累积{requiredInviteRecordCount}次邀请记录（填写别人的邀请码或别人填写你的邀请码）才能解锁第{task.TotalFlips + 1}次翻牌");
            }
        }

        // 计算翻牌结果（基于当前是第几次翻牌，而不是卡片序号）
        var flipCount = task.TotalFlips + 1; // 当前这次翻牌是第几次
        var result = CalculateFlipResult(flipCount);

        // 将卡片序号信息也返回
        result.FlipNumber = flipNumber;

        // 更新翻牌次数（必须在记录奖励之前，因为需要先确定是第几次）
        task.IncrementFlip(flipType);

        // 记录翻牌顺序
        if (task.FlippedOrder == null)
        {
            task.FlippedOrder = new List<int>();
        }
        task.FlippedOrder.Add(flipNumber);

        // 如果中奖，记录奖励金额（用于后续查询显示）
        if (result.IsWin)
        {
            task.SetWinReward(flipCount, result.RewardAmount);
        }

        await _cardFlipTaskRepository.UpdateAsync(task);

        _logger.LogInformation($"用户 {userId} 完成第 {flipNumber} 次翻牌,中奖:{result.IsWin}");

        return result;
    }

    /// <summary>
    /// 判断是否可以翻牌
    /// </summary>
    public bool CanFlipCard(CardFlipTaskAggregateRoot? task)
    {
        if (task == null) return true; // 没有任务记录,可以开始翻牌

        return task.TotalFlips < TOTAL_MAX_FLIPS;
    }

    /// <summary>
    /// 判断翻牌类型
    /// </summary>
    public FlipType DetermineFlipType(CardFlipTaskAggregateRoot task)
    {
        if (task.FreeFlipsUsed < MAX_FREE_FLIPS)
        {
            return FlipType.Free;
        }
        else
        {
            return FlipType.Invite;
        }
    }

    /// <summary>
    /// 判断是否可以使用该翻牌类型
    /// </summary>
    public bool CanUseFlipType(CardFlipTaskAggregateRoot task, FlipType flipType)
    {
        return flipType switch
        {
            FlipType.Free => task.FreeFlipsUsed < MAX_FREE_FLIPS,
            FlipType.Invite => task.InviteFlipsUsed < MAX_INVITE_FLIPS,
            _ => false
        };
    }

    /// <summary>
    /// 计算翻牌结果
    /// </summary>
    /// <param name="flipCount">第几次翻牌（1-10）</param>
    private FlipResult CalculateFlipResult(int flipCount)
    {
        var result = new FlipResult
        {
            FlipNumber = 0, // 稍后会被设置为实际的卡片序号
            IsWin = false
        };

        var random = new Random();

        // 前7次: 50%概率中奖，奖励1w-3w
        if (flipCount <= 7)
        {
            // 50%概率中奖
            if (random.NextDouble() < FREE_WIN_RATE)
            {
                var rewardAmount = GenerateRandomReward(FREE_MIN_REWARD, FREE_MAX_REWARD);
                result.IsWin = true;
                result.RewardAmount = rewardAmount;
                result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000m:0.##}w tokens!";
            }
            else
            {
                result.IsWin = false;
                result.RewardDesc = "很遗憾,未中奖";
            }
        }
        // 第8次中奖 (邀请码解锁)
        else if (flipCount == EIGHTH_FLIP)
        {
            var rewardAmount = GenerateRandomReward(EIGHTH_MIN_REWARD, EIGHTH_MAX_REWARD);
            result.IsWin = true;
            result.RewardAmount = rewardAmount;
            result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000}w tokens!";
        }
        // 第9次中奖 (邀请码解锁)
        else if (flipCount == NINTH_FLIP)
        {
            var rewardAmount = GenerateRandomReward(NINTH_MIN_REWARD, NINTH_MAX_REWARD);
            result.IsWin = true;
            result.RewardAmount = rewardAmount;
            result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000}w tokens!";
        }
        // 第10次中奖 (邀请码解锁)
        else if (flipCount == TENTH_FLIP)
        {
            var rewardAmount = GenerateRandomReward(TENTH_MIN_REWARD, TENTH_MAX_REWARD);
            result.IsWin = true;
            result.RewardAmount = rewardAmount;
            result.RewardDesc = $"恭喜获得尊享包 {rewardAmount / 10000}w tokens!";
        }

        return result;
    }

    /// <summary>
    /// 获取翻牌类型错误提示
    /// </summary>
    private string GetFlipTypeErrorMessage(FlipType flipType)
    {
        return flipType switch
        {
            FlipType.Free => "免费翻牌次数已用完",
            FlipType.Invite => "需要使用邀请码解锁更多次数",
            _ => "无法翻牌"
        };
    }

    /// <summary>
    /// 生成随机奖励金额
    /// </summary>
    private long GenerateRandomReward(long min, long max)
    {
        var random = new Random();

        // 根据最小值判断单位
        // 如果min小于100000，则使用1w(10000)作为单位；否则使用100w(1000000)作为单位
        long unit = min < 100000 ? 10000 : 1000000;

        // 将min和max转换为单位的倍数
        long minUnits = min / unit;
        long maxUnits = max / unit;

        // 在倍数范围内随机
        long randomUnits = random.Next((int)minUnits, (int)maxUnits + 1);

        // 返回单位的倍数
        return randomUnits * unit;
    }

    /// <summary>
    /// 获取本周开始日期(周一)
    /// </summary>
    public static DateTime GetWeekStartDate(DateTime date)
    {
        var dayOfWeek = (int)date.DayOfWeek;
        // 将周日(0)转换为7
        if (dayOfWeek == 0) dayOfWeek = 7;

        // 计算本周一的日期
        var monday = date.Date.AddDays(-(dayOfWeek - 1));
        return monday;
    }

    /// <summary>
    /// 获取翻牌类型描述
    /// </summary>
    public static string GetFlipTypeDesc(int flipNumber)
    {
        if (flipNumber <= MAX_FREE_FLIPS)
        {
            return "免费";
        }
        else
        {
            return "邀请解锁";
        }
    }
}

/// <summary>
/// 翻牌结果
/// </summary>
public class FlipResult
{
    /// <summary>
    /// 翻牌序号
    /// </summary>
    public int FlipNumber { get; set; }

    /// <summary>
    /// 是否中奖
    /// </summary>
    public bool IsWin { get; set; }

    /// <summary>
    /// 奖励金额
    /// </summary>
    public long RewardAmount { get; set; }

    /// <summary>
    /// 奖励描述
    /// </summary>
    public string RewardDesc { get; set; } = string.Empty;
}
