"""
Direct Date-Based Setlist Importer
Matches setlists to shows by date
"""
import requests
from datetime import datetime
from sqlmodel import Session, select
from database import engine
from models import Show, Song, Performance

BASE_URL = "https://elgoose.net/api/v2"

def main():
    print("DATE-BASED SETLIST IMPORTER")
    print("=" * 40)
    
    with Session(engine) as session:
        # Build show map by date
        shows = session.exec(select(Show)).all()
        show_by_date = {}
        for s in shows:
            date_str = s.date.strftime('%Y-%m-%d')
            show_by_date[date_str] = s.id
        print(f"Mapped {len(show_by_date)} shows by date")
        
        # Build song map by title (lowercase)
        songs = session.exec(select(Song)).all()
        song_map = {s.title.lower().strip(): s.id for s in songs}
        print(f"Mapped {len(song_map)} songs")
        
        # Get existing performances for dedup
        existing = set()
        perfs = session.exec(
            select(Performance.show_id, Performance.song_id, Performance.position)
        ).all()
        for p in perfs:
            existing.add((p[0], p[1], p[2]))
        print(f"Found {len(existing)} existing performances")
        
        # Fetch setlists page by page
        print("\\nImporting setlists...")
        page = 1
        total_added = 0
        
        while page <= 200:  # Safety limit
            print(f"  Page {page}...", end="", flush=True)
            
            url = f"{BASE_URL}/setlists.json"
            try:
                resp = requests.get(url, params={"page": page}, timeout=60)
                data = resp.json().get('data', [])
            except Exception as e:
                print(f" Error: {e}")
                break
            
            if not data:
                print(" Done (no data)")
                break
            
            added_this_page = 0
            for item in data:
                # Get showdate and match to our shows
                showdate = item.get('showdate')
                if not showdate:
                    continue
                
                our_show_id = show_by_date.get(showdate)
                if not our_show_id:
                    continue
                
                # Match song
                song_name = (item.get('songname') or '').lower().strip()
                song_id = song_map.get(song_name)
                if not song_id:
                    continue
                
                position = item.get('position', 0)
                key = (our_show_id, song_id, position)
                
                if key in existing:
                    continue
                
                perf = Performance(
                    show_id=our_show_id,
                    song_id=song_id,
                    position=position,
                    set_name=item.get('setnumber'),
                    segue=(item.get('transition', ', ') != ', '),
                    notes=item.get('footnote')
                )
                session.add(perf)
                existing.add(key)
                added_this_page += 1
                total_added += 1
            
            session.commit()
            print(f" +{added_this_page} ({total_added} total)")
            page += 1
        
        print(f"\\n✓ Added {total_added} performances")

if __name__ == "__main__":
    main()
