// 외부 DB 연결 관련 타입 정의
// 작성일: 2024-12-17

export interface ExternalDbConnection {
  id?: number;
  connection_name: string;
  description?: string | null;
  db_type: "mysql" | "postgresql" | "oracle" | "mssql" | "sqlite" | "mariadb";
  host: string;
  port: number;
  database_name: string;
  username: string;
  password: string;
  connection_timeout?: number | null;
  query_timeout?: number | null;
  max_connections?: number | null;
  ssl_enabled?: string | null;
  ssl_cert_path?: string;
  connection_options?: Record<string, unknown>;
  company_code: string;
  is_active: string;
  created_date?: Date;
  created_by?: string;
  updated_date?: Date;
  updated_by?: string;
}

export interface ExternalDbConnectionFilter {
  db_type?: string;
  is_active?: string;
  company_code?: string;
  search?: string;
}

export interface TableColumn {
  column_name: string;
  data_type: string;
  is_nullable: string;
  column_default: string | null;
}

export interface TableInfo {
  table_name: string;
  columns: TableColumn[];
  description: string | null;
}

export interface ApiResponse<T> {
  success: boolean;
  data?: T;
  message?: string;
  error?: string;
}

// DB 타입 옵션
export const DB_TYPE_OPTIONS = [
  { value: "mysql", label: "MySQL" },
  { value: "postgresql", label: "PostgreSQL" },
  { value: "oracle", label: "Oracle" },
  { value: "mssql", label: "SQL Server" },
  { value: "mariadb", label: "MariaDB" },
  { value: "sqlite", label: "SQLite" },
];

// DB 타입별 기본 설정
export const DB_TYPE_DEFAULTS = {
  mysql: { port: 3306, driver: "mysql2" },
  postgresql: { port: 5432, driver: "pg" },
  oracle: { port: 1521, driver: "oracledb" },
  mssql: { port: 1433, driver: "mssql" },
  mariadb: { port: 3306, driver: "mysql2" },
  sqlite: { port: 0, driver: "sqlite3" },
};

// 활성 상태 옵션
export const ACTIVE_STATUS_OPTIONS = [
  { value: "Y", label: "활성" },
  { value: "N", label: "비활성" },
  { value: "", label: "전체" },
];

// 연결 테스트 관련 타입
export interface ConnectionTestRequest {
  db_type: string;
  host: string;
  port: number;
  database_name: string;
  username: string;
  password: string;
  connection_timeout?: number;
  ssl_enabled?: string;
}

export interface ConnectionTestResult {
  success: boolean;
  message: string;
  details?: {
    response_time?: number;
    server_version?: string;
    database_size?: string;
  };
  error?: {
    code?: string;
    details?: string;
  };
}

// 연결 옵션 스키마 (각 DB 타입별 추가 옵션)
export interface MySQLConnectionOptions {
  charset?: string;
  timezone?: string;
  connectTimeout?: number;
  acquireTimeout?: number;
  multipleStatements?: boolean;
}

export interface PostgreSQLConnectionOptions {
  schema?: string;
  ssl?: boolean | object;
  application_name?: string;
  statement_timeout?: number;
}

export interface OracleConnectionOptions {
  serviceName?: string;
  sid?: string;
  connectString?: string;
  poolMin?: number;
  poolMax?: number;
}

export interface SQLServerConnectionOptions {
  encrypt?: boolean;
  trustServerCertificate?: boolean;
  requestTimeout?: number;
  connectionTimeout?: number;
}

export interface SQLiteConnectionOptions {
  mode?: string;
  cache?: string;
  foreign_keys?: boolean;
}

export type SupportedConnectionOptions =
  | MySQLConnectionOptions
  | PostgreSQLConnectionOptions
  | OracleConnectionOptions
  | SQLServerConnectionOptions
  | SQLiteConnectionOptions;
