/**
 * 외부 호출 관련 타입 정의
 */

// 기본 외부 호출 설정
export interface ExternalCallConfig {
  callType: "rest-api" | "email" | "ftp" | "queue";
  apiType?: "slack" | "kakao-talk" | "discord" | "generic";

  // 공통 설정
  timeout?: number; // ms
  retryCount?: number;
  retryDelay?: number; // ms
}

// REST API 공통 설정
export interface RestApiConfig extends ExternalCallConfig {
  callType: "rest-api";
  url: string;
  method: "GET" | "POST" | "PUT" | "DELETE";
  headers?: Record<string, string>;
  body?: string;
}

// 슬랙 웹훅 설정
export interface SlackSettings extends ExternalCallConfig {
  callType: "rest-api";
  apiType: "slack";
  webhookUrl: string;
  channel?: string;
  message: string;
  username?: string;
  iconEmoji?: string;
}

// 카카오톡 API 설정
export interface KakaoTalkSettings extends ExternalCallConfig {
  callType: "rest-api";
  apiType: "kakao-talk";
  accessToken: string;
  message: string;
  templateId?: string;
  phoneNumber?: string;
}

// 디스코드 웹훅 설정
export interface DiscordSettings extends ExternalCallConfig {
  callType: "rest-api";
  apiType: "discord";
  webhookUrl: string;
  message: string;
  username?: string;
  avatarUrl?: string;
}

// 인증 설정 타입
export interface AuthenticationSettings {
  type: "none" | "api-key" | "basic" | "bearer" | "custom";
  apiKey?: string;
  username?: string;
  password?: string;
  token?: string;
  headerName?: string;
  headerValue?: string;
}

// 일반 REST API 설정
export interface GenericApiSettings extends ExternalCallConfig {
  callType: "rest-api";
  apiType: "generic";
  url: string;
  method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "HEAD";
  headers?: Record<string, string>;
  body?: string;
  authentication?: AuthenticationSettings;
}

// 이메일 설정
export interface EmailSettings extends ExternalCallConfig {
  callType: "email";
  smtpHost: string;
  smtpPort: number;
  smtpUser: string;
  smtpPass: string;
  fromEmail: string;
  toEmail: string;
  subject: string;
  body: string;
}

// 외부 호출 실행 결과
export interface ExternalCallResult {
  success: boolean;
  statusCode?: number;
  response?: string;
  error?: string;
  executionTime: number; // ms
  timestamp: Date;
}

// 외부 호출 실행 요청
export interface ExternalCallRequest {
  diagramId: number;
  relationshipId: string;
  settings: ExternalCallConfig;
  templateData?: Record<string, unknown>; // 템플릿 변수 데이터
}

// 템플릿 처리 옵션
export interface TemplateOptions {
  startDelimiter?: string; // 기본값: "{{"
  endDelimiter?: string; // 기본값: "}}"
  escapeHtml?: boolean; // 기본값: false
}

// 외부 호출 로그 (향후 구현)
export interface ExternalCallLog {
  id: number;
  diagramId: number;
  relationshipId: string;
  callType: string;
  apiType?: string;
  targetUrl: string;
  requestPayload?: string;
  responseStatus?: number;
  responseBody?: string;
  errorMessage?: string;
  executionTimeMs: number;
  createdAt: Date;
}

// 지원되는 외부 호출 타입들의 Union 타입
export type SupportedExternalCallSettings =
  | SlackSettings
  | KakaoTalkSettings
  | DiscordSettings
  | GenericApiSettings
  | EmailSettings;
