import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'

export interface ApiResponse<T> {
  data: T
  meta?: {
    pagination?: {
      page: number
      page_size: number
      total: number
      total_pages: number
    }
  }
}

export interface ApiError {
  error: {
    code: string
    message: string
    details?: unknown
    request_id?: string
  }
}

export function createApiClient(baseURL: string = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080', getApiKey?: () => string | null) {
  const client = axios.create({
    baseURL,
    timeout: 30000,
    headers: { 'Content-Type': 'application/json' },
  })

  client.interceptors.request.use(
    (config) => {
      const key = getApiKey ? getApiKey() : (typeof window !== 'undefined' ? localStorage.getItem('api_key') : null)
      if (key) config.headers['X-API-Key'] = key
      return config
    },
    (error) => Promise.reject(error)
  )

  client.interceptors.response.use(
    (response) => response,
    (error) => {
      if (error.response?.data) return Promise.reject(error.response.data as ApiError)
      return Promise.reject(error)
    }
  )

  return {
    async get<T>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
      const response: AxiosResponse<ApiResponse<T>> = await client.get(url, config)
      return response.data
    },
    /** Returns { ok, data } so callers can check ok before setting state (avoids treating 4xx/5xx body as data). */
    async getSafe<T>(url: string, config?: AxiosRequestConfig): Promise<{ ok: boolean; data: T | null }> {
      try {
        const response = await client.get<ApiResponse<T>>(url, { ...config, validateStatus: () => true })
        const ok = response.status >= 200 && response.status < 300
        const data = ok && response.data ? (response.data as ApiResponse<T>).data ?? null : null
        return { ok, data }
      } catch {
        return { ok: false, data: null }
      }
    },
    async post<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
      const response: AxiosResponse<ApiResponse<T>> = await client.post(url, data, config)
      return response.data
    },
    async put<T>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
      const response: AxiosResponse<ApiResponse<T>> = await client.put(url, data, config)
      return response.data
    },
    async delete<T>(url: string, config?: AxiosRequestConfig): Promise<ApiResponse<T>> {
      const response: AxiosResponse<ApiResponse<T>> = await client.delete(url, config)
      return response.data
    },
  }
}
