"""Initialize the database with sample data — for demo and testing.

Running this is idempotent: it wipes existing rows (in the 4 tables) and reseeds.
Safe to run repeatedly. Do NOT run in production once real data exists."""
from database import Base, SessionLocal, engine
import models

Base.metadata.create_all(bind=engine)
db = SessionLocal()

# ---- Wipe existing rows (idempotent) ----
db.query(models.Ticket).delete()
db.query(models.Support).delete()
db.query(models.System).delete()
db.query(models.Product).delete()
db.commit()

# ---- Products ----
products = [
    models.Product(product_code="MFORCE", product_name="MFORCE"),
    models.Product(product_code="MTRADE", product_name="MTRADE"),
]
db.add_all(products)
db.commit()

# ---- Systems (each belongs to a product) ----
systems = [
    models.System(system_code="DMS",         system_name="DMS",         product_code="MFORCE"),
    models.System(system_code="SFA",         system_name="SFA",         product_code="MFORCE"),
    models.System(system_code="FSM",         system_name="FSM",         product_code="MFORCE"),
    models.System(system_code="STP",         system_name="STP",         product_code="MTRADE"),
    models.System(system_code="TPM",         system_name="TPM",         product_code="MTRADE"),
    models.System(system_code="SMART TRADE", system_name="SMART TRADE", product_code="MTRADE"),
]
db.add_all(systems)
db.commit()

# ---- Support routing (system_code → support_email) ----
supports = [
    models.Support(support_email="allen@mobileone.com.my", system_code="DMS"),
    models.Support(support_email="allen@mobileone.com.my", system_code="SFA"),
    models.Support(support_email="allen@mobileone.com.my", system_code="FSM"),
    models.Support(support_email="timmy@mobileone.com.my", system_code="STP"),
    models.Support(support_email="timmy@mobileone.com.my", system_code="TPM"),
    models.Support(support_email="timmy@mobileone.com.my", system_code="SMART TRADE"),
]
db.add_all(supports)
db.commit()

print(
    f"Seeded: {len(products)} products, "
    f"{len(systems)} systems, "
    f"{len(supports)} support entries"
)
db.close()