import type {
  LiveDatabaseIntrospectionOptions,
  LiveDatabaseIntrospectionPort,
} from '../../context/ingest/adapters/live-database/types.js';
import type { KtxProjectConnectionConfig } from '../../context/project/config.js';
import {
  KtxClickHouseScanConnector,
  type KtxClickHouseClientFactory,
  type KtxClickHouseConnectionConfig,
  type KtxClickHouseEndpointResolver,
} from './connector.js';

interface CreateClickHouseLiveDatabaseIntrospectionOptions {
  connections: Record<string, KtxProjectConnectionConfig>;
  clientFactory?: KtxClickHouseClientFactory;
  endpointResolver?: KtxClickHouseEndpointResolver;
  now?: () => Date;
}

export function createClickHouseLiveDatabaseIntrospection(
  options: CreateClickHouseLiveDatabaseIntrospectionOptions,
): LiveDatabaseIntrospectionPort {
  return {
    async extractSchema(connectionId: string, introspectionOptions?: LiveDatabaseIntrospectionOptions) {
      const connection = options.connections[connectionId] as KtxClickHouseConnectionConfig | undefined;
      const connector = new KtxClickHouseScanConnector({
        connectionId,
        connection,
        clientFactory: options.clientFactory,
        endpointResolver: options.endpointResolver,
        now: options.now,
      });
      try {
        return await connector.introspect(
          {
            connectionId,
            driver: 'clickhouse',
            ...(introspectionOptions?.tableScope ? { tableScope: introspectionOptions.tableScope } : {}),
          },
          { runId: `clickhouse-${connectionId}` },
        );
      } finally {
        await connector.cleanup();
      }
    },
  };
}
