"""Persian idioms, expressions, and social conventions."""

from ai import ask

# Built-in collection of common Persian expressions and idioms
EXPRESSIONS = [
    {
        "persian": "سلام علیکم",
        "finglish": "salâm aleykom",
        "english": "Peace be upon you (formal greeting)",
        "context": "Formal greeting, especially with elders",
    },
    {
        "persian": "خسته نباشید",
        "finglish": "khaste nabâshid",
        "english": "May you not be tired",
        "context": "Common greeting to someone who has been working. Used as 'hello' in shops, offices, etc.",
    },
    {
        "persian": "دستت درد نکنه",
        "finglish": "dastet dard nakone",
        "english": "May your hand not hurt",
        "context": "Thank you for your effort (after someone does something for you)",
    },
    {
        "persian": "قابلی نداره",
        "finglish": "ghâbeli nadâre",
        "english": "It's not worthy (of you)",
        "context": "You're welcome / Don't mention it — said when giving a gift or doing a favour",
    },
    {
        "persian": "تعارف نکن",
        "finglish": "ta'ârof nakon",
        "english": "Don't do ta'arof",
        "context": "Stop being politely modest — please accept! Part of Persian ta'arof culture.",
    },
    {
        "persian": "نوش جان",
        "finglish": "nush-e jân",
        "english": "May it nourish your soul",
        "context": "Said to someone eating — like 'bon appétit' or 'enjoy your meal'",
    },
    {
        "persian": "چشمت روز بد نبینه",
        "finglish": "cheshmet ruz-e bad nabine",
        "english": "May your eyes never see a bad day",
        "context": "A warm wish for someone's wellbeing",
    },
    {
        "persian": "قدمت روی چشم",
        "finglish": "ghadamet ru-ye cheshm",
        "english": "Your step is on my eye",
        "context": "Warm welcome — 'you're very welcome here'. Extremely hospitable expression.",
    },
    {
        "persian": "ان‌شاءالله",
        "finglish": "inshâ'allâh",
        "english": "God willing",
        "context": "Used when talking about future plans. Very common in daily speech.",
    },
    {
        "persian": "ماشاءالله",
        "finglish": "mâshâ'allâh",
        "english": "What God has willed",
        "context": "Expression of admiration or praise, also used to ward off the evil eye.",
    },
    {
        "persian": "الهی شکر",
        "finglish": "elâhi shokr",
        "english": "Thank God",
        "context": "Expression of gratitude, similar to 'thankfully'",
    },
    {
        "persian": "به سلامتی",
        "finglish": "be salâmati",
        "english": "To your health / Cheers",
        "context": "A toast or general well-wishing expression",
    },
    {
        "persian": "عید مبارک",
        "finglish": "eyd mobârak",
        "english": "Happy holiday/celebration",
        "context": "Used for any celebration, especially Nowruz",
    },
    {
        "persian": "تسلیت می‌گم",
        "finglish": "tasliyat migam",
        "english": "I offer my condolences",
        "context": "Expressing sympathy when someone has lost a loved one",
    },
    {
        "persian": "خدا بیامرزه",
        "finglish": "khodâ biâmorzesh",
        "english": "May God forgive them (rest in peace)",
        "context": "Said about someone who has passed away",
    },
    {
        "persian": "زبونت رو گاز بگیر",
        "finglish": "zaboonet ro gâz begir",
        "english": "Bite your tongue",
        "context": "Don't say such things! (similar to English 'touch wood')",
    },
    {
        "persian": "دمت گرم",
        "finglish": "damet garm",
        "english": "May your breath be warm",
        "context": "Well done! / Good for you! (informal, friendly praise)",
    },
    {
        "persian": "چشم",
        "finglish": "cheshm",
        "english": "On my eye (I will do it)",
        "context": "Respectful way of saying 'yes, I'll do it' — shows obedience/respect",
    },
    {
        "persian": "بفرمایید",
        "finglish": "befarmâyid",
        "english": "Please (go ahead / help yourself / come in)",
        "context": "Very versatile polite expression: offering food, inviting someone in, or giving way",
    },
    {
        "persian": "ببخشید",
        "finglish": "bebakhshid",
        "english": "Excuse me / I'm sorry",
        "context": "Used for both apologies and getting someone's attention",
    },
    {
        "persian": "مخلصیم",
        "finglish": "mokhlesim",
        "english": "I'm your humble servant",
        "context": "Polite/humble way of saying goodbye or responding to a compliment (ta'arof)",
    },
    {
        "persian": "سرت سلامت باشه",
        "finglish": "saret salâmat bâshe",
        "english": "May your head be safe",
        "context": "Expression of condolence — 'I'm sorry for your loss'",
    },
    {
        "persian": "روی ما رو زمین ننداز",
        "finglish": "ru-ye mâ ro zamin nandâz",
        "english": "Don't throw our face on the ground",
        "context": "Please don't refuse/embarrass us — said when insisting on a request",
    },
    {
        "persian": "قربونت برم",
        "finglish": "ghorboonet beram",
        "english": "I'd sacrifice myself for you",
        "context": "Term of endearment — very common among family and close friends",
    },
    {
        "persian": "جون دل",
        "finglish": "jun-e del",
        "english": "Life of my heart",
        "context": "Affectionate term used with loved ones",
    },
]


def get_all_expressions():
    """Return all built-in expressions."""
    return EXPRESSIONS


def get_random_expression():
    """Pick a random expression."""
    import random
    return random.choice(EXPRESSIONS)


def explain_expression(expression):
    """Use AI to generate a detailed explanation with usage examples."""
    prompt = f"""Explain this Persian expression for an English-speaking student:

Persian: {expression['persian']}
Transliteration: {expression['finglish']}
Literal meaning: {expression['english']}
Context: {expression['context']}

Please provide:
1. A fuller explanation of when and how this is used
2. The cultural context (ta'arof, hospitality, etc.)
3. Two example dialogues showing it in use (in Persian with English translation)
4. Any variations or related expressions

Keep it concise and student-friendly."""

    return ask(prompt, quality="fast")


def format_expression(expr, show_transliteration="off"):
    """Format an expression for display."""
    parts = [
        f'<div dir="rtl" style="font-size:1.8em; text-align:center">{expr["persian"]}</div>',
        f'<div style="text-align:center; font-size:1.2em">{expr["english"]}</div>',
    ]
    if show_transliteration != "off":
        parts.append(f'<div style="text-align:center; color:#666; font-style:italic">{expr["finglish"]}</div>')
    parts.append(f'<div style="text-align:center; color:#888; margin-top:0.5em">{expr["context"]}</div>')
    return "\n".join(parts)
