/**
 * Type definitions for workflow executions
 */

import type { Doc, Id } from '../../_generated/dataModel';
import type { ConvexJsonValue } from '../../lib/validators/json';

// =============================================================================
// MANUAL TYPES (these rely on Doc types, not validators)
// =============================================================================

export type WorkflowExecution = Doc<'wfExecutions'>;
export type ExecutionStatus = WorkflowExecution['status'];
export type ExecutionVariables = Record<string, unknown>;

/**
 * Coarse run-level failure classification, persisted on `wfExecutions` at the
 * failure chokepoints. `invalid_input` is reserved for the test-run input
 * preflight, which currently rejects before an execution record exists.
 */
export type ExecutionErrorCode =
  | 'start_failure'
  | 'step_failure'
  | 'timeout'
  | 'canceled'
  | 'invalid_input';

export interface DeserializedWorkflowExecution extends Omit<
  WorkflowExecution,
  'workflowConfig' | 'stepsConfig' | 'variables' | 'error'
> {
  workflowConfig: unknown;
  stepsConfig: Record<string, unknown>;
  variables: ExecutionVariables;
  error?: string;
}

export interface UpdateExecutionStatusArgs {
  executionId: Doc<'wfExecutions'>['_id'];
  status: ExecutionStatus;
  currentStepSlug?: string;
  currentStepName?: string;
  loopProgress?: { current: number; total: number } | null;
  waitingFor?: string;
  error?: string;
  errorCode?: ExecutionErrorCode;
}

export interface CompleteExecutionArgs {
  executionId: Doc<'wfExecutions'>['_id'];
  output: ConvexJsonValue;
  outputStorageId?: Id<'_storage'>;
  variablesSerialized?: string;
  variablesStorageId?: Id<'_storage'>;
}

export interface FailExecutionArgs {
  executionId: Doc<'wfExecutions'>['_id'];
  error: string;
  errorCode?: ExecutionErrorCode;
}

export interface PatchExecutionArgs {
  executionId: Doc<'wfExecutions'>['_id'];
  updates: {
    threadId?: string;
    currentStepSlug?: string;
    variables?: string;
    metadata?: string;
  };
}

export interface ResumeExecutionArgs {
  executionId: Doc<'wfExecutions'>['_id'];
  variablesSerialized?: string;
  variablesStorageId?: Id<'_storage'>;
  metadata?: Record<string, unknown>;
}

export interface SetComponentWorkflowArgs {
  executionId: Doc<'wfExecutions'>['_id'];
  componentWorkflowId: string;
}

export interface UpdateExecutionMetadataArgs {
  executionId: Doc<'wfExecutions'>['_id'];
  metadata: Record<string, unknown>;
}

export interface UpdateExecutionVariablesArgs {
  executionId: Doc<'wfExecutions'>['_id'];
  // Must be pre-serialized in an action
  variablesSerialized?: string;
  variablesStorageId?: Id<'_storage'>;
}

export interface ListExecutionsArgs {
  wfDefinitionId: string;
  status?: string;
  limit?: number;
  search?: string;
  triggeredBy?: string;
  dateFrom?: string;
  dateTo?: string;
}

// =============================================================================
// CURSOR-BASED PAGINATION (for infinite scroll)
// =============================================================================

export interface ListExecutionsCursorArgs {
  wfDefinitionId: string;
  /** Number of items to fetch per page */
  numItems?: number;
  /** Cursor for pagination (null for first page) */
  cursor: string | null;
  /** Search term to filter by execution ID */
  searchTerm?: string;
  /** Filter by status values */
  status?: string[];
  /** Filter by triggeredBy value */
  triggeredBy?: string;
  /** ISO date string for start of date range */
  dateFrom?: string;
  /** ISO date string for end of date range */
  dateTo?: string;
}

export interface CursorPaginatedExecutionsResult {
  page: WorkflowExecution[];
  isDone: boolean;
  continueCursor: string;
}
