export interface SpotifyDevice {
  id: string;
  is_active: boolean;
  is_private_session: boolean;
  is_restricted: boolean;
  name: string;
  type: string;
  volume_percent: number | null;
  supports_volume: boolean;
}

export interface SpotifyTrack {
  id: string;
  name: string;
  artists: Array<{
    id: string;
    name: string;
  }>;
  album: {
    id: string;
    name: string;
    images: Array<{
      url: string;
      height: number;
      width: number;
    }>;
  };
  duration_ms: number;
  external_urls: {
    spotify: string;
  };
}

export interface SpotifyPlaybackState {
  device: SpotifyDevice;
  repeat_state: 'off' | 'context' | 'track';
  shuffle_state: boolean;
  context: {
    type: string;
    href: string;
    external_urls: {
      spotify: string;
    };
    uri: string;
  } | null;
  timestamp: number;
  progress_ms: number | null;
  is_playing: boolean;
  item: SpotifyTrack | null;
  currently_playing_type: 'track' | 'episode' | 'ad' | 'unknown';
}

export interface SpotifyTokenResponse {
  access_token: string;
  token_type: 'Bearer';
  scope: string;
  expires_in: number;
  refresh_token?: string;
  state?: string;
}

export interface SpotifyError {
  error: {
    status: number;
    message: string;
  };
}

export interface SpotifyAuthState {
  isAuthenticated: boolean;
  accessToken: string | null;
  refreshToken: string | null;
  expiresAt: number | null;
  scopes: string[];
}

export interface SpotifyContextType extends SpotifyAuthState {
  currentTrack: SpotifyTrack | null;
  playbackState: SpotifyPlaybackState | null;
  devices: SpotifyDevice[];
  activeDevice: SpotifyDevice | null;
  isLoading: boolean;
  error: string | null;
  
  capturedState: SpotifyPlaybackSnapshot | null;
  isCaptureLoading: boolean;
  isResumeLoading: boolean;
  
  login: () => void;
  logout: () => void;
  
  play: (deviceId?: string) => Promise<void>;
  playTrack: (trackId: string, deviceId?: string, positionMs?: number) => Promise<void>;
  pause: () => Promise<void>;
  skipNext: () => Promise<void>;
  skipPrevious: () => Promise<void>;
  setVolume: (volumePercent: number) => Promise<void>;
  
  getDevices: () => Promise<void>;
  setActiveDevice: (deviceId: string) => Promise<void>;
  
  refreshPlaybackState: () => Promise<void>;
  
  capturePlaybackState: () => Promise<void>;
  resumePlaybackState: () => Promise<void>;
  clearCapturedState: () => void;

  searchTracks: (query: string) => Promise<SpotifyTrack[]>;
}

export interface PKCEState {
  codeVerifier: string;
  codeChallenge: string;
  state: string;
}

export interface SpotifyDevicesResponse {
  devices: SpotifyDevice[];
}

export interface SpotifyPlaybackSnapshot {
  contextUri?: string;
  trackUri?: string;
  trackId?: string;
  positionMs: number;
  shuffleState: boolean;
  repeatState: 'off' | 'context' | 'track';
  volumePercent?: number;
  deviceId?: string;
  timestamp: number;
  isPlaying: boolean;
  trackName?: string;
  artistName?: string;
  albumName?: string;
  albumImageUrl?: string;
}