import { ConditionNode } from "@/lib/api/dataflow";

// 연결 정보 타입
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;
    note?: string; // 연결 설명
    settings?: Record<string, unknown>;
  };
}

// 연결 설정 타입
export interface ConnectionConfig {
  relationshipName: string;
  connectionType: "simple-key" | "data-save" | "external-call";
  fromColumnName: string;
  toColumnName: string;
  settings?: Record<string, unknown>;
}

// 단순 키값 연결 설정
export interface SimpleKeySettings {
  notes: string;
}

// 데이터 저장 설정
export interface DataSaveSettings {
  actions: Array<{
    id: string;
    name: string;
    actionType: "insert" | "update" | "delete" | "upsert";
    logicalOperator?: "AND" | "OR"; // 액션 간 논리 연산자 (첫 번째 액션 제외)
    conditions?: ConditionNode[];
    fieldMappings: Array<{
      sourceTable?: string;
      sourceField: string;
      targetTable?: string;
      targetField: string;
      defaultValue?: string;
      transformFunction?: string;
    }>;
    splitConfig?: {
      sourceField: string; // 분할할 소스 필드
      delimiter: string; // 구분자 (예: ",")
      targetField: string; // 분할된 값이 들어갈 필드
    };
  }>;
}

// 외부 호출 설정
export interface ExternalCallSettings {
  callType: "rest-api" | "email" | "ftp" | "queue";

  // REST API 세부 종류
  apiType?: "slack" | "kakao-talk" | "discord" | "generic";

  // 일반 REST API 설정
  apiUrl?: string;
  httpMethod?: "GET" | "POST" | "PUT" | "DELETE";
  headers?: string;
  bodyTemplate?: string;

  // 슬랙 전용 설정
  slackWebhookUrl?: string;
  slackChannel?: string;
  slackMessage?: string;

  // 카카오톡 전용 설정
  kakaoAccessToken?: string;
  kakaoMessage?: string;

  // 디스코드 전용 설정
  discordWebhookUrl?: string;
  discordMessage?: string;
  discordUsername?: string;
}

// 단순화된 외부 호출 설정 (새로운 버전)
export interface SimpleExternalCallSettings {
  configId?: number; // 선택된 외부 호출 설정 ID
  configName?: string; // 설정 이름 (표시용)
  message: string; // 메시지 템플릿
}

// ConnectionSetupModal Props 타입
export interface ConnectionSetupModalProps {
  isOpen: boolean;
  connection: ConnectionInfo | null;
  companyCode: string;
  onConfirm: (relationship: any) => void;
  onCancel: () => void;
}
