from data.exercises import EXERCISES_DB

def generate_workout_program(user_data: dict) -> dict:
    place = user_data.get("training_place", "خانه بدون وسیله")
    activity = user_data.get("activity_level", "کم")
    goal = user_data.get("goal", "مردانه‌سازی هیکل")
    injury = user_data.get("injury")

    if "بدون" in place: place_key = "home_no_equip"
    elif "کش" in place or "دمبل" in place: place_key = "home_with_equip"
    else: place_key = "gym"

    available_exercises = {ex_id: ex for ex_id, ex in EXERCISES_DB.items() if ex.get(place_key, False)}

    if injury and injury != "ندارم":
        if "کمر" in injury or "زانو" in injury:
            available_exercises.pop("bodyweight_squat", None)
        if "شانه" in injury:
            for ex in ["pike_pushup", "overhead_press", "pushup", "diamond_pushup"]:
                available_exercises.pop(ex, None)

    rest_note = "⏱ استراحت بین ست‌ها: ۹۰-۱۲۰ ثانیه" if activity == "کم" else "⏱ استراحت بین ست‌ها: ۶۰-۹۰ ثانیه"

    def _p1():
        p = ["wall_angel", "knee_pushup", "plank", "bodyweight_squat", "superman"]
        if place_key in ("home_with_equip", "gym"): p.extend(["band_pulldown", "face_pull_band"])
        return [x for x in p if x in available_exercises]

    def _p2():
        p = ["wall_angel", "pushup", "pike_pushup", "inverted_row", "plank", "bodyweight_squat", "superman"]
        if place_key in ("home_with_equip", "gym"): p.extend(["lateral_raise_band", "band_pulldown", "face_pull_band", "band_curl"])
        if place_key == "gym": p.append("dead_hang")
        return [x for x in p if x in available_exercises]

    def _p3():
        p = ["wall_angel", "pushup", "pike_pushup", "diamond_pushup", "inverted_row", "plank", "bodyweight_squat", "superman"]
        if place_key in ("home_with_equip", "gym"): p.extend(["lateral_raise_band", "band_pulldown", "face_pull_band", "band_curl", "dead_hang", "overhead_press"])
        if place_key == "gym": p.append("negative_pullup")
        return [x for x in p if x in available_exercises]

    def _p4():
        return list(available_exercises.keys())

    return {
        "place": place, "goal": goal, "rest_note": rest_note,
        "phases": {
            "week_1": {"name": "📅 فاز ۱ — هفته ۱ و ۲ (آشنایی)", "description": f"🎯 هدف: یادگیری فرم صحیح | ۳ روز در هفته\n{rest_note}", "exercise_ids": _p1()},
            "week_2": {"name": "📅 فاز ۲ — هفته ۳ و ۴ (پایه)", "description": f"🎯 هدف: افزایش حجم تمرین | ۳-۴ روز در هفته\n{rest_note}", "exercise_ids": _p2()},
            "week_3": {"name": "📅 فاز ۳ — هفته ۵ و ۶ (قدرت)", "description": f"🎯 هدف: توسعه قدرت | ۴ روز در هفته\n{rest_note}", "exercise_ids": _p3()},
            "week_4": {"name": "📅 فاز ۴ — هفته ۷ و ۸ (پیشرفت)", "description": f"🎯 هدف: چالش نهایی | ۴-۵ روز در هفته\n{rest_note}", "exercise_ids": _p4()},
        }
    }

def format_phase_text(phase: dict) -> str:
    text = f"<b>{phase['name']}</b>\n\n{phase['description']}\n\n━━━━━━━━━━━━━━━━━━━━\n<b>📋 حرکات این فاز:</b>\n\n"
    for i, ex_id in enumerate(phase['exercise_ids'], 1):
        ex = EXERCISES_DB.get(ex_id)
        if ex:
            text += f"  {i}. {ex['name_fa']}\n      🎯 {ex['muscles'].split(',')[0]}\n\n"
    text += "━━━━━━━━━━━━━━━━━━━━\n👇 روی دکمه هر حرکت کلیک کن:"
    return text
