// Workflow service layer for CRUD operations

import { apiRequest } from './api';
import { Workflow } from '../types/workflow';
import { Trigger } from '../types/trigger';
import { Action } from '../types/action';

interface GetWorkflowsFilters {
  status?: string;
  trigger_type?: string;
  page?: number;
  per_page?: number;
}

/**
 * Get all workflows with optional filters
 */
export async function getWorkflows(filters?: GetWorkflowsFilters): Promise<Workflow[]> {
  // Build query string from filters
  const params = new URLSearchParams();
  if (filters?.status) params.append('status', filters.status);
  if (filters?.trigger_type) params.append('trigger_type', filters.trigger_type);
  if (filters?.page) params.append('page', filters.page.toString());
  if (filters?.per_page) params.append('per_page', filters.per_page.toString());

  const queryString = params.toString();
  const endpoint = queryString ? `/workflows?${queryString}` : '/workflows';

  return apiRequest<Workflow[]>(endpoint, { method: 'GET' });
}

/**
 * Get a single workflow by ID
 */
export async function getWorkflow(id: number): Promise<Workflow> {
  return apiRequest<Workflow>(`/workflows/${id}`, { method: 'GET' });
}

/**
 * Create a new workflow
 */
export async function createWorkflow(data: Partial<Workflow>): Promise<Workflow> {
  return apiRequest<Workflow>('/workflows', {
    method: 'POST',
    body: JSON.stringify(data),
  });
}

/**
 * Update an existing workflow
 */
export async function updateWorkflow(id: number, data: Partial<Workflow>): Promise<Workflow> {
  return apiRequest<Workflow>(`/workflows/${id}`, {
    method: 'PUT',
    body: JSON.stringify(data),
  });
}

/**
 * Rename a workflow (lightweight name-only update)
 */
export async function renameWorkflow(id: number, name: string): Promise<Workflow> {
  return apiRequest<Workflow>(`/workflows/${id}`, {
    method: 'PUT',
    body: JSON.stringify({ name }),
  });
}

/**
 * Delete a workflow
 */
export async function deleteWorkflow(id: number): Promise<void> {
  return apiRequest<void>(`/workflows/${id}`, { method: 'DELETE' });
}

/**
 * Activate a workflow
 */
export async function activateWorkflow(id: number): Promise<Workflow> {
  return apiRequest<Workflow>(`/workflows/${id}/activate`, { method: 'POST' });
}

/**
 * Deactivate a workflow
 */
export async function deactivateWorkflow(id: number): Promise<Workflow> {
  return apiRequest<Workflow>(`/workflows/${id}/deactivate`, { method: 'POST' });
}

/**
 * Get all available triggers with metadata
 */
export async function getTriggers(): Promise<Trigger[]> {
  return apiRequest<Trigger[]>('/triggers', { method: 'GET' });
}

/**
 * Get all available actions with metadata
 *
 * Fetches the complete list of registered action handlers from the WordPress backend,
 * including their configuration schemas. This enables dynamic action discovery and
 * form generation in the workflow builder.
 *
 * @returns Promise resolving to array of Action objects with type, label, description, and schema
 */
export async function getActions(): Promise<Action[]> {
  return apiRequest<Action[]>('/actions', { method: 'GET' });
}

// Export all functions as a service object
export const workflowService = {
  getWorkflows,
  getWorkflow,
  createWorkflow,
  updateWorkflow,
  renameWorkflow,
  deleteWorkflow,
  activateWorkflow,
  deactivateWorkflow,
  getTriggers,
  getActions,
};
