export const sourceTypeOptions = [
  { label: 'Excel 文件', value: 'EXCEL' },
  { label: 'MySQL', value: 'MYSQL' },
  { label: 'PostgreSQL', value: 'POSTGRESQL' },
  { label: 'Oracle', value: 'ORACLE' },
  { label: 'GaussDB', value: 'GAUSSDB_NATIVE' },
  { label: 'GBase 8a', value: 'GBASE_8A' },
  { label: 'GBase 8s', value: 'GBASE_8S' },
];

export const sourceConnectionDefaults: Record<
  string,
  {
    buildJdbcUrl: (form: Record<string, any>) => string;
    defaultDriver: string;
    defaultPort: number;
  }
> = {
  MYSQL: {
    defaultPort: 3306,
    defaultDriver: 'com.mysql.cj.jdbc.Driver',
    buildJdbcUrl: (p) =>
      p.host && p.port && p.databaseName
        ? `jdbc:mysql://${p.host}:${p.port}/${p.databaseName}?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai&useSSL=false`
        : '',
  },
  POSTGRESQL: {
    defaultPort: 5432,
    defaultDriver: 'org.postgresql.Driver',
    buildJdbcUrl: (p) =>
      p.host && p.port && p.databaseName
        ? `jdbc:postgresql://${p.host}:${p.port}/${p.databaseName}`
        : '',
  },
  ORACLE: {
    defaultPort: 1521,
    defaultDriver: 'oracle.jdbc.OracleDriver',
    buildJdbcUrl: (p) => {
      const serviceName = p?.configJson?.serviceName || p.databaseName;
      return p.host && p.port && serviceName
        ? `jdbc:oracle:thin:@//${p.host}:${p.port}/${serviceName}`
        : '';
    },
  },
  GAUSSDB_NATIVE: {
    defaultPort: 5432,
    defaultDriver: 'org.postgresql.Driver',
    buildJdbcUrl: (p) =>
      p.host && p.port && p.databaseName
        ? `jdbc:postgresql://${p.host}:${p.port}/${p.databaseName}`
        : '',
  },
  GBASE_8A: {
    defaultPort: 5258,
    defaultDriver: 'com.gbase.jdbc.Driver',
    buildJdbcUrl: (p) =>
      p.host && p.port && p.databaseName
        ? `jdbc:gbase://${p.host}:${p.port}/${p.databaseName}`
        : '',
  },
  GBASE_8S: {
    defaultPort: 9088,
    defaultDriver: 'com.gbasedbt.jdbc.Driver',
    buildJdbcUrl: (p) => {
      const informixServer = p?.configJson?.informixServer || 'gbasedbt_server';
      return p.host && p.port && p.databaseName
        ? `jdbc:gbasedbt-sqli://${p.host}:${p.port}/${p.databaseName}:INFORMIXSERVER=${informixServer}`
        : '';
    },
  },
};

export const sourceTypeLabels: Record<string, string> = {
  PROJECT_MYSQL: '项目 MySQL',
  EXCEL: 'Excel 文件',
  MYSQL: 'MySQL',
  POSTGRESQL: 'PostgreSQL',
  ORACLE: 'Oracle',
  GAUSSDB_NATIVE: 'GaussDB',
  GBASE_8A: 'GBase 8a',
  GBASE_8S: 'GBase 8s',
};

export const accessModeLabels: Record<string, string> = {
  READ_ONLY: '只读',
  READ_WRITE: '可编辑',
};

export const tableKindLabels: Record<string, string> = {
  SOURCE_TABLE: '表',
  SOURCE_VIEW: '视图',
  TABLE: '表',
  VIEW: '视图',
  EXCEL_SHEET: '工作表',
  DERIVED_TABLE: '生成表',
  MATERIALIZED_TABLE: '缓存表',
};

export const jobTypeLabels: Record<string, string> = {
  IMPORT: '导入',
  SPLIT: '拆分',
  MERGE: '合并',
  DERIVE: '生成新表',
  EXPORT: '导出',
};

export const jobStatusLabels: Record<string, string> = {
  SUCCESS: '完成',
  FAILED: '失败',
  RUNNING: '处理中',
  PENDING: '等待中',
  CREATED: '已创建',
};

export const relationTypeLabels: Record<string, string> = {
  SPLIT: '拆分生成',
  MERGE: '合并生成',
  DERIVE: '生成新表',
};

export function formatSourceType(value?: string) {
  return value ? sourceTypeLabels[value] || value : '-';
}

export function formatAccessMode(value?: string) {
  return value ? accessModeLabels[value] || value : '-';
}

export function formatTestStatus(value?: string) {
  if (!value) return '未测试';
  return value === 'SUCCESS' ? '连接正常' : '连接失败';
}

export function formatTableKind(value?: string) {
  return value ? tableKindLabels[value] || value : '-';
}

export function formatJobType(value?: string) {
  return value ? jobTypeLabels[value] || value : value || '-';
}

export function formatJobStatus(value?: string) {
  return value ? jobStatusLabels[value] || value : '-';
}

export function formatRelationType(value?: string) {
  return value ? relationTypeLabels[value] || value : '关联';
}

export function formatFieldFlag(value?: null | number) {
  return value === null || value === undefined || value === 1 ? '是' : '否';
}

export function mergeConfigJsonBySourceType(
  sourceType: string,
  configJson?: Record<string, any>,
) {
  const merged = { ...configJson };
  if (sourceType === 'GBASE_8S' && !merged.informixServer) {
    merged.informixServer = 'gbasedbt_server';
  }
  return merged;
}
