"""Demo users and a menu, created on first start in dev (refused in production by Settings)."""
from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker

from .config import Settings
from .models import Product, User
from .security import hash_password

DEMO_PASSWORD = "brew-demo-pass"         # local demo accounts only

DEMO_USERS = [("asha@example.com", "customer"), ("ravi@example.com", "customer"), ("admin@example.com", "admin")]

MENU = [  # sku, name, price in paise, active
    ("espresso", "Espresso", 12000, True),
    ("latte", "Latte", 18000, True),
    ("cappuccino", "Cappuccino", 17000, True),
    ("mocha", "Mocha", 21000, True),
    ("cold-brew", "Cold brew", 22000, True),
    ("croissant", "Butter croissant", 9000, True),
    ("banana-bread", "Banana bread", 11000, True),
    ("pumpkin-spice", "Pumpkin spice latte (seasonal)", 25000, False),
]


async def seed(sessions: async_sessionmaker[AsyncSession], settings: Settings) -> None:
    async with sessions() as session:
        if await session.scalar(select(func.count()).select_from(User)):
            return
        password_hash = hash_password(DEMO_PASSWORD, settings.bcrypt_rounds)
        session.add_all(User(email=email, role=role, password_hash=password_hash) for email, role in DEMO_USERS)
        session.add_all(Product(sku=sku, name=name, price_minor=price, active=active)
                        for sku, name, price, active in MENU)
        await session.commit()
