/**
 * Workflow Service
 * API service for workflow operations
 */
import { apiClient } from '@/shared/api/client';
import {
  Workflow,
  CreateWorkflowDto,
  UpdateWorkflowItemDto,
  ApprovalRequestDto,
} from '../types/workflow.types';
import { ListResponse, PaginationParams } from '@/shared/types/api.types';

export const workflowService = {
  /**
   * Get all workflows with optional pagination and filtering
   */
  async getAll(
    params?: PaginationParams & { project_name?: string }
  ): Promise<ListResponse<Workflow>> {
    return apiClient.get<ListResponse<Workflow>>('/api/v1/workflows', {
      params,
    });
  },

  /**
   * Get workflow by ID
   */
  async getById(id: string): Promise<Workflow> {
    return apiClient.get<Workflow>(`/api/v1/workflows/${id}`);
  },

  /**
   * Create a new workflow
   */
  async create(data: CreateWorkflowDto): Promise<Workflow> {
    return apiClient.post<Workflow>('/api/v1/workflows', data);
  },

  /**
   * Update a workflow item
   */
  async updateItem(
    workflowId: string,
    data: UpdateWorkflowItemDto
  ): Promise<Workflow> {
    return apiClient.put<Workflow>(
      `/api/v1/workflows/${workflowId}/items`,
      data
    );
  },

  /**
   * Submit an approval
   */
  async submitApproval(
    workflowId: string,
    data: ApprovalRequestDto
  ): Promise<Workflow> {
    return apiClient.post<Workflow>(
      `/api/v1/workflows/${workflowId}/approvals`,
      data
    );
  },

  /**
   * Advance workflow to next phase
   */
  async advancePhase(workflowId: string): Promise<Workflow> {
    return apiClient.post<Workflow>(
      `/api/v1/workflows/${workflowId}/advance`
    );
  },

  /**
   * Delete a workflow
   */
  async delete(id: string): Promise<void> {
    return apiClient.delete(`/api/v1/workflows/${id}`);
  },
};
