// 외부 REST API 연결 관리 타입 정의

export type AuthType =
  | "none"
  | "api-key"
  | "bearer"
  | "basic"
  | "oauth2"
  | "db-token";

export interface ExternalRestApiConnection {
  id?: number;
  connection_name: string;
  description?: string;
  base_url: string;
  endpoint_path?: string;
  default_headers: Record<string, string>;

  // 기본 메서드 및 바디 추가
  default_method?: string;
  default_body?: string;

  auth_type: AuthType;
  auth_config?: {
    // API Key
    keyLocation?: "header" | "query";
    keyName?: string;
    keyValue?: string;

    // Bearer Token
    token?: string;

    // Basic Auth
    username?: string;
    password?: string;

    // OAuth2
    clientId?: string;
    clientSecret?: string;
    tokenUrl?: string;
    accessToken?: string;

    // DB 기반 토큰 모드
    dbTableName?: string;
    dbValueColumn?: string;
    dbWhereColumn?: string;
    dbWhereValue?: string;
    dbHeaderName?: string;
    dbHeaderTemplate?: string;
  };
  timeout?: number;
  retry_count?: number;
  retry_delay?: number;
  company_code: string;
  is_active: string;
  
  // 위치 이력 저장 설정 (지도 위젯용)
  save_to_history?: string; // 'Y' 또는 'N' - REST API에서 가져온 위치 데이터를 vehicle_location_history에 저장
  created_date?: Date;
  created_by?: string;
  updated_date?: Date;
  updated_by?: string;
  last_test_date?: Date;
  last_test_result?: string;
  last_test_message?: string;
}

export interface ExternalRestApiConnectionFilter {
  auth_type?: string;
  is_active?: string;
  company_code?: string;
  search?: string;
}

export interface RestApiTestRequest {
  id?: number;
  base_url: string;
  endpoint?: string;
  method?: "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
  headers?: Record<string, string>;
  body?: any; // 테스트 요청 바디 추가
  auth_type?: AuthType;
  auth_config?: any;
  timeout?: number;
}

export interface RestApiTestResult {
  success: boolean;
  message: string;
  response_time?: number;
  status_code?: number;
  response_data?: any;
  error_details?: string;
}

export const AUTH_TYPE_OPTIONS = [
  { value: "none", label: "인증 없음" },
  { value: "api-key", label: "API Key" },
  { value: "bearer", label: "Bearer Token" },
  { value: "basic", label: "Basic Auth" },
  { value: "oauth2", label: "OAuth 2.0" },
  { value: "db-token", label: "DB 토큰" },
];
