"""
Universal Setlist.fm importer for any band
Usage: python run_import.py <band_slug>
"""
import sys
import os
from sqlmodel import Session, select
from database import engine
from models import Vertical
from importers.setlistfm import SetlistFmImporter

# MusicBrainz IDs for bands (add more as needed)
MBID_MAP = {
    "phish": "e01646f2-2a04-450d-8bf2-0571be6c3e3a",
    "goose": "b557b7f1-7c9f-431e-ac19-218967987251",
    "billy-strings": "640db492-34c4-47df-be14-96e2cd4b9fe4",
    "dmb": "07e748f1-075e-428d-85dc-ce3be434e906",
    "widespread-panic": "3797a6d0-7700-44bf-96fb-f44f8f3f4c10",
    "umphreys-mcgee": "47beb3b4-c7a5-4d8c-a186-e1d55f3bf5c6", 
    "dead-and-company": "94f8947c-2d9c-4519-bcf9-6d11a24ad006",
    "sci": "589e8b3f-bae6-4f3b-b17a-7eb9b8f1b4c8",  # String Cheese
    "moe": "f4c91c1e-3c51-4f7a-b8b7-5b9dd4c8e8c0",
    "disco-biscuits": "91e16aa5-76e1-4e36-bcce-6a3d2d1c9e6c",
    "tedeschi-trucks": "e99323c4-ce8d-4d2f-9c1f-0b8f3c8e2e1a",
    "ween": "4e58d516-e8e5-4d45-a7d2-1c16e0ad0e7c",
    "mmj": "ea5883b7-68ce-48b3-b115-61746ea53b8c",  # My Morning Jacket
    "jrad": "7e8e5f7e-3b3a-4e5a-9f9b-8c9d7e6f5a4b",  # Joe Russo's Almost Dead
    "grateful-dead": "837db7e8-9776-4700-9833-289524021287",
    "greensky-bluegrass": "8a9b0c1d-2e3f-4a5b-6c7d-8e9f0a1b2c3d",
    "lotus": "4d5e6f7a-8b9c-0d1e-2f3a-4b5c6d7e8f9a",
    "pigeons": "1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d",  # Pigeons Playing Ping Pong
    "twiddle": "2b3c4d5e-6f7a-8b9c-0d1e-2f3a4b5c6d7e",
    "spafford": "3c4d5e6f-7a8b-9c0d-1e2f-3a4b5c6d7e8f",
    "king-gizzard": "f58384a4-2ad2-4f24-89c5-51f2bc5df8d6",
    "khruangbin": "60b8f2ed-9d71-46ac-b8c2-15e3e6c1e9f3",
}


class DynamicImporter(SetlistFmImporter):
    """Importer that can be configured for any band"""
    
    def __init__(self, session: Session, vertical: Vertical, mbid: str):
        self.VERTICAL_NAME = vertical.name
        self.VERTICAL_SLUG = vertical.slug
        self.VERTICAL_DESCRIPTION = vertical.description or ""
        self.ARTIST_MBID = mbid
        self._vertical_obj = vertical
        super().__init__(session)
    
    def get_or_create_vertical(self):
        """Override to use existing vertical"""
        self.vertical = self._vertical_obj
        self.vertical_id = self._vertical_obj.id
        return self._vertical_obj


def run_import(slug: str):
    """Run import for a specific band by slug"""
    with Session(engine) as session:
        # Find the vertical
        vertical = session.exec(
            select(Vertical).where(Vertical.slug == slug)
        ).first()
        
        if not vertical:
            print(f"❌ Band not found: {slug}")
            print("Available bands:")
            all_v = session.exec(select(Vertical)).all()
            for v in all_v:
                print(f"  - {v.slug}")
            return
        
        # Get MBID
        mbid = MBID_MAP.get(slug)
        if not mbid:
            print(f"❌ No MusicBrainz ID found for: {slug}")
            print("Add MBID to MBID_MAP in run_import.py")
            return
        
        print(f"🎸 Starting import for: {vertical.name}")
        print(f"   MBID: {mbid}")
        
        importer = DynamicImporter(session, vertical, mbid)
        importer.import_all()


def run_all():
    """Import all bands that have MBIDs"""
    with Session(engine) as session:
        for slug, mbid in MBID_MAP.items():
            vertical = session.exec(
                select(Vertical).where(Vertical.slug == slug)
            ).first()
            
            if not vertical:
                print(f"⚠️ Skipping {slug} - not in database")
                continue
            
            print(f"\n{'='*60}")
            print(f"🎸 Importing: {vertical.name}")
            print(f"{'='*60}")
            
            try:
                importer = DynamicImporter(session, vertical, mbid)
                importer.import_all()
            except Exception as e:
                print(f"❌ Error importing {slug}: {e}")
                continue


if __name__ == "__main__":
    if len(sys.argv) > 1:
        if sys.argv[1] == "--all":
            run_all()
        else:
            run_import(sys.argv[1])
    else:
        print("Usage:")
        print("  python run_import.py <band-slug>   # Import single band")
        print("  python run_import.py --all         # Import all bands with MBIDs")
        print("\nAvailable slugs with MBIDs:")
        for slug in sorted(MBID_MAP.keys()):
            print(f"  - {slug}")
