/**
 * Action metadata types for dynamic action configuration
 *
 * These types mirror the structure returned by the /sequensy/v1/actions REST API endpoint
 * and enable dynamic form generation based on action schemas.
 */

import { ContextVariableType } from './workflow-context';
import type { VisibilityCondition, RepeatableConfig, FilterGroupConfig } from './schema';

/**
 * Supported field types for action configuration schemas
 */
export type ActionFieldType =
  | 'string'
  | 'text'
  | 'number'
  | 'boolean'
  | 'select'
  | 'key_value'
  | 'repeatable'
  | 'filter_group'
  | 'richtext'
  | 'button_group'
  | 'multi_select'
  | 'date';

/**
 * Supported data types for action output fields
 */
export type ActionOutputFieldType = 'string' | 'integer' | 'boolean' | 'datetime' | 'array';

/**
 * Option for select-type fields
 */
export interface ActionFieldOption {
  value: string;
  label: string;
  description?: string;
  icon?: string;
  disabled?: boolean;
  group?: string;
}

/**
 * Schema definition for a single action configuration field
 */
export interface ActionField {
  type: ActionFieldType;
  label: string;
  description: string;
  placeholder?: string; // Placeholder text for input fields
  required: boolean;
  options?: ActionFieldOption[]; // Only present for static select-type fields
  default?: string | number | boolean | string[] | null; // Default value for the field
  // Dynamic field properties (for integration-powered dropdowns)
  dynamic?: boolean; // If true, fetch options from endpoint
  endpoint?: string; // REST API endpoint to fetch options from
  value_field?: string; // Field in API response to use as option value
  label_field?: string; // Field in API response to use as option label
  // Key-value field properties (for custom fields)
  dynamic_options?: boolean; // If true, fetch key options from endpoint
  depends_on?: string; // Field that this depends on (e.g., list_id)
  key_field?: string; // Field in API response to use as key value
  // Ordering and organization
  order?: number; // Field display order (lower numbers appear first)
  // Combined selector properties
  selector_for?: string; // This browse field populates the target ID field
  hidden?: boolean; // Hide from UI (merged into another field)
  // Variable type filtering (for number fields that only accept integer variables)
  allowed_variable_types?: ContextVariableType[]; // Restricts variable picker to these types
  // Conditional visibility - show field only when condition is met
  visible_when?: VisibilityCondition;
  // Field grouping and layout
  group?: string; // Group identifier for organizing related fields
  /** Layout width: 'full' (default) or '1/2' for half-width side-by-side fields */
  width?: 'full' | '1/2';
  // Repeatable field group configuration (for type: 'repeatable')
  repeatable?: RepeatableConfig;
  item_schema?: Record<string, ActionField>; // Template schema for repeatable items
  // Filter group configuration (for type: 'filter_group')
  filter_config?: FilterGroupConfig;
}

/**
 * Output field definition for an action
 *
 * Describes the data fields this action produces after execution.
 * Used by the workflow builder to show available variables for subsequent nodes.
 */
export interface ActionOutputField {
  /** Field key (e.g., 'post_id', 'permalink') */
  name: string;
  /** Data type for validation and display */
  type: ActionOutputFieldType;
  /** Human-readable description */
  description: string;
  /** Example value for preview */
  example: string;
}

/**
 * Complete action configuration schema
 * Keys are field names, values are field definitions
 */
export type ActionSchema = Record<string, ActionField>;

/**
 * Complete action metadata from the API
 */
export interface Action {
  type: string; // Unique action type identifier (e.g., 'send_email')
  label: string; // Human-readable action name (e.g., 'Send Email')
  description: string; // Action description for UI display
  integration: string; // Integration identifier for icon mapping (e.g., 'campaignmonitor', 'wordpress')
  schema: ActionSchema; // Configuration schema for dynamic form generation
  output_fields: ActionOutputField[]; // Output fields produced by this action
  is_available: boolean; // Whether the integration's required plugin is available
  availability_reason: string; // Reason if unavailable (e.g., "WooCommerce plugin is not installed")
  plugin_type?: 'free' | 'paid' | ''; // Plugin type: free (WordPress.org) or paid (external)
  install_url?: string; // URL to install/purchase the required plugin
  is_connected: boolean; // Whether the integration has at least one connected account
  requires_connection: boolean; // Whether the integration requires an API connection (third-party services)
}
