import client from './client';
import type { PaginatedResponse } from './types';

export interface TaskInfo {
  id: string;
  instance_id: string;
  token_id: string;
  node_id: string;
  node_name?: string;
  assignee_id?: string;
  candidate_groups?: unknown;
  status: string;
  outcome?: string;
  form_data?: unknown;
  due_date?: string;
  completed_at?: string;
  created_at: string;
  definition_name?: string;
  business_key?: string;
}

export interface CompleteTaskRequest {
  outcome: string;
  form_data?: Record<string, unknown>;
}

export interface DelegateTaskRequest {
  delegate_to: string;
}

export async function listPendingTasks(page = 1, pageSize = 20) {
  const { data } = await client.get<{ success: boolean; data: PaginatedResponse<TaskInfo> }>(
    '/workflow/tasks/pending',
    { params: { page, page_size: pageSize } },
  );
  return data.data;
}

export async function listCompletedTasks(page = 1, pageSize = 20) {
  const { data } = await client.get<{ success: boolean; data: PaginatedResponse<TaskInfo> }>(
    '/workflow/tasks/completed',
    { params: { page, page_size: pageSize } },
  );
  return data.data;
}

export async function completeTask(id: string, req: CompleteTaskRequest) {
  const { data } = await client.post<{ success: boolean; data: TaskInfo }>(
    `/workflow/tasks/${id}/complete`,
    req,
  );
  return data.data;
}

export async function delegateTask(id: string, req: DelegateTaskRequest) {
  const { data } = await client.post<{ success: boolean; data: TaskInfo }>(
    `/workflow/tasks/${id}/delegate`,
    req,
  );
  return data.data;
}
