from googleapiclient.discovery import build
from datetime import datetime
import warnings

# Suppress specific FutureWarnings about Python 3.9 EOL
warnings.filterwarnings("ignore", category=FutureWarning, message=".*3.9.*")
def get_nba_highlight_url(api_key, team_a, team_b, game_date):
    """
    Fetches the YouTube highlight URL for a specific NBA matchup and date.
    
    :param api_key: Your YouTube Data API v3 key
    :param team_a: Name of the first team (e.g., "Lakers")
    :param team_b: Name of the second team (e.g., "Celtics")
    :param game_date: Date string in 'YYYY-MM-DD' format
    """
    youtube = build('youtube', 'v3', developerKey=api_key)
    
    # Format the date for the search query to improve accuracy
    date_obj = datetime.strptime(game_date, "%Y-%m-%d")
    formatted_date = date_obj.strftime("%B %d, %Y") # e.g., "May 28, 2026"
    
    # Construct a highly targeted query focusing on the official NBA channel
    query = f"{team_a} vs {team_b} Game Recap Full Highlights {formatted_date}"
    
    # The official NBA Channel ID is 'UCwXb1aZ6S_v79tCIaXwS3_g'
    request = youtube.search().list(
        q=query,
        part='snippet',
        channelId='UCwXh0i7-fVy9fSBAIn9z9_A', # Restricts search to official NBA channel
        maxResults=1,
        type='video'
    )
    
    response = request.execute()
    
    if response.get('items'):
        video_id = response['items'][0]['id']['videoId']
        video_title = response['items'][0]['snippet']['title']
        video_url = f"https://www.youtube.com/watch?v={video_id}"
        
        return {
            "title": video_title,
            "url": video_url
        }
    else:
        return None

# --- Example Usage ---
API_KEY = "AIzaSyDZKrV0fytZZ50KiyKJ6WFU4FhaBE4aLn0"
TEAM_1 = "Phoenix Suns"
TEAM_2 = "Oklahoma City Thunder"
DATE = "2026-01-05"

result = get_nba_highlight_url(API_KEY, TEAM_1, TEAM_2, DATE)

if result:
    print(f"Found Video: {result['title']}")
    print(f"URL: {result['url']}")
else:
    print("No official highlight video found for that date and matchup.")
