// 命格資料配置
// 機率設定：SSR(1%), SR(9%), R(20%), N(30%), C(40%)

export const FATE_TIERS = {
    SSR: { name: '傳說', probability: 0.01, color: '#ff6b6b' },
    SR: { name: '史詩', probability: 0.09, color: '#9b59b6' },
    R: { name: '稀有', probability: 0.20, color: '#3498db' },
    N: { name: '普通', probability: 0.30, color: '#2ecc71' },
    C: { name: '常見', probability: 0.40, color: '#95a5a6' }
}

export const FATES = [
    // ==================== SSR 級 (傳說) - 10種 ====================
    {
        id: 'mazu_blessing', name: '媽祖庇佑', tier: 'SSR', description: '海上女神降福，逢凶化吉，四海皆平安',
        buffs: { luck: 35, badEventReduction: 0.6, sicknessReduction: 0.7, happinessRecovery: 0.4, resourceGain: 0.3, dropRate: 0.35 }
    },
    {
        id: 'white_tiger_soul', name: '白虎魂', tier: 'SSR', description: '西方神獸降臨，攻守兼備，天下無敵',
        buffs: { attack: 0.5, defense: 0.5, speed: 0.4, strGain: 0.5, healthRegen: 0.4 }
    },
    {
        id: 'guan_gong_spirit', name: '關公顯靈', tier: 'SSR', description: '武聖在世，忠義雙全，戰無不勝',
        buffs: { attack: 0.6, strGain: 0.5, defense: 0.4, luck: 28, badEventReduction: 0.5 }
    },
    {
        id: 'jade_emperor', name: '玉皇大天尊', tier: 'SSR', description: '天界之主加持，萬靈朝拜，福壽綿長',
        buffs: { intGain: 0.6, healthRegen: 0.6, luck: 30, resourceGain: 0.4, gameSuccessRate: 0.5 }
    },
    {
        id: 'dragon_king', name: '青龍王', tier: 'SSR', description: '東方神龍，呼風喚雨，財源滾滾',
        buffs: { resourceGain: 0.6, dropRate: 0.5, luck: 32, speed: 0.4, miniGameBonus: 0.5 }
    },
    {
        id: 'third_prince', name: '三太子附體', tier: 'SSR', description: '哪吒鬧東海，電音三太子本尊降臨',
        buffs: { attack: 0.65, speed: 0.55, luck: 35, strGain: 0.6, miniGameBonus: 0.6 }
    },
    {
        id: 'city_god', name: '城隍爺點將', tier: 'SSR', description: '陰陽兩界都買單，黑白兩道都吃香',
        buffs: { defense: 0.6, badEventReduction: 0.7, luck: 33, resourceGain: 0.5, healthRegen: 0.5 }
    },
    {
        id: 'wangye_patrol', name: '王爺千歲巡境', tier: 'SSR', description: '代天巡狩，瘟疫退散，所向披靡',
        buffs: { sicknessReduction: 0.8, badEventReduction: 0.7, attack: 0.5, defense: 0.5, luck: 30 }
    },
    {
        id: 'zhusheng', name: '註生娘娘加持', tier: 'SSR', description: '送子觀音+註生娘娘雙重保佑，生育率爆表',
        buffs: { breedingSuccess: 0.9, happinessRecovery: 0.6, healthRegen: 0.6, luck: 34 }
    },
    {
        id: 'guanyin', name: '觀音顯靈', tier: 'SSR', description: '大慈大悲救苦救難，千手千眼護佑眾生',
        buffs: { healthRegen: 0.7, sicknessReduction: 0.8, happinessRecovery: 0.7, badEventReduction: 0.7, luck: 38 }
    },

    // ==================== SR 級 (史詩) - 25種 ====================
    {
        id: 'bajiajiang', name: '八家將', tier: 'SR', description: '神將護體，驅邪避凶，威震四方',
        buffs: { attack: 0.4, defense: 0.35, badEventReduction: 0.5, speed: 0.3, luck: 20 }
    },
    {
        id: 'seventh_eighth_lords', name: '七爺八爺', tier: 'SR', description: '黑白無常護佑，轉運奇效，不受病痛',
        buffs: { luck: 28, sicknessReduction: 0.6, healthRegen: 0.4, badEventReduction: 0.4, happinessRecovery: 0.3 }
    },
    {
        id: 'tudi_gong', name: '土地公保庇', tier: 'SR', description: '地方守護神，錢財廣進，日日有餘',
        buffs: { resourceGain: 0.5, dropRate: 0.4, luck: 25, happinessRecovery: 0.3, gameSuccessRate: 0.25 }
    },
    {
        id: 'palanquin_deity', name: '神轎真身', tier: 'SR', description: '乘神轎而行，力大無窮，氣勢如虹',
        buffs: { strGain: 0.45, attack: 0.4, speed: 0.35, defense: 0.3, healthRegen: 0.3 }
    },
    {
        id: 'lion_dance', name: '瑞獅之靈', tier: 'SR', description: '舞獅獻瑞，驅邪納福，喜氣洋洋',
        buffs: { happinessRecovery: 0.45, luck: 22, badEventReduction: 0.4, strGain: 0.3, resourceGain: 0.25 }
    },
    {
        id: 'incense_master', name: '香火鼎盛', tier: 'SR', description: '供奉不斷，神明眷顧，萬事如意',
        buffs: { luck: 26, happinessRecovery: 0.4, sicknessReduction: 0.4, resourceGain: 0.3, intGain: 0.3 }
    },
    {
        id: 'temple_guardian', name: '石獅守護', tier: 'SR', description: '廟口石獅加護，固若金湯，百邪不侵',
        buffs: { defense: 0.5, healthRegen: 0.4, sicknessReduction: 0.45, badEventReduction: 0.35, strGain: 0.3 }
    },
    {
        id: 'firecracker_spirit', name: '炮仔聲', tier: 'SR', description: '鞭炮震天，邪煞退散，運勢亨通',
        buffs: { badEventReduction: 0.5, luck: 24, speed: 0.4, attack: 0.3, happinessRecovery: 0.25 }
    },
    {
        id: 'temple_fair', name: '廟會之子', tier: 'SR', description: '廟會熱鬧，人氣旺盛，好事不斷',
        buffs: { happinessRecovery: 0.4, breedingSuccess: 0.5, resourceGain: 0.3, luck: 20, miniGameBonus: 0.3 }
    },
    {
        id: 'moon_blocks', name: '聖筊連發', tier: 'SR', description: '擲筊必中，神明應允，事事順遂',
        buffs: { gameSuccessRate: 0.45, luck: 27, miniGameBonus: 0.4, resourceGain: 0.25, intGain: 0.35 }
    },
    {
        id: 'sausage_god', name: '大腸包小腸之神', tier: 'SR', description: '夜市第一霸主，香氣迷人，所向披靡',
        buffs: { happinessRecovery: 0.5, hungerDecay: -0.4, resourceGain: 0.35, luck: 24 }
    },
    {
        id: 'fried_chicken', name: '雞排天皇', tier: 'SR', description: '一咬下去會發光的那塊雞排',
        buffs: { attack: 0.45, strGain: 0.4, happinessRecovery: 0.4, hungerDecay: -0.35 }
    },
    {
        id: 'oyster_omelette', name: '蚵仔煎宗師', tier: 'SR', description: '蛋香+蚵香+醬汁，完美三角',
        buffs: { healthRegen: 0.45, happinessRecovery: 0.45, resourceGain: 0.3, strGain: 0.3 }
    },
    {
        id: 'stinky_tofu_king', name: '臭豆腐霸主', tier: 'SR', description: '聞臭吃香，臭到街口轉角還聞得到',
        buffs: { defense: 0.4, attack: 0.35, luck: 22, badEventReduction: 0.35 }
    },
    {
        id: 'shaved_ice', name: '刨冰仙人', tier: 'SR', description: '芒果冰要加三種配料才夠料',
        buffs: { happinessRecovery: 0.5, healthRegen: 0.4, hungerDecay: -0.3, luck: 23 }
    },
    {
        id: 'temple_array', name: '陣頭總幹事', tier: 'SR', description: '宮廟活動永遠少不了他',
        buffs: { speed: 0.45, strGain: 0.4, miniGameBonus: 0.4, luck: 22 }
    },
    {
        id: 'electronic_prince', name: '電音三太子', tier: 'SR', description: 'DJ版三太子，夜市遶境最閃',
        buffs: { speed: 0.5, happinessRecovery: 0.45, miniGameBonus: 0.5, luck: 25 }
    },
    {
        id: 'first_incense', name: '頭香爭奪戰常勝軍', tier: 'SR', description: '除夕半夜就去卡位的那個',
        buffs: { luck: 30, badEventReduction: 0.5, resourceGain: 0.4 }
    },
    {
        id: 'ghost_grabbing', name: '普渡搶孤冠軍', tier: 'SR', description: '搶孤肉搶到手軟',
        buffs: { strGain: 0.5, speed: 0.45, resourceGain: 0.4, luck: 24 }
    },
    {
        id: 'night_market_king', name: '士林夜市劍潭霸主', tier: 'SR', description: '從豪大大雞排排到胡椒餅',
        buffs: { happinessRecovery: 0.45, resourceGain: 0.4, hungerDecay: -0.35 }
    },
    {
        id: 'bubble_tea_master', name: '手搖飲調飲大師', tier: 'SR', description: '半糖去冰少冰加波霸說三遍都不會錯',
        buffs: { intGain: 0.45, miniGameBonus: 0.45, happinessRecovery: 0.4 }
    },
    {
        id: 'temple_medium', name: '乩童本童', tier: 'SR', description: '神明上身，刀槍不入',
        buffs: { defense: 0.55, healthRegen: 0.5, badEventReduction: 0.5 }
    },
    {
        id: 'fengchia_king', name: '逢甲夜市傳人', tier: 'SR', description: '逢甲第一帥氣老闆',
        buffs: { resourceGain: 0.45, dropRate: 0.4, luck: 23 }
    },
    {
        id: 'luwei_queen', name: '滷味攤老闆娘', tier: 'SR', description: '阿姨～這個這個這個都要！',
        buffs: { hungerDecay: -0.45, happinessRecovery: 0.4, resourceGain: 0.35 }
    },
    {
        id: 'sky_lantern', name: '平溪天燈節常勝軍', tier: 'SR', description: '放天燈必中大獎',
        buffs: { luck: 32, dropRate: 0.45, gameSuccessRate: 0.4 }
    },

    // ==================== R 級 (稀有) - 40種 ====================
    // （原本15個 + 新增25個）
    // ...（原本的 R 級 15 個保留，此處省略以節省篇幅，實際使用請保留原本的）
    // 新增的直接接在後面
    {
        id: 'convenience_god', name: '7-11 鎮店之寶', tier: 'R', description: '店員都認識你，咖啡永遠幫你留一杯',
        buffs: { resourceGain: 0.35, happinessRecovery: 0.3, hungerDecay: -0.25, luck: 16 }
    },
    {
        id: 'parking_god', name: '停車格之神', tier: 'R', description: '永遠找得到車位，連百貨公司地下五樓都有',
        buffs: { luck: 20, speed: 0.35, badEventReduction: 0.3 }
    },
    {
        id: 'redline_parking', name: '紅線停車免罰單體質', tier: 'R', description: '停紅線從沒被拖過',
        buffs: { luck: 22, badEventReduction: 0.35 }
    },
    {
        id: 'claw_machine', name: '夾娃娃天后', tier: 'R', description: '一百元可以夾十隻',
        buffs: { miniGameBonus: 0.45, luck: 18, dropRate: 0.35 }
    },
    {
        id: 'delivery_king', name: 'Uber Eats 外送王', tier: 'R', description: '日送百單，雨天加倍',
        buffs: { speed: 0.5, resourceGain: 0.4, strGain: 0.25 }
    },
    {
        id: 'betel_nut_beauty', name: '檳榔西施', tier: 'R', description: '路過的機車都會慢下來',
        buffs: { happinessRecovery: 0.35, breedingSuccess: 0.4, resourceGain: 0.3 }
    },
    {
        id: 'lottery_200', name: '統一發票對中200元常客', tier: 'R', description: '每個月至少中三次200',
        buffs: { resourceGain: 0.35, luck: 18, dropRate: 0.3 }
    },
    {
        id: 'year_end_bonus', name: '尾牙抽中最大獎', tier: 'R', description: 'iPhone、機車、海外旅遊抽不完',
        buffs: { luck: 25, resourceGain: 0.4, dropRate: 0.35 }
    },
    {
        id: 'morning_market', name: '傳統市場殺價之神', tier: 'R', description: '老闆娘看到你就自動降價',
        buffs: { resourceGain: 0.4, intGain: 0.35, luck: 15 }
    },
    {
        id: 'koifish', name: '錦鯉附體', tier: 'R', description: '朋友圈轉發必中獎',
        buffs: { luck: 24, dropRate: 0.35, gameSuccessRate: 0.3 }
    },

    // ==================== N 級 (普通) - 35種 ====================
    // （原本15個 + 新增20個，省略原本，新增直接接上）
    {
        id: 'soymilk', name: '每天豆漿燒餅', tier: 'N', description: '早餐永遠這一套',
        buffs: { healthRegen: 0.2, strGain: 0.18, happinessRecovery: 0.15 }
    },
    {
        id: 'train_bento', name: '台鐵便當收集狂', tier: 'N', description: '排骨、雞腿、魚排都吃過',
        buffs: { hungerDecay: -0.25, happinessRecovery: 0.2 }
    },
    {
        id: 'instant_noodle', name: '泡麵加蛋加蔥加火腿', tier: 'N', description: '宵夜基本配備',
        buffs: { hungerDecay: -0.3, happinessRecovery: 0.18 }
    },
    {
        id: 'spicy_chicken', name: '鹹酥雞一定要辣', tier: 'N', description: '不辣等於沒吃',
        buffs: { attack: 0.2, happinessRecovery: 0.2 }
    },
    {
        id: 'half_sugar', name: '珍奶半糖少冰', tier: 'N', description: '點飲料永遠這一句',
        buffs: { happinessRecovery: 0.22, hungerDecay: -0.18 }
    },

    // ==================== C 級 (常見) - 15種 ====================
    {
        id: 'ordinary_life', name: '平凡人生', tier: 'C', description: '平平淡淡才是真',
        buffs: { luck: 5, happinessRecovery: 0.1 }
    },
    {
        id: 'unlucky_star', name: '歹運連連', tier: 'C', description: '運氣不好但吃都吃不飽',
        buffs: { luck: -5, hungerDecay: -0.2, healthRegen: 0.15 }
    },
    {
        id: 'lazy_homebody', name: '宅在家', tier: 'C', description: '能躺就不坐',
        buffs: { strGain: -0.12, happinessRecovery: 0.18, hungerDecay: -0.15 }
    },
    {
        id: 'traffic_jam', name: '永遠在塞車', tier: 'C', description: '國道一號忠實顧客',
        buffs: { speed: -0.2, badEventReduction: -0.1 }
    },
    {
        id: 'no_umbrella', name: '雨傘永遠忘記帶', tier: 'C', description: '出門看天氣永遠看錯',
        buffs: { luck: -8, sicknessReduction: -0.15 }
    },
    {
        id: 'no_typhoon_day', name: '颱風假永遠不放', tier: 'C', description: '公司最硬',
        buffs: { happinessRecovery: -0.1 }
    },
    {
        id: 'late_king', name: '遲到大王', tier: 'C', description: '永遠差五分鐘',
        buffs: { speed: 0.15, luck: -10 }
    },
    {
        id: 'phone_no_battery', name: '手機永遠沒電', tier: 'C', description: '出門必忘充電',
        buffs: { intGain: -0.1 }
    },
    {
        id: 'taiwanese', name: '台灣人', tier: 'C', description: '最普遍的命格，啥都不加',
        buffs: {}
    }
];

// 根據機率隨機抽取命格
export function getRandomFate() {
    const rand = Math.random()
    let cumulativeProbability = 0

    // 按照稀有度排序抽取
    const tiers = ['SSR', 'SR', 'R', 'N', 'C']

    for (const tier of tiers) {
        cumulativeProbability += FATE_TIERS[tier].probability
        if (rand < cumulativeProbability) {
            // 從該等級中隨機選一個
            const fatesInTier = FATES.filter(f => f.tier === tier)
            return fatesInTier[Math.floor(Math.random() * fatesInTier.length)]
        }
    }

    // 兜底返回普通命格
    return FATES.find(f => f.id === 'taiwanese')
}

// 獲取命格顏色
export function getFateColor(tier) {
    return FATE_TIERS[tier]?.color || '#95a5a6'
}

// 獲取指定等級的所有命格
export function getFatesByTier(tier) {
    return FATES.filter(f => f.tier === tier)
}
