/**
 * Workflow API client
 */

import type {
  WorkflowDefinition,
  WorkflowSummary,
  WorkflowExecution,
} from '@/utils/workflowSerializer'
import { BaseApiClient } from './base'

const API_BASE = '/api/v1'

class WorkflowApiClient extends BaseApiClient {
  constructor(baseUrl: string = API_BASE) {
    super(baseUrl)
  }

  /** List all workflows */
  async listWorkflows(limit: number = 50): Promise<{ workflows: WorkflowSummary[]; total: number }> {
    return this.request(`/workflows?limit=${limit}`)
  }

  /** Create a new workflow */
  async createWorkflow(data: {
    name: string
    stages: unknown[]
    triggers?: unknown[]
    variables_schema?: Record<string, unknown>
    output_schema?: Record<string, unknown>
  }): Promise<WorkflowDefinition> {
    return this.request('/workflows', {
      method: 'POST',
      body: JSON.stringify(data),
    })
  }

  /** Get a workflow by ID */
  async getWorkflow(workflowId: string): Promise<WorkflowDefinition> {
    return this.request(`/workflows/${workflowId}`)
  }

  /** Update a workflow */
  async updateWorkflow(
    workflowId: string,
    data: {
      name: string
      stages: unknown[]
      triggers?: unknown[]
      variables_schema?: Record<string, unknown>
      output_schema?: Record<string, unknown>
    }
  ): Promise<WorkflowDefinition> {
    return this.request(`/workflows/${workflowId}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    })
  }

  /** Delete a workflow */
  async deleteWorkflow(workflowId: string): Promise<void> {
    await this.request(`/workflows/${workflowId}`, {
      method: 'DELETE',
    })
  }

  /** Execute a workflow */
  async executeWorkflow(
    workflowId: string,
    variables?: Record<string, unknown>
  ): Promise<{ execution_id: string; workflow_id: string; status: string }> {
    return this.request(`/workflows/${workflowId}/execute`, {
      method: 'POST',
      body: JSON.stringify({ variables: variables || {} }),
    })
  }

  /** Get execution status */
  async getExecution(executionId: string): Promise<WorkflowExecution> {
    return this.request(`/workflows/executions/${executionId}`)
  }

  /** Approve a paused execution */
  async approveExecution(
    executionId: string,
    approved: boolean,
    comment?: string
  ): Promise<WorkflowExecution> {
    return this.request(`/workflows/executions/${executionId}/approve`, {
      method: 'POST',
      body: JSON.stringify({ approved, comment }),
    })
  }

  /** Cancel an execution */
  async cancelExecution(executionId: string): Promise<WorkflowExecution> {
    return this.request(`/workflows/executions/${executionId}/cancel`, {
      method: 'POST',
    })
  }

  /** List execution history for a specific workflow */
  async listExecutions(
    workflowId: string,
    limit: number = 50,
    offset: number = 0
  ): Promise<{ executions: WorkflowExecution[]; total: number }> {
    return this.request(`/workflows/${workflowId}/executions?limit=${limit}&offset=${offset}`)
  }

  /** Create a WebSocket connection for real-time workflow progress */
  createWorkflowWebSocket(): WebSocket {
    return super.createWebSocket('/workflows/ws')
  }
}

export const workflowApi = new WorkflowApiClient()
export { WorkflowApiClient }
