namespace DramaLing.Api.Services;

/// <summary>
/// CEFR等級映射服務 - 將CEFR等級轉換為詞彙難度數值
/// </summary>
public static class CEFRMappingService
{
    private static readonly Dictionary<string, int> CEFRToWordLevel = new()
    {
        { "A1", 20 },  // 基礎詞彙 (1-1000常用詞)
        { "A2", 35 },  // 常用詞彙 (1001-3000詞)
        { "B1", 50 },  // 中級詞彙 (3001-6000詞)
        { "B2", 65 },  // 中高級詞彙 (6001-12000詞)
        { "C1", 80 },  // 高級詞彙 (12001-20000詞)
        { "C2", 95 }   // 精通詞彙 (20000+詞)
    };

    private static readonly Dictionary<int, string> WordLevelToCEFR = new()
    {
        { 20, "A1" }, { 35, "A2" }, { 50, "B1" },
        { 65, "B2" }, { 80, "C1" }, { 95, "C2" }
    };

    /// <summary>
    /// 根據CEFR等級獲取詞彙難度數值
    /// </summary>
    /// <param name="cefrLevel">CEFR等級 (A1-C2)</param>
    /// <returns>詞彙難度 (1-100)</returns>
    public static int GetWordLevel(string? cefrLevel)
    {
        if (string.IsNullOrEmpty(cefrLevel))
            return 50; // 預設B1級別

        return CEFRToWordLevel.GetValueOrDefault(cefrLevel.ToUpperInvariant(), 50);
    }

    /// <summary>
    /// 根據詞彙難度數值獲取CEFR等級
    /// </summary>
    /// <param name="wordLevel">詞彙難度 (1-100)</param>
    /// <returns>對應的CEFR等級</returns>
    public static string GetCEFRLevel(int wordLevel)
    {
        // 找到最接近的CEFR等級
        var closestLevel = WordLevelToCEFR.Keys
            .OrderBy(level => Math.Abs(level - wordLevel))
            .First();

        return WordLevelToCEFR[closestLevel];
    }

    /// <summary>
    /// 獲取新用戶的預設程度
    /// </summary>
    /// <returns>預設用戶程度 (50 = B1級別)</returns>
    public static int GetDefaultUserLevel() => 50;

    /// <summary>
    /// 判斷是否為A1學習者
    /// </summary>
    /// <param name="userLevel">學習者程度</param>
    /// <returns>是否為A1學習者</returns>
    public static bool IsA1Learner(int userLevel) => userLevel <= 20;

    /// <summary>
    /// 獲取學習者程度描述
    /// </summary>
    /// <param name="userLevel">學習者程度 (1-100)</param>
    /// <returns>程度描述</returns>
    public static string GetUserLevelDescription(int userLevel)
    {
        return userLevel switch
        {
            <= 20 => "A1 - 初學者",
            <= 35 => "A2 - 基礎",
            <= 50 => "B1 - 中級",
            <= 65 => "B2 - 中高級",
            <= 80 => "C1 - 高級",
            _ => "C2 - 精通"
        };
    }

    /// <summary>
    /// 根據詞彙使用頻率估算難度 (未來擴展用)
    /// </summary>
    /// <param name="frequency">詞彙頻率排名</param>
    /// <returns>估算的詞彙難度</returns>
    public static int EstimateWordLevelByFrequency(int frequency)
    {
        return frequency switch
        {
            <= 1000 => 20,    // 最常用1000詞 → A1
            <= 3000 => 35,    // 常用3000詞 → A2
            <= 6000 => 50,    // 中級6000詞 → B1
            <= 12000 => 65,   // 中高級12000詞 → B2
            <= 20000 => 80,   // 高級20000詞 → C1
            _ => 95           // 超過20000詞 → C2
        };
    }

    /// <summary>
    /// 獲取所有CEFR等級列表
    /// </summary>
    /// <returns>CEFR等級數組</returns>
    public static string[] GetAllCEFRLevels() => new[] { "A1", "A2", "B1", "B2", "C1", "C2" };

    /// <summary>
    /// 驗證CEFR等級是否有效
    /// </summary>
    /// <param name="cefrLevel">要驗證的CEFR等級</param>
    /// <returns>是否有效</returns>
    public static bool IsValidCEFRLevel(string? cefrLevel)
    {
        if (string.IsNullOrEmpty(cefrLevel))
            return false;

        return CEFRToWordLevel.ContainsKey(cefrLevel.ToUpperInvariant());
    }
}