from sqlmodel import Session, select, create_engine
from database import engine
from models import Vertical, Scene, VerticalScene, Artist
from importers.dso import DsoImporter

def seed_dso():
    with Session(engine) as session:
        # 1. Ensure "Grateful Dead Family" scene exists
        scene = session.exec(select(Scene).where(Scene.slug == "grateful-dead-family")).first()
        if not scene:
            scene = Scene(name="Grateful Dead Family", slug="grateful-dead-family")
            session.add(scene)
            session.commit()
            session.refresh(scene)

        # 2. Add Artist
        print("Creating Artist...")
        artist = session.exec(select(Artist).where(Artist.slug == "dark-star-orchestra")).first()
        if not artist:
            artist = Artist(name="Dark Star Orchestra", slug="dark-star-orchestra")
            session.add(artist)
            session.commit()
            session.refresh(artist)

        # 3. Add Vertical (Band)
        print("Creating Vertical...")
        vertical = session.exec(select(Vertical).where(Vertical.slug == "dark-star-orchestra")).first()
        if not vertical:
            vertical = Vertical(
                name="Dark Star Orchestra",
                slug="dark-star-orchestra",
                description="Recreating the Grateful Dead concert experience.",
                setlistfm_mbid="e477d9c0-1f35-40f7-ad1a-b915d2523b84",
                primary_artist_id=artist.id
            )
            session.add(vertical)
            session.commit()
            session.refresh(vertical)

        # 4. Link to Scene
        link = session.exec(select(VerticalScene).where(
            VerticalScene.vertical_id == vertical.id,
            VerticalScene.scene_id == scene.id
        )).first()
        if not link:
            session.add(VerticalScene(vertical_id=vertical.id, scene_id=scene.id))
            session.commit()

        print("✅ Vertical seeded successfully.")

        # 5. Run Import (Optional - can be run separately)
        print("Starting import...")
        importer = DsoImporter(session)
        importer.import_all()

if __name__ == "__main__":
    seed_dso()
