/**
 * API client for Audio Classifier backend
 */
import axios from 'axios'
import type {
  Track,
  TracksResponse,
  SearchResponse,
  SimilarTracksResponse,
  JobStatus,
  AnalyzeFolderRequest,
  WaveformData,
  Stats,
  FilterParams,
} from './types'

// Get API URL from runtime config (injected at container startup) or fallback to env var
export function getApiUrl(): string {
  if (typeof window !== 'undefined' && (window as any).__RUNTIME_CONFIG__) {
    return (window as any).__RUNTIME_CONFIG__.API_URL
  }
  return process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8000'
}

// Create axios instance dynamically to use runtime config
function getApiClient() {
  const client = axios.create({
    baseURL: getApiUrl(),
    headers: {
      'Content-Type': 'application/json',
    },
  })

  // Add JWT token to requests if available
  client.interceptors.request.use((config) => {
    if (typeof window !== 'undefined') {
      const token = localStorage.getItem('access_token')
      if (token) {
        config.headers.Authorization = `Bearer ${token}`
      }
    }
    return config
  })

  // Handle 401 errors (redirect to login)
  client.interceptors.response.use(
    (response) => response,
    (error) => {
      if (error.response?.status === 401 && typeof window !== 'undefined') {
        localStorage.removeItem('access_token')
        localStorage.removeItem('user')
        window.location.href = '/login'
      }
      return Promise.reject(error)
    }
  )

  return client
}

// Tracks
export async function getTracks(params: FilterParams & { skip?: number; limit?: number }): Promise<TracksResponse> {
  const response = await getApiClient().get('/api/tracks', { params })
  return response.data
}

export async function getTrack(id: string): Promise<Track> {
  const response = await getApiClient().get(`/api/tracks/${id}`)
  return response.data
}

export async function deleteTrack(id: string): Promise<void> {
  await getApiClient().delete(`/api/tracks/${id}`)
}

// Search
export async function searchTracks(
  query: string,
  filters?: { genre?: string; mood?: string; limit?: number }
): Promise<SearchResponse> {
  const response = await getApiClient().get('/api/search', {
    params: { q: query, ...filters },
  })
  return response.data
}

// Similar
export async function getSimilarTracks(id: string, limit: number = 10): Promise<SimilarTracksResponse> {
  const response = await getApiClient().get(`/api/tracks/${id}/similar`, {
    params: { limit },
  })
  return response.data
}

// Analysis
export async function analyzeFolder(request: AnalyzeFolderRequest): Promise<{ job_id: string }> {
  const response = await getApiClient().post('/api/analyze/folder', request)
  return response.data
}

export async function getAnalyzeStatus(jobId: string): Promise<JobStatus> {
  const response = await getApiClient().get(`/api/analyze/status/${jobId}`)
  return response.data
}

export async function deleteJob(jobId: string): Promise<void> {
  await getApiClient().delete(`/api/analyze/job/${jobId}`)
}

// Audio
export function getStreamUrl(trackId: string): string {
  return `${getApiUrl()}/api/audio/stream/${trackId}`
}

export function getDownloadUrl(trackId: string): string {
  return `${getApiUrl()}/api/audio/download/${trackId}`
}

export async function getWaveform(trackId: string, numPeaks: number = 800): Promise<WaveformData> {
  const response = await getApiClient().get(`/api/audio/waveform/${trackId}`, {
    params: { num_peaks: numPeaks },
  })
  return response.data
}

// Stats
export async function getStats(): Promise<Stats> {
  const response = await getApiClient().get('/api/stats')
  return response.data
}

// Health
export async function healthCheck(): Promise<{ status: string }> {
  const response = await getApiClient().get('/health')
  return response.data
}
