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];
    }

    /// <summary>
    /// 取得所有有效的CEFR等級
    /// </summary>
    /// <returns>CEFR等級數組</returns>
    public static string[] GetAllLevels()
    {
        return (string[])Levels.Clone();
    }

    /// <summary>
    /// 驗證CEFR等級是否有效
    /// </summary>
    /// <param name="level">要驗證的等級</param>
    /// <returns>是否為有效等級</returns>
    public static bool IsValidLevel(string level)
    {
        return !string.IsNullOrEmpty(level) &&
               Array.IndexOf(Levels, level.ToUpper()) != -1;
    }

    /// <summary>
    /// 取得等級的描述
    /// </summary>
    /// <param name="level">CEFR等級</param>
    /// <returns>等級描述</returns>
    public static string GetLevelDescription(string level)
    {
        return level.ToUpper() switch
        {
            "A1" => "初學者 - 能理解基本詞彙和簡單句子",
            "A2" => "基礎 - 能處理日常對話和常見主題",
            "B1" => "中級 - 能理解清楚標準語言的要點",
            "B2" => "中高級 - 能理解複雜文本的主要內容",
            "C1" => "高級 - 能流利表達，理解含蓄意思",
            "C2" => "精通 - 接近母語水平",
            _ => "未知等級"
        };
    }

    /// <summary>
    /// 取得等級的範例詞彙
    /// </summary>
    /// <param name="level">CEFR等級</param>
    /// <returns>範例詞彙數組</returns>
    public static string[] GetLevelExamples(string level)
    {
        return level.ToUpper() switch
        {
            "A1" => new[] { "hello", "good", "house", "eat", "happy" },
            "A2" => new[] { "important", "difficult", "interesting", "beautiful", "understand" },
            "B1" => new[] { "analyze", "opportunity", "environment", "responsibility", "development" },
            "B2" => new[] { "sophisticated", "implications", "comprehensive", "substantial", "methodology" },
            "C1" => new[] { "meticulous", "predominantly", "intricate", "corroborate", "paradigm" },
            "C2" => new[] { "ubiquitous", "ephemeral", "perspicacious", "multifarious", "idiosyncratic" },
            _ => new[] { "example" }
        };
    }
}