
import os
import sys
import requests
from dotenv import load_dotenv

load_dotenv()

API_KEY = os.getenv("SETLISTFM_API_KEY")
BASE_URL = "https://api.setlist.fm/rest/1.0"

def search_artist(name):
    print(f"Searching for '{name}'...")
    headers = {
        "Accept": "application/json",
        "x-api-key": API_KEY
    }
    
    url = f"{BASE_URL}/search/artists"
    params = {"artistName": name, "sort": "relevance"}
    
    try:
        response = requests.get(url, headers=headers, params=params)
        response.raise_for_status()
        data = response.json()
        
        artists = data.get("artist", [])
        if not artists:
            print("No artists found.")
            return

        print(f"Found {len(artists)} results:")
        for artist in artists[:3]:
            print(f"  - Name: {artist.get('name')}")
            print(f"    MBID: {artist.get('mbid')}")
            print(f"    URL: {artist.get('url')}")
            print(f"    Disambiguation: {artist.get('disambiguation', 'N/A')}")
            print("-" * 20)

    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python find_artist.py <artist_name>")
        sys.exit(1)
    
    search_artist(sys.argv[1])
