import { useAuthStore } from '@/stores/auth'

const API_BASE = '/api'

interface RequestOptions {
  method?: string
  body?: unknown
  headers?: Record<string, string>
}

/**
 * Composable for making authenticated API requests.
 * Automatically attaches JWT token and handles token refresh.
 */
export function useApi() {
  const auth = useAuthStore()

  async function request<T>(path: string, options: RequestOptions = {}): Promise<T> {
    const headers: Record<string, string> = {
      'Content-Type': 'application/json',
      ...options.headers,
    }

    if (auth.accessToken) {
      headers['Authorization'] = `Bearer ${auth.accessToken}`
    }

    const response = await fetch(`${API_BASE}${path}`, {
      method: options.method || 'GET',
      headers,
      body: options.body ? JSON.stringify(options.body) : undefined,
    })

    if (response.status === 401) {
      // TODO: Attempt token refresh, retry, or redirect to login
      auth.logout()
      window.location.href = '/login'
      throw new Error('Unauthorized')
    }

    if (!response.ok) {
      const error = await response.json().catch(() => ({ message: 'Request failed' }))
      throw new Error(error.message || `HTTP ${response.status}`)
    }

    return response.json()
  }

  function get<T>(path: string) {
    return request<T>(path)
  }

  function post<T>(path: string, body?: unknown) {
    return request<T>(path, { method: 'POST', body })
  }

  function put<T>(path: string, body?: unknown) {
    return request<T>(path, { method: 'PUT', body })
  }

  function del<T>(path: string) {
    return request<T>(path, { method: 'DELETE' })
  }

  return { request, get, post, put, del }
}
