"""Subject-specific story archetypes — gender-aware, 3-beat structure.

Standard: Urdu verb must agree with subject in person, gender, number.
Masculine progressive = 'raha tha', feminine = 'rahi thi'.
Masculine present = 'samajhta hai', feminine = 'samajhti hai'.
"""
import random

CHARACTERS = [
    ('Ahmed', 'M'), ('Bilal', 'M'), ('Hassan', 'M'), ('Ali', 'M'), ('Usman', 'M'),
    ('Fatima', 'F'), ('Ayesha', 'F'), ('Zainab', 'F'), ('Maryam', 'F'), ('Sana', 'F'),
]


class BaseArchetype:
    def __init__(self, seed=''):
        self.rng = random.Random(seed or 'default')

    def _pick_char(self):
        return self._pick(CHARACTERS)

    def _pick(self, lst):
        return self.rng.choice(lst)

    def _progressive(self, gender):
        """Returns (soch raha/soch rahi, tha/thi)."""
        if gender == 'F':
            return ('soch rahi', 'thi')
        return ('soch raha', 'tha')

    def _samajh(self, gender):
        return 'samajhti' if gender == 'F' else 'samajhta'

    def _resolution(self, terms):
        if len(terms) >= 2:
            return (f"Asal wajah ye thi ke {terms[0]} aur {terms[1]} alag alag cheezein hain, "
                    f"inhein aik hi samajhna ghalti thi.")
        if terms:
            return f"Asal wajah ye thi ke {terms[0]} ki asal haqeeqat kuch aur thi."
        return "Asal wajah ye thi ke soch mein kuch kami thi."

    def _final_line(self, hero, gender, title):
        samajh = self._samajh(gender)
        return f"Ab jab bhi {title} ka sawal aata hai, {hero} sahi {samajh} hai."


class CookingArchetype(BaseArchetype):
    def render(self, title, misconception, mechanism, terms):
        hero, gender = self._pick_char()
        place = self._pick(['apni kitchen', 'dawat ki rasoi', 'hotel ki rasoi'])
        action = self._pick(['khana pakate hue', 'chai banate hue', 'cheezein milate hue'])
        prog, aux = self._progressive(gender)
        return (
            f"{hero} {place} mein {action} {title} ke baare mein {prog} {aux}. "
            f"Us ne socha: {misconception}. "
            f"Us ne apne dost ko yehi samjhaya, aur dono ne isi soch par amal kiya. "
            f"Lekin jab asal mein paka, to natija ghalat nikla. "
            f"{self._resolution(terms)} "
            f"{hero} ne apni ghalti maani aur ab sahi samajh{'ti' if gender == 'F' else 'ta'} hai. "
           
        )


class SportsArchetype(BaseArchetype):
    def render(self, title, misconception, mechanism, terms):
        hero, gender = self._pick_char()
        place = self._pick(['cricket match', 'football practice', 'race ground', 'gully cricket'])
        action = self._pick(['khelte hue', 'practice karte hue', 'match mein utarte hue'])
        prog, aux = self._progressive(gender)
        return (
            f"{hero} {place} mein {action} {title} ke baare mein {prog} {aux}. "
            f"Us ne socha: {misconception}. "
            f"Us ne apne dost ko yehi bataya, aur dono ne isi strategy par khela. "
            f"Lekin jab asal ball aya, to andaza ghalat nikla. "
            f"{self._resolution(terms)} "
            f"{hero} ne apni ghalti maani aur ab sahi samajh{'ti' if gender == 'F' else 'ta'} hai. "
           
        )


class JourneyArchetype(BaseArchetype):
    def render(self, title, misconception, mechanism, terms):
        hero, gender = self._pick_char()
        place = self._pick(['safar', 'hospital visit', 'bagh mein tehalte hue', 'gaon ke safar'])
        prog, aux = self._progressive(gender)
        return (
            f"{hero} {place} ke doran {title} ke baare mein {prog} {aux}. "
            f"Us ne socha: {misconception}. "
            f"Us ne apne dost ko yehi samjhaya, aur dono ne isi soch ke sath aage barhe. "
            f"Lekin jab asal waqt aya, to natija uski umeed ke barkhilaf tha. "
            f"{self._resolution(terms)} "
            f"{hero} ne apni ghalti maani aur ab sahi samajh{'ti' if gender == 'F' else 'ta'} hai. "
           
        )


class CricketScoreArchetype(BaseArchetype):
    def render(self, title, misconception, mechanism, terms):
        hero, gender = self._pick_char()
        place = self._pick(['cricket match', 'team ka score board', 'dost ke sath hisaab'])
        prog, aux = self._progressive(gender)
        return (
            f"{hero} {place} mein {title} ka hisaab laga {'rahi thi' if gender == 'F' else 'raha tha'}. "
            f"Us ne socha: {misconception}. "
            f"Us ne apne dost ko yehi bataya, aur dono ne isi hisaab par bharosa kiya. "
            f"Lekin jab result check kiya, to jawab ghalat tha. "
            f"{self._resolution(terms)} "
            f"{hero} ne calculation dobara ki aur sahi natija nikala. "
           
        )


class ConversationArchetype(BaseArchetype):
    def render(self, title, misconception, mechanism, terms):
        hero, gender = self._pick_char()
        place = self._pick(['chai ki dukan', 'dost ke ghar', 'masjid ke sahun', 'school ke baad'])
        activity = self._pick(['baat karte hue', 'chai peete hue', 'padhte hue'])
        prog, aux = self._progressive(gender)
        return (
            f"{hero} apne dost ke sath {place} mein {activity} {title} par baat kar "
            f"{'rahi thi' if gender == 'F' else 'raha tha'}. "
            f"Us ne socha: {misconception}. "
            f"Us ne apne dost ko yehi samjhaya, aur dono ne isi soch par yakeen kiya. "
            f"Lekin jab asal waqt aya, baat ghalat sabit hui. "
            f"{self._resolution(terms)} "
            f"{hero} ne apni baat sudhari aur ab sahi samajh{'ti' if gender == 'F' else 'ta'} hai. "
           
        )


ARCHETYPES = {
    'chemistry': CookingArchetype,
    'physics': SportsArchetype,
    'biology': JourneyArchetype,
    'mathematics': CricketScoreArchetype,
    'math': CricketScoreArchetype,
    'urdu': ConversationArchetype,
    'islamiyat': ConversationArchetype,
    'english': ConversationArchetype,
    'computer science': JourneyArchetype,
    'tarjumatul quran': ConversationArchetype,
}


def get_archetype(subject, seed=''):
    key = (subject or '').strip().lower()
    cls = ARCHETYPES.get(key, ConversationArchetype)
    return cls(seed=seed)
