"""
Videos endpoint - list all performances and shows with YouTube links
"""
from typing import List, Optional
from fastapi import APIRouter, Depends, Query
from sqlmodel import Session, select
from database import get_session
from models import Show, Performance, Song, Venue

router = APIRouter(prefix="/videos", tags=["videos"])


@router.get("/")
def get_all_videos(
    limit: int = Query(default=100, le=500),
    offset: int = Query(default=0),
    session: Session = Depends(get_session)
):
    """Get all performances and shows with YouTube links."""
    
    # Get performances with videos
    perf_query = (
        select(
            Performance.id,
            Performance.youtube_link,
            Performance.show_id,
            Song.id.label("song_id"),
            Song.title.label("song_title"),
            Song.slug.label("song_slug"),
            Show.date,
            Show.slug.label("show_slug"),
            Venue.name.label("venue_name"),
            Venue.city.label("venue_city"),
            Venue.state.label("venue_state"),
            Performance.slug.label("performance_slug"),
            Venue.slug.label("venue_slug")
        )
        .join(Song, Performance.song_id == Song.id)
        .join(Show, Performance.show_id == Show.id)
        .join(Venue, Show.venue_id == Venue.id)
        .where(Performance.youtube_link != None)
        .order_by(Show.date.desc())
        .limit(limit)
        .offset(offset)
    )
    
    perf_results = session.exec(perf_query).all()
    
    performances = [
        {
            "type": "performance",
            "id": r[0],
            "youtube_link": r[1],
            "show_id": r[2],
            "song_id": r[3],
            "song_title": r[4],
            "song_slug": r[5],
            "date": r[6].isoformat() if r[6] else None,
            "show_slug": r[7],
            "venue_name": r[8],
            "venue_city": r[9],
            "venue_state": r[10],
            "performance_slug": r[11],
            "venue_slug": r[12]
        }
        for r in perf_results
    ]
    
    # Get shows with videos
    show_query = (
        select(
            Show.id,
            Show.youtube_link,
            Show.date,
            Show.slug,
            Venue.name.label("venue_name"),
            Venue.city.label("venue_city"),
            Venue.state.label("venue_state"),
            Venue.slug.label("venue_slug")
        )
        .join(Venue, Show.venue_id == Venue.id)
        .where(Show.youtube_link != None)
        .order_by(Show.date.desc())
        .limit(limit)
        .offset(offset)
    )
    
    show_results = session.exec(show_query).all()
    
    shows = [
        {
            "type": "full_show",
            "id": r[0],
            "youtube_link": r[1],
            "date": r[2].isoformat() if r[2] else None,
            "show_slug": r[3],
            "venue_name": r[4],
            "venue_city": r[5],
            "venue_state": r[6],
            "venue_slug": r[7]
        }
        for r in show_results
    ]
    
    return {
        "performances": performances,
        "shows": shows,
        "total_performances": len(performances),
        "total_shows": len(shows)
    }


@router.get("/stats")
def get_video_stats(session: Session = Depends(get_session)):
    """Get counts of videos in the database."""
    from sqlmodel import func
    
    perf_count = session.exec(
        select(func.count(Performance.id)).where(Performance.youtube_link != None)
    ).one()
    
    show_count = session.exec(
        select(func.count(Show.id)).where(Show.youtube_link != None)
    ).one()
    
    return {
        "performance_videos": perf_count,
        "full_show_videos": show_count,
        "total": perf_count + show_count
    }
