import apiClient from './client'

export enum JobStatus {
  PENDING = 'pending',
  STARTED = 'started',
  PROGRESS = 'progress',
  SUCCESS = 'success',
  FAILURE = 'failure',
  CANCELLED = 'cancelled',
}

export interface JobResponse {
  id: string
  status: JobStatus
  progress: number
  message: string
  created_at: string
  updated_at: string
}

export const jobsApi = {
  getJob: async (jobId: string): Promise<JobResponse> => {
    const { data } = await apiClient.get<JobResponse>(
      `/api/v1/jobs/${jobId}`
    )
    return data
  },

  streamJobProgress: (jobId: string): EventSource => {
    // EventSource needs absolute URL - use VITE_API_URL or fallback to direct backend URL
    const baseURL = import.meta.env.VITE_API_URL || 'http://127.0.0.1:8000'
    return new EventSource(`${baseURL}/api/v1/jobs/stream/${jobId}`)
  },

  cancelJob: async (jobId: string): Promise<{ message: string; status: string }> => {
    const { data } = await apiClient.delete<{ message: string; status: string }>(
      `/api/v1/jobs/${jobId}`
    )
    return data
  },
}

