"""
Run once to bootstrap the database with initial data.
Usage: python seed.py
"""
import bcrypt
from database import engine, Base, SessionLocal
import models.user
import models.table
import models.printer
import models.product
import models.order

Base.metadata.create_all(bind=engine)

db = SessionLocal()

try:
    from models.user import User
    from models.printer import Printer

    # ── Manager account ───────────────────────────────────────────────────────
    if not db.query(User).filter(User.username == "manager").first():
        db.add(User(
            username="manager",
            pin_hash=bcrypt.hashpw(b"1234", bcrypt.gensalt()).decode(),
            role="manager",
        ))
        print("Created manager account  (PIN: 1234)")
    else:
        print("Manager account already exists — skipping")

    # ── Printers ──────────────────────────────────────────────────────────────
    if not db.query(Printer).filter(Printer.name == "Kitchen").first():
        db.add(Printer(name="Kitchen", ip_address="10.98.20.25", port=9100))
        print("Created Kitchen printer")

    if not db.query(Printer).filter(Printer.name == "Bar").first():
        db.add(Printer(name="Bar", ip_address="10.98.20.25", port=9100))
        print("Created Bar printer  (update IP when you have a second printer)")

    db.commit()
    print("\nDone. Change the manager PIN after first login.")

finally:
    db.close()
