/**
 * Builds graphicalEditor-compatible graph structures for trustee pipeline execution.
 *
 * The consolidated automation system runs all workflows through the graphicalEditor
 * execution engine (POST /api/workflows/{instanceId}/execute). These helpers build
 * the graph format expected by that engine: { nodes, connections } with _method/_action
 * mappings to the unified Action Library.
 */

interface TrusteeGraphNode {
  id: string;
  type: string;
  label: string;
  _method: string;
  _action: string;
  parameters: Record<string, unknown>;
  position: { x: number; y: number };
}

interface TrusteeGraphConnection {
  source: string;
  sourcePort: number;
  target: string;
  targetPort: number;
}

export interface TrusteeGraph {
  nodes: TrusteeGraphNode[];
  connections: TrusteeGraphConnection[];
}

/**
 * Build a graph for the scan/upload pipeline (UC1):
 * trigger.manual → trustee.extractFromFiles → trustee.processDocuments → trustee.syncToAccounting
 */
export function _buildScanUploadGraph(
  trusteeInstanceId: string,
  fileIds: string[],
  extractionPrompt: string
): TrusteeGraph {
  const nodes: TrusteeGraphNode[] = [
    {
      id: 'trigger-manual',
      type: 'trigger.manual',
      label: 'Start',
      _method: '',
      _action: '',
      parameters: {},
      position: { x: 0, y: 0 },
    },
    {
      id: 'extract',
      type: 'trustee.extractFromFiles',
      label: 'Extract Documents',
      _method: 'trustee',
      _action: 'extractFromFiles',
      parameters: {
        fileIds,
        featureInstanceId: trusteeInstanceId,
        prompt: extractionPrompt,
      },
      position: { x: 250, y: 0 },
    },
    {
      id: 'process',
      type: 'trustee.processDocuments',
      label: 'Process Documents',
      _method: 'trustee',
      _action: 'processDocuments',
      parameters: {
        documentList: [],
        featureInstanceId: trusteeInstanceId,
      },
      position: { x: 500, y: 0 },
    },
    {
      id: 'sync',
      type: 'trustee.syncToAccounting',
      label: 'Sync to Accounting',
      _method: 'trustee',
      _action: 'syncToAccounting',
      parameters: {
        documentList: [],
        featureInstanceId: trusteeInstanceId,
      },
      position: { x: 750, y: 0 },
    },
  ];

  const connections: TrusteeGraphConnection[] = [
    { source: 'trigger-manual', sourcePort: 0, target: 'extract', targetPort: 0 },
    { source: 'extract', sourcePort: 0, target: 'process', targetPort: 0 },
    { source: 'process', sourcePort: 0, target: 'sync', targetPort: 0 },
  ];

  return { nodes, connections };
}

/**
 * Build a graph for the expense import pipeline (SharePoint-based):
 * trigger.manual → trustee.extractFromFiles (SharePoint) → trustee.processDocuments → trustee.syncToAccounting
 */
export function _buildExpenseImportGraph(
  trusteeInstanceId: string,
  connectionReference: string,
  sharepointFolder: string,
  extractionPrompt: string
): TrusteeGraph {
  const nodes: TrusteeGraphNode[] = [
    {
      id: 'trigger-manual',
      type: 'trigger.manual',
      label: 'Start',
      _method: '',
      _action: '',
      parameters: {},
      position: { x: 0, y: 0 },
    },
    {
      id: 'extract',
      type: 'trustee.extractFromFiles',
      label: 'Extract from SharePoint',
      _method: 'trustee',
      _action: 'extractFromFiles',
      parameters: {
        connectionReference,
        sharepointFolder,
        featureInstanceId: trusteeInstanceId,
        prompt: extractionPrompt,
      },
      position: { x: 250, y: 0 },
    },
    {
      id: 'process',
      type: 'trustee.processDocuments',
      label: 'Process Documents',
      _method: 'trustee',
      _action: 'processDocuments',
      parameters: {
        documentList: [],
        featureInstanceId: trusteeInstanceId,
      },
      position: { x: 500, y: 0 },
    },
    {
      id: 'sync',
      type: 'trustee.syncToAccounting',
      label: 'Sync to Accounting',
      _method: 'trustee',
      _action: 'syncToAccounting',
      parameters: {
        documentList: [],
        featureInstanceId: trusteeInstanceId,
      },
      position: { x: 750, y: 0 },
    },
  ];

  const connections: TrusteeGraphConnection[] = [
    { source: 'trigger-manual', sourcePort: 0, target: 'extract', targetPort: 0 },
    { source: 'extract', sourcePort: 0, target: 'process', targetPort: 0 },
    { source: 'process', sourcePort: 0, target: 'sync', targetPort: 0 },
  ];

  return { nodes, connections };
}

/**
 * Build a scheduled workflow graph for daily expense import:
 * trigger.schedule (cron) → trustee.extractFromFiles → trustee.processDocuments → trustee.syncToAccounting
 */
export function _buildScheduledExpenseImportGraph(
  trusteeInstanceId: string,
  connectionReference: string,
  sharepointFolder: string,
  extractionPrompt: string,
  cronExpression: string
): TrusteeGraph {
  const nodes: TrusteeGraphNode[] = [
    {
      id: 'trigger-schedule',
      type: 'trigger.schedule',
      label: 'Daily Schedule',
      _method: '',
      _action: '',
      parameters: {
        cron: cronExpression,
        enabled: true,
      },
      position: { x: 0, y: 0 },
    },
    {
      id: 'extract',
      type: 'trustee.extractFromFiles',
      label: 'Extract from SharePoint',
      _method: 'trustee',
      _action: 'extractFromFiles',
      parameters: {
        connectionReference,
        sharepointFolder,
        featureInstanceId: trusteeInstanceId,
        prompt: extractionPrompt,
      },
      position: { x: 250, y: 0 },
    },
    {
      id: 'process',
      type: 'trustee.processDocuments',
      label: 'Process Documents',
      _method: 'trustee',
      _action: 'processDocuments',
      parameters: {
        documentList: [],
        featureInstanceId: trusteeInstanceId,
      },
      position: { x: 500, y: 0 },
    },
    {
      id: 'sync',
      type: 'trustee.syncToAccounting',
      label: 'Sync to Accounting',
      _method: 'trustee',
      _action: 'syncToAccounting',
      parameters: {
        documentList: [],
        featureInstanceId: trusteeInstanceId,
      },
      position: { x: 750, y: 0 },
    },
  ];

  const connections: TrusteeGraphConnection[] = [
    { source: 'trigger-schedule', sourcePort: 0, target: 'extract', targetPort: 0 },
    { source: 'extract', sourcePort: 0, target: 'process', targetPort: 0 },
    { source: 'process', sourcePort: 0, target: 'sync', targetPort: 0 },
  ];

  return { nodes, connections };
}
