import { JsonRelationship, TableRelationship, DataFlowDiagram } from "@/lib/api/dataflow";

// 테이블 노드 데이터 타입 정의
export interface TableNodeData extends Record<string, unknown> {
  table: {
    table_name: string;
    display_name: string;
    description: string;
    columns: Array<{
      name: string;
      type: string;
      description: string;
    }>;
  };
  onColumnClick: (tableName: string, columnName: string) => void;
  selected_columns: string[];
  connected_columns?: {
    [columnName: string]: { direction: "source" | "target" | "both" };
  };
}

// 내부에서 사용할 확장된 JsonRelationship 타입 (connection_type 포함)
export interface ExtendedJsonRelationship extends JsonRelationship {
  connection_type: "simple-key" | "data-save" | "external-call";
  note?: string; // 데이터 연결에 대한 설명
  settings?: {
    control?: {
      trigger_type?: "insert" | "update" | "delete";
      condition_tree?: Array<{
        id: string;
        type: string;
        field?: string;
        operator?: string;
        value?: unknown;
        logical_operator?: string;
        group_id?: string;
        group_level?: number;
      }>;
    };
    actions?: Array<{
      id: string;
      name: string;
      action_type: "insert" | "update" | "delete" | "upsert";
      conditions?: Array<{
        id: string;
        type: string;
        field?: string;
        operator?: string;
        value?: unknown;
        logical_operator?: string;
        group_id?: string;
        group_level?: number;
      }>;
      field_mappings: Array<{
        source_table?: string;
        source_field: string;
        target_table?: string;
        target_field: string;
        default_value?: string;
      }>;
      split_config?: {
        source_field: string;
        delimiter: string;
        target_field: string;
      };
    }>;
    notes?: string;
    api_call?: {
      url: string;
      method: string;
      headers: Array<{ key: string; value: string }>;
      body: string;
      success_criteria: string;
    };
  };
}

// DataFlowDesigner Props 타입
export interface DataFlowDesignerProps {
  company_code?: string;
  onSave?: (relationships: TableRelationship[]) => void;
  selected_diagram?: DataFlowDiagram | string | null;
  diagram_id?: number;
  relationship_id?: string; // 하위 호환성 유지
  onBackToList?: () => void;
  onDiagramNameUpdate?: (diagramName: string) => void;
}

// 연결 정보 타입
export interface ConnectionInfo {
  from_node: { id: string; table_name: string; display_name: string };
  to_node: { id: string; table_name: string; display_name: string };
  from_column?: string;
  to_column?: string;
  selected_columns_data?: {
    [tableName: string]: {
      display_name: string;
      columns: string[];
    };
  };
  existing_relationship?: {
    relationship_name: string;
    connection_type: string;
    settings?: Record<string, unknown>;
  };
}

// 선택된 엣지 정보 타입
export interface SelectedEdgeInfo {
  relationship_id: string;
  relationship_name: string;
  from_table: string;
  to_table: string;
  from_columns: string[];
  to_columns: string[];
  connection_type: string;
  connection_info: string;
}
