"""User keyboard definitions."""

from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from utils.helpers import gregorian_to_persian


def get_start_keyboard() -> InlineKeyboardMarkup:
    """Keyboard for start command."""
    return InlineKeyboardMarkup(
        inline_keyboard=[
            [InlineKeyboardButton(text="شروع مسابقه 🚀", callback_data="start_registration")]
        ]
    )


def get_channel_keyboard() -> InlineKeyboardMarkup:
    """Keyboard for channel membership."""
    return InlineKeyboardMarkup(
        inline_keyboard=[
            [
                InlineKeyboardButton(text="عضویت در کانال 📢", url="https://t.me/GisouAcademy"),
                InlineKeyboardButton(text="بررسی عضویت ✅", callback_data="check_channel_membership")
            ]
        ]
    )


def get_role_keyboard() -> InlineKeyboardMarkup:
    """Keyboard for role selection."""
    return InlineKeyboardMarkup(
        inline_keyboard=[
            [
                InlineKeyboardButton(text="🦷 دندانپزشک", callback_data="role_dentist"),
                InlineKeyboardButton(text="✏️ دانشجو", callback_data="role_student")
            ]
        ]
    )


from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.enums.button_style import ButtonStyle  # ایمپورت استایل‌ها

def get_main_menu_keyboard() -> InlineKeyboardMarkup:
    """Main menu keyboard for registered users."""
    return InlineKeyboardMarkup(
        inline_keyboard=[
            [InlineKeyboardButton(text="⚽ پیش بینی مسابقات", callback_data="predict_matches")],
            [InlineKeyboardButton(text="🎁 جوایز", callback_data="rewards")],
            [InlineKeyboardButton(text="👤 امتیاز من", callback_data="my_score")],
            [InlineKeyboardButton(text="📈 جدول رتبه بندی", callback_data="ranking")],
            [InlineKeyboardButton(text="👥 دعوت دوستان", callback_data="referral")],
            [InlineKeyboardButton(text="📃 قوانین امتیازدهی", callback_data="scoring_rules")]
        ]
    )


def get_upcoming_matches_keyboard(matches: list[dict]) -> InlineKeyboardMarkup:
    """Keyboard with upcoming matches list - improved display."""
    keyboard = []
    for idx, match in enumerate(matches, 1):
        team1 = match.get("team1", "")
        team2 = match.get("team2", "")
        team1_flag = match.get("team1_flag", "")
        team2_flag = match.get("team2_flag", "")
        match_time = match.get("match_time", "")
        match_id = match.get("id", "")

        time_str = gregorian_to_persian(match_time) if match_time else ""
        
        # گزینه 1: دو خطی (نام مسابقه و زمان در دو خط جدا)
        button_text = f"{idx}. {team1_flag} {team1} 🆚 {team2} {team2_flag}"
        
        # گزینه 2: یک خطی با جداکننده زیبا (فعال کردن این خط و کامنت خط بالا)
        # button_text = f"{idx}. {team1_flag} {team1} vs {team2} {team2_flag} | ⏰ {time_str}"
        
        keyboard.append([InlineKeyboardButton(text=button_text, callback_data=f"match_{match_id}")])

    keyboard.append([InlineKeyboardButton(text="🔙 بازگشت", callback_data="back_to_menu")])
    return InlineKeyboardMarkup(inline_keyboard=keyboard)


def get_prediction_type_keyboard(match_id: int) -> InlineKeyboardMarkup:
    """Keyboard for selecting prediction type (win/lose or exact score)."""
    return InlineKeyboardMarkup(
        inline_keyboard=[
            [InlineKeyboardButton(text="🔮 پیش‌بینی نتیجه (برد/مساوی/باخت)", callback_data=f"predict_result_{match_id}")],
            [InlineKeyboardButton(text="⚽ پیش‌بینی گل (دقیق)", callback_data=f"predict_score_{match_id}")],
            [InlineKeyboardButton(text="بازگشت", callback_data="back_to_menu")]
        ]
    )


def get_result_prediction_keyboard(team1: str, team2: str) -> InlineKeyboardMarkup:
    """Keyboard for result prediction with actual team names."""
    return InlineKeyboardMarkup(
        inline_keyboard=[
            [InlineKeyboardButton(text=f"🏆 برد {team1}", callback_data="result_team1_win")],
            [InlineKeyboardButton(text="🤝 مساوی", callback_data="result_draw")],
            [InlineKeyboardButton(text=f"🏆 برد {team2}", callback_data="result_team2_win")],
            [InlineKeyboardButton(text="🔙 بازگشت", callback_data="back_to_menu")]
        ]
    )


def get_back_to_menu_keyboard() -> InlineKeyboardMarkup:
    """Keyboard with back to menu button."""
    return InlineKeyboardMarkup(
        inline_keyboard=[
            [InlineKeyboardButton(text="بازگشت", callback_data="back_to_menu")]
        ]
    )


def get_referral_keyboard(referral_code: str, bot_username: str) -> InlineKeyboardMarkup:
    """Keyboard with referral link."""
    referral_link = f"https://t.me/{bot_username}?start={referral_code}"
    return InlineKeyboardMarkup(
        inline_keyboard=[
            [InlineKeyboardButton(text="📋 کپی لینک دعوت", switch_inline_query=referral_link)],
            [InlineKeyboardButton(text="بازگشت", callback_data="back_to_menu")]
        ]
    )