// Workflow type definitions

export type WorkflowStatus = 'active' | 'inactive';

/**
 * Primitive config value types that can be stored in workflow config
 */
export type ConfigValue = string | number | boolean | null | undefined;

/**
 * Complex config value for key-value fields
 */
export type KeyValueConfig = Record<string, string>;

/**
 * Array of key-value entries (for serialization)
 */
export type KeyValueEntries = Array<{ key: string; value: string }>;

/**
 * Type-safe config object for workflow nodes
 * Allows primitive values and key-value pairs
 */
export type NodeConfig = Record<string, ConfigValue | ConfigValue[] | KeyValueConfig | KeyValueEntries>;

/**
 * Trigger configuration within a workflow
 */
export interface TriggerConfig {
  /** Trigger type identifier */
  type: string;
  /** Trigger-specific configuration */
  config: NodeConfig;
  /** Display name for the trigger */
  name?: string;
  /** Integration service identifier */
  integration?: string;
  /** Trigger filter conditions (optional, stored separately from config) */
  filters?: Record<string, unknown> | null;
}

/**
 * Action configuration within a workflow
 */
export interface ActionConfig {
  /** Unique action instance ID */
  id: string;
  /** Action type identifier */
  type: string;
  /** User-defined label for namespaced context (e.g., "CreatePost", "SendEmail") */
  label?: string;
  /** Action-specific configuration */
  config: NodeConfig;
  /** Display name for the action */
  name?: string;
  /** Integration service identifier */
  integration?: string;
  /** Connection ID for multi-account support */
  connection_id?: string;
  /** Next action pointer: string = single next, Record = route map, null = terminal */
  next?: string | Record<string, string> | null;
}

/**
 * Node position in the workflow canvas
 */
export interface NodePosition {
  id: string;
  position: { x: number; y: number };
}

/**
 * Edge connection between nodes
 */
export interface EdgeConnection {
  id: string;
  source: string;
  target: string;
  sourceHandle?: string;
}

/**
 * Workflow layout for visual representation
 */
export interface WorkflowLayout {
  nodes: NodePosition[];
  edges: EdgeConnection[];
}

/**
 * Complete workflow configuration
 */
export interface WorkflowConfig {
  trigger: TriggerConfig;
  /** Actions map keyed by action ID */
  actions: Record<string, ActionConfig>;
  /** ID of the first action to execute */
  entry_action?: string;
  layout?: WorkflowLayout;
}

export interface Workflow {
  id: number;
  name: string;
  description: string;
  trigger_type: string;
  draft_config: WorkflowConfig;
  status: WorkflowStatus;
  created_at: string;
  updated_at: string;
  /** ID of the currently published version (null if never published) */
  published_version_id: number | null;
  /** Timestamp of last publish */
  last_published_at: string | null;
  /** Whether there are unpublished changes (returned by API on load) */
  has_unpublished_changes?: boolean;
}

/**
 * A published version of a workflow
 */
export interface Version {
  /** Unique version ID */
  id: number;
  /** Workflow this version belongs to */
  workflow_id: number;
  /** Sequential version number (1, 2, 3...) */
  version_number: number;
  /** Frozen configuration at time of publish */
  config: WorkflowConfig;
  /** When this version was created */
  created_at: string;
  /** User who published this version (null for system) */
  created_by: number | null;
  /** Optional notes describing the version */
  notes: string;
  /** Whether this is the currently published version */
  is_published?: boolean;
}

/**
 * Response from publish endpoint
 */
export interface PublishResponse {
  success: boolean;
  version: Version;
  message: string;
}

/**
 * Response from restore version endpoint
 */
export interface RestoreResponse {
  success: boolean;
  message: string;
  workflow: Workflow;
  restored_version: number;
  is_active: boolean;
}

/**
 * Response from make version live endpoint
 */
export interface MakeVersionLiveResponse {
  success: boolean;
  version: Version;
  message: string;
}

/**
 * Workflow publishing status from status endpoint
 */
export interface WorkflowVersionStatus {
  has_unpublished_changes: boolean;
  published_version_id: number | null;
  last_published_at: string | null;
  published_version: {
    id: number;
    version_number: number;
    created_at: string;
    notes: string;
  } | null;
  version_count: number;
}
