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

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

// 내부에서 사용할 확장된 JsonRelationship 타입 (connectionType 포함)
export interface ExtendedJsonRelationship extends JsonRelationship {
  connectionType: "simple-key" | "data-save" | "external-call";
  note?: string; // 데이터 연결에 대한 설명
  settings?: {
    control?: {
      triggerType?: "insert" | "update" | "delete";
      conditionTree?: Array<{
        id: string;
        type: string;
        field?: string;
        operator?: string;
        value?: unknown;
        logicalOperator?: string;
        groupId?: string;
        groupLevel?: number;
      }>;
    };
    actions?: Array<{
      id: string;
      name: string;
      actionType: "insert" | "update" | "delete" | "upsert";
      conditions?: Array<{
        id: string;
        type: string;
        field?: string;
        operator?: string;
        value?: unknown;
        logicalOperator?: string;
        groupId?: string;
        groupLevel?: number;
      }>;
      fieldMappings: Array<{
        sourceTable?: string;
        sourceField: string;
        targetTable?: string;
        targetField: string;
        defaultValue?: string;
      }>;
      splitConfig?: {
        sourceField: string;
        delimiter: string;
        targetField: string;
      };
    }>;
    notes?: string;
    apiCall?: {
      url: string;
      method: string;
      headers: Array<{ key: string; value: string }>;
      body: string;
      successCriteria: string;
    };
  };
}

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

// 연결 정보 타입
export interface ConnectionInfo {
  fromNode: { id: string; tableName: string; displayName: string };
  toNode: { id: string; tableName: string; displayName: string };
  fromColumn?: string;
  toColumn?: string;
  selectedColumnsData?: {
    [tableName: string]: {
      displayName: string;
      columns: string[];
    };
  };
  existingRelationship?: {
    relationshipName: string;
    connectionType: string;
    settings?: Record<string, unknown>;
  };
}

// 선택된 엣지 정보 타입
export interface SelectedEdgeInfo {
  relationshipId: string;
  relationshipName: string;
  fromTable: string;
  toTable: string;
  fromColumns: string[];
  toColumns: string[];
  connectionType: string;
  connectionInfo: string;
}
