import { requestClient } from '#/api/request';

export type DatabaseConnectionType =
  | 'mysql'
  | 'oracle'
  | 'postgresql'
  | 'sqlserver';

export interface DatabaseConnection {
  id: string;
  code: string;
  name: string;
  db_type: DatabaseConnectionType;
  host: string;
  port: number;
  user: string;
  default_database: string;
  description: string;
  status: boolean;
  is_system: boolean;
  has_password: boolean;
  application_id?: string;
  application_name?: string;
  extra_options?: Record<string, unknown>;
  sort?: number;
  sys_create_datetime?: string;
  sys_update_datetime?: string;
}

export interface DatabaseConnectionInput {
  code: string;
  name: string;
  dbType: DatabaseConnectionType;
  host: string;
  port: number;
  user?: string;
  password?: string;
  defaultDatabase?: string;
  description?: string;
  status?: boolean;
  applicationId?: string;
}

export interface DatabaseConnectionUpdateInput {
  name?: string;
  dbType?: DatabaseConnectionType;
  host?: string;
  port?: number;
  user?: string;
  password?: string;
  defaultDatabase?: string;
  description?: string;
  status?: boolean;
}

export interface DatabaseConnectionTestInput {
  dbType: DatabaseConnectionType;
  host: string;
  port: number;
  user?: string;
  password?: string;
  defaultDatabase?: string;
}

export interface DatabaseConnectionTestResult {
  success: boolean;
  message: string;
  db_name?: string;
  db_type?: string;
}

export interface DatabaseConnectionSimple {
  code: string;
  name: string;
  db_type: string;
  host: string;
  port: number;
  database: string;
  is_system: boolean;
}

export async function getDatabaseConnectionListApi(params?: {
  page?: number;
  pageSize?: number;
  applicationId?: string;
  name?: string;
  code?: string;
  status?: boolean;
}) {
  return requestClient.get<{ items: DatabaseConnection[]; total: number }>(
    '/api/core/database-connection',
    { params },
  );
}

export async function getAllDatabaseConnectionApi(applicationId?: string) {
  return requestClient.get<DatabaseConnectionSimple[]>(
    '/api/core/database-connection/get/all',
    { params: { applicationId } },
  );
}

export async function getDatabaseConnectionDetailApi(id: string) {
  return requestClient.get<DatabaseConnection>(
    `/api/core/database-connection/${id}`,
  );
}

export async function createDatabaseConnectionApi(data: DatabaseConnectionInput) {
  return requestClient.post<DatabaseConnection>(
    '/api/core/database-connection',
    {
      code: data.code,
      name: data.name,
      db_type: data.dbType,
      host: data.host,
      port: data.port,
      user: data.user ?? '',
      password: data.password ?? '',
      default_database: data.defaultDatabase ?? '',
      description: data.description ?? '',
      status: data.status ?? true,
      application_id: data.applicationId,
    },
  );
}

export async function updateDatabaseConnectionApi(
  id: string,
  data: DatabaseConnectionUpdateInput,
) {
  return requestClient.put<DatabaseConnection>(
    `/api/core/database-connection/${id}`,
    {
      name: data.name,
      db_type: data.dbType,
      host: data.host,
      port: data.port,
      user: data.user,
      password: data.password,
      default_database: data.defaultDatabase,
      description: data.description,
      status: data.status,
    },
  );
}

export async function deleteDatabaseConnectionApi(id: string) {
  return requestClient.delete(`/api/core/database-connection/${id}`);
}

export async function testDatabaseConnectionApi(data: DatabaseConnectionTestInput) {
  return requestClient.post<DatabaseConnectionTestResult>(
    '/api/core/database-connection/test',
    {
      db_type: data.dbType,
      host: data.host,
      port: data.port,
      user: data.user ?? '',
      password: data.password ?? '',
      default_database: data.defaultDatabase ?? '',
    },
  );
}

export async function testSavedDatabaseConnectionApi(id: string) {
  return requestClient.post<DatabaseConnectionTestResult>(
    `/api/core/database-connection/${id}/test`,
  );
}

export async function checkDatabaseConnectionCodeApi(code: string) {
  return requestClient.get<{ available: boolean; message?: string }>(
    `/api/core/database-connection/check-code/${code}`,
  );
}
