"""Seed script to create initial scenes and assign bands to them"""

from sqlmodel import Session, select
from database import engine
from models import Scene, Vertical, VerticalScene


# Scene definitions
SCENES = [
    {"name": "Jam", "slug": "jam", "description": "Improvisational rock bands with extended jams"},
    {"name": "Bluegrass", "slug": "bluegrass", "description": "Progressive and traditional bluegrass"},
    {"name": "Dead Family", "slug": "dead-family", "description": "Grateful Dead and related projects"},
    {"name": "Funk", "slug": "funk", "description": "Funk, soul, and groove-oriented bands"},
]

# Band -> Scene assignments
BAND_SCENES = {
    "goose": ["jam"],
    "phish": ["jam"],
    "grateful-dead": ["jam", "dead-family"],
    "dead-and-company": ["jam", "dead-family"],
    "billy-strings": ["bluegrass", "jam"],
    # Expansion Wave 1
    "pigeons-playing-ping-pong": ["jam", "funk"],
    "eggy": ["jam"],
    "dogs-in-a-pile": ["jam"],
    "greensky-bluegrass": ["bluegrass", "jam"],
    "daniel-donato": ["jam"],
    # Expansion Wave 2
    "umphreys-mcgee": ["jam"],
    "moe": ["jam"],
    "widespread-panic": ["jam"],
    "sturgill-simpson": ["bluegrass"],
    "slightly-stoopid": ["jam", "funk"],
}


def seed_scenes():
    """Create scenes if they don't exist"""
    print("Seeding scenes...")
    
    with Session(engine) as session:
        for scene_data in SCENES:
            existing = session.exec(
                select(Scene).where(Scene.slug == scene_data["slug"])
            ).first()
            
            if not existing:
                scene = Scene(**scene_data)
                session.add(scene)
                print(f"  Created scene: {scene_data['name']}")
            else:
                print(f"  Scene exists: {scene_data['name']}")
        
        session.commit()
        print("Scenes seeded.")


def assign_bands_to_scenes():
    """Assign existing bands to their scenes"""
    print("\nAssigning bands to scenes...")
    
    with Session(engine) as session:
        for band_slug, scene_slugs in BAND_SCENES.items():
            vertical = session.exec(
                select(Vertical).where(Vertical.slug == band_slug)
            ).first()
            
            if not vertical:
                continue
            
            for scene_slug in scene_slugs:
                scene = session.exec(
                    select(Scene).where(Scene.slug == scene_slug)
                ).first()
                
                if not scene:
                    continue
                
                # Check if assignment exists
                existing = session.exec(
                    select(VerticalScene)
                    .where(VerticalScene.vertical_id == vertical.id)
                    .where(VerticalScene.scene_id == scene.id)
                ).first()
                
                if not existing:
                    vs = VerticalScene(vertical_id=vertical.id, scene_id=scene.id)
                    session.add(vs)
                    print(f"  Assigned {band_slug} -> {scene_slug}")
        
        session.commit()
        print("Band scene assignments complete.")


if __name__ == "__main__":
    seed_scenes()
    assign_bands_to_scenes()
