/**
 * Shared types for step execution
 */

import { Id } from '../../../_generated/dataModel';
import { StepExecutionResult } from '../../types';

export type StepType =
  | 'start'
  | 'trigger'
  | 'llm'
  | 'condition'
  | 'action'
  | 'loop'
  | 'output';

export interface StepDefinition {
  stepSlug: string;
  name: string;
  stepType: StepType;
  config: Record<string, unknown>;
  organizationId: string;
}

export interface ExecutionData {
  _id: Id<'wfExecutions'>;
  wfDefinitionId?: string | null;
  rootWfDefinitionId?: string; // Root version of the workflow family
  userId?: string;
  stepsConfig?: Record<string, unknown>;
  workflowConfig?: WorkflowConfig;
  variables?: Record<string, unknown>;
  workflowSlug?: string; // Auto-generated from workflow name (snake_case)
}

export interface WorkflowConfig {
  name?: string; // Workflow name (used to generate workflowId)
  description?: string; // Workflow description
  version?: string; // Workflow version
  workflowType?: 'predefined'; // Workflow type
  config?: {
    variables?: Record<string, unknown>;
    secrets?: Record<
      string,
      {
        kind: 'inlineEncrypted';
        cipherText: string;
        keyId?: string;
      }
    >;
    timeout?: number;
    retryPolicy?: {
      maxRetries: number;
      backoffMs: number;
    };
    /**
     * Workflow-level fallback chain inherited by every LLM step that defines
     * neither `model` nor `models`. Step-level overrides win.
     */
    models?: string[];
  };
}

export interface StepConfig {
  [key: string]: unknown;
}

export interface InitializeVariablesArgs {
  executionId: string;
  organizationId: string;
  resumeVariables?: unknown;
  initialInput?: unknown;
}

export interface LoadExecutionResult {
  execution: ExecutionData;
  stepConfig: StepConfig;
  workflowConfig: WorkflowConfig;
}

export type { StepExecutionResult };
