from sqlalchemy.ext.asyncio import AsyncSession
from sqlmodel import select

from app.models.common_question import CommonQuestion

COMMON_QUESTION_CATALOG: list[dict[str, str]] = [
    # ── General Info ──
    # The basics every diner needs before they commit to a reservation.
    {
        "question_key": "address_parking",
        "question_text": "Where are you located and is there parking?",
        "category": "General Info",
    },
    {
        "question_key": "walk_ins",
        "question_text": "Do you accept walk-ins?",
        "category": "General Info",
    },
    {
        "question_key": "price_range",
        "question_text": "What is the average price per person?",
        "category": "General Info",
    },
    {
        "question_key": "meal_duration",
        "question_text": "How long should I plan for a meal?",
        "category": "General Info",
    },
    {
        "question_key": "dress_code",
        "question_text": "Is there a dress code?",
        "category": "General Info",
    },
    {
        "question_key": "public_transport",
        "question_text": "How do I get there by public transport?",
        "category": "General Info",
    },
    # ── Menu & Dietary ──
    # What can I actually eat here — the decisive filter for many diners.
    {
        "question_key": "dietary_options",
        "question_text": "What vegetarian, vegan, or gluten-free options do you have?",
        "category": "Menu & Dietary",
    },
    {
        "question_key": "allergen_info",
        "question_text": "How do you handle food allergies?",
        "category": "Menu & Dietary",
    },
    {
        "question_key": "halal_kosher",
        "question_text": "Do you serve halal or kosher food?",
        "category": "Menu & Dietary",
    },
    {
        "question_key": "kids_menu",
        "question_text": "Do you have a children's menu?",
        "category": "Menu & Dietary",
    },
    {
        "question_key": "tasting_menu",
        "question_text": "Do you offer a tasting or chef's menu?",
        "category": "Menu & Dietary",
    },
    {
        "question_key": "drink_specials",
        "question_text": "What drink specials or signature cocktails do you offer?",
        "category": "Menu & Dietary",
    },
    # ── Facilities ──
    # Practical concerns that can make or break the evening.
    {
        "question_key": "accessibility",
        "question_text": "Is your restaurant wheelchair accessible?",
        "category": "Facilities",
    },
    {
        "question_key": "outdoor_seating",
        "question_text": "Do you have outdoor seating or a terrace?",
        "category": "Facilities",
    },
    {
        "question_key": "private_dining",
        "question_text": "Do you offer private dining rooms?",
        "category": "Facilities",
    },
    {
        "question_key": "parking_details",
        "question_text": "What parking options are available?",
        "category": "Facilities",
    },
    {
        "question_key": "baby_facilities",
        "question_text": "Do you have high chairs or baby changing facilities?",
        "category": "Facilities",
    },
    {
        "question_key": "kid_friendly",
        "question_text": "Is the restaurant suitable for young children?",
        "category": "Facilities",
    },
    {
        "question_key": "wifi",
        "question_text": "Do you have Wi-Fi?",
        "category": "Facilities",
    },
    {
        "question_key": "noise_ambiance",
        "question_text": "What is the ambiance like — is it suitable for a quiet dinner?",
        "category": "Facilities",
    },
    # ── Policies ──
    # The fine print that avoids surprises.
    {
        "question_key": "payment_methods",
        "question_text": "What payment methods do you accept?",
        "category": "Policies",
    },
    {
        "question_key": "tipping",
        "question_text": "Is a service charge included or is tipping expected?",
        "category": "Policies",
    },
    {
        "question_key": "pet_policy",
        "question_text": "Are pets allowed?",
        "category": "Policies",
    },
    {
        "question_key": "corkage_fee",
        "question_text": "Do you charge a corkage fee for bringing your own wine?",
        "category": "Policies",
    },
    {
        "question_key": "gift_cards",
        "question_text": "Do you offer gift cards or vouchers?",
        "category": "Policies",
    },
    # ── Special Occasions ──
    # The reason most "nice" dinners happen.
    {
        "question_key": "celebrations",
        "question_text": "Can you arrange something special for a birthday or anniversary?",
        "category": "Special Occasions",
    },
    {
        "question_key": "bring_own_cake",
        "question_text": "Can I bring my own birthday cake or champagne?",
        "category": "Special Occasions",
    },
    {
        "question_key": "seating_preference",
        "question_text": "Can I request a specific table or seating area?",
        "category": "Special Occasions",
    },
    {
        "question_key": "group_bookings",
        "question_text": "Do you accommodate large groups or private events?",
        "category": "Special Occasions",
    },
    {
        "question_key": "live_entertainment",
        "question_text": "Do you have live music or entertainment?",
        "category": "Special Occasions",
    },
    # ── Takeaway & Delivery ──
    {
        "question_key": "takeaway_delivery",
        "question_text": "Do you offer takeaway or delivery?",
        "category": "Takeaway & Delivery",
    },
    {
        "question_key": "delivery_area",
        "question_text": "What areas do you deliver to?",
        "category": "Takeaway & Delivery",
    },
    {
        "question_key": "online_ordering",
        "question_text": "Can I order online?",
        "category": "Takeaway & Delivery",
    },
]


async def seed_common_questions(session: AsyncSession, restaurant_id: str) -> int:
    """Seed predefined common questions for a restaurant.

    Idempotent: only inserts questions whose question_key does not yet
    exist for the given restaurant.

    Returns the count of newly created questions.
    """
    result = await session.execute(
        select(CommonQuestion.question_key).where(CommonQuestion.restaurant_id == restaurant_id)
    )
    existing_keys = set(result.scalars().all())

    created = 0
    for item in COMMON_QUESTION_CATALOG:
        if item["question_key"] in existing_keys:
            continue
        question = CommonQuestion(
            restaurant_id=restaurant_id,
            question_key=item["question_key"],
            question_text=item["question_text"],
            category=item["category"],
        )
        session.add(question)
        created += 1

    if created:
        await session.commit()
    return created
