using System;

namespace DramaLing.Api.Services;

public static class CEFRLevelService
{
    private static readonly string[] Levels = { "A1", "A2", "B1", "B2", "C1", "C2" };

    /// <summary>
    /// 取得 CEFR 等級的數字索引
    /// </summary>
    public static int GetLevelIndex(string level)
    {
        if (string.IsNullOrEmpty(level)) return 1; // 預設 A2
        return Array.IndexOf(Levels, level.ToUpper());
    }

    /// <summary>
    /// 判定詞彙對特定用戶是否為高價值
    /// 規則：比用戶程度高 1-2 級的詞彙為高價值
    /// </summary>
    public static bool IsHighValueForUser(string wordLevel, string userLevel)
    {
        var userIndex = GetLevelIndex(userLevel);
        var wordIndex = GetLevelIndex(wordLevel);

        // 無效等級處理
        if (userIndex == -1 || wordIndex == -1) return false;

        // 高價值 = 比用戶程度高 1-2 級
        return wordIndex >= userIndex + 1 && wordIndex <= userIndex + 2;
    }

    /// <summary>
    /// 取得用戶的目標學習等級範圍
    /// </summary>
    public static string GetTargetLevelRange(string userLevel)
    {
        var userIndex = GetLevelIndex(userLevel);
        if (userIndex == -1) return "B1-B2";

        var targetMin = Levels[Math.Min(userIndex + 1, Levels.Length - 1)];
        var targetMax = Levels[Math.Min(userIndex + 2, Levels.Length - 1)];

        return targetMin == targetMax ? targetMin : $"{targetMin}-{targetMax}";
    }

    /// <summary>
    /// 取得下一個等級
    /// </summary>
    public static string GetNextLevel(string currentLevel, int steps = 1)
    {
        var currentIndex = GetLevelIndex(currentLevel);
        if (currentIndex == -1) return "B1";

        var nextIndex = Math.Min(currentIndex + steps, Levels.Length - 1);
        return Levels[nextIndex];
    }
}