o
    i                     @   s   d Z ddlZddlZddlZddlmZ edZe d Z	e	d Z
eddZed	dZd
Zg dZdd Zdd Zdd ZdS )u  
Spotify OAuth2 Handler for Luke.

Manages authorization, token refresh, and service access for Spotify.
Uses Authorization Code flow with spotipy's built-in redirect handling.

Redirect URI: http://127.0.0.1:8091
  → Spotify allows http for 127.0.0.1 (but NOT "localhost")
  → Add exactly this in Spotify Developer Dashboard > Settings > Redirect URIs

Scopes:
  - user-read-currently-playing: what's playing now
  - user-read-recently-played: recent listening history
  - user-read-playback-state: playback device info
  - user-modify-playback-state: play/pause/skip/queue
  - user-top-read: top artists and tracks
  - playlist-read-private: read private playlists
  - playlist-modify-public: create/edit playlists
  - playlist-modify-private: create/edit private playlists

Setup:
  1. Add SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET to ~/clawd/.env
  2. In Spotify Dashboard > Settings > Redirect URIs, add: http://127.0.0.1:8091
  3. Run: python3 setup_spotify_auth.py
  4. Authorize in browser
  5. Token auto-refreshes from there
    N)Pathzspotify-authclawdz.spotify-token.jsonSPOTIFY_CLIENT_ID SPOTIFY_CLIENT_SECRETzhttp://127.0.0.1:8091)	zuser-read-currently-playingzuser-read-recently-playedzuser-read-playback-statezuser-modify-playback-statezuser-top-readzuser-follow-readzplaylist-read-privatezplaylist-modify-publiczplaylist-modify-privatec                   C   s   t totS )z%Check if Spotify credentials are set.)boolr   r    r   r   G/sessions/lucid-sleepy-lamport/mnt/clawd/whatsapp-agent/spotify_auth.pyis_configured=   s   r
   c               
   C   s   t  s
td dS z9ddl} ddlm} |tttd	t
ttdd}| }|s2td W dS | j|d	}|  td
 |W S  ty^ } ztd|  W Y d}~dS d}~ww )zf
    Get an authenticated Spotipy client.
    Returns None if not configured or token is missing.
    zOSpotify not configured. Set SPOTIFY_CLIENT_ID and SPOTIFY_CLIENT_SECRET in .envNr   SpotifyOAuth F	client_idclient_secretredirect_uriscope
cache_pathopen_browserz8No Spotify token found. Run setup_spotify_auth.py first.)auth_managerzSpotify client: ACTIVEzSpotify client init failed: )r
   loggerinfospotipyspotipy.oauth2r   r   r   SPOTIFY_REDIRECT_URIjoinSCOPESstr
TOKEN_PATHget_cached_tokenwarningSpotifycurrent_user	Exceptionerror)r   r   r   
token_infoclienter   r   r	   get_spotify_clientB   s6   



r(   c                  C   s2   ddl } ddlm} |tttdttt	ddS )z4Get the SpotifyOAuth manager (used by setup script).r   Nr   r   Tr   )
r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r	   get_auth_manageri   s   r)   )__doc__osjsonloggingpathlibr   	getLoggerr   home	CLAWD_DIRr   getenvr   r   r   r   r
   r(   r)   r   r   r   r	   <module>   s    
'