/**
 * LanceDB Client - Embedded Vector Database
 * 
 * LanceDB is an embedded vector database that stores data locally on disk.
 * No external server is required - it runs entirely within the application.
 */

import * as lancedb from "@lancedb/lancedb";
import { existsSync, mkdirSync } from "fs";
import { join } from "path";
import { loadSettings } from "@/lib/settings/settings-manager";

let lanceDBInstance: lancedb.Connection | null = null;

/**
 * Get the path where LanceDB data will be stored
 */
export function getVectorDBPath(): string {
  // Use LOCAL_DATA_PATH if set (Electron mode), otherwise use .local-data
  const basePath = process.env.LOCAL_DATA_PATH || join(process.cwd(), ".local-data");
  const vectorDBPath = join(basePath, "vectordb");
  
  // Ensure directory exists
  if (!existsSync(vectorDBPath)) {
    mkdirSync(vectorDBPath, { recursive: true });
  }
  
  return vectorDBPath;
}

/**
 * Check if Vector DB (LanceDB) is enabled in settings
 */
export function isVectorDBEnabled(): boolean {
  const settings = loadSettings();
  return settings.vectorDBEnabled === true;
}

/**
 * Get or create the LanceDB connection instance
 * LanceDB is an embedded database - no external server needed
 */
export async function getLanceDB(): Promise<lancedb.Connection | null> {
  if (!isVectorDBEnabled()) {
    return null;
  }
  
  if (!lanceDBInstance) {
    const dbPath = getVectorDBPath();
    console.log("[VectorDB] Connecting to LanceDB at:", dbPath);
    lanceDBInstance = await lancedb.connect(dbPath);
  }
  
  return lanceDBInstance;
}

/**
 * Test the LanceDB connection
 */
export async function testVectorDBConnection(): Promise<{
  success: boolean;
  message: string;
  path?: string;
  tableCount?: number;
}> {
  try {
    const dbPath = getVectorDBPath();
    const db = await lancedb.connect(dbPath);
    const tables = await db.tableNames();
    
    return {
      success: true,
      message: `Connected successfully. ${tables.length} tables found.`,
      path: dbPath,
      tableCount: tables.length,
    };
  } catch (error) {
    return {
      success: false,
      message: error instanceof Error ? error.message : "Connection failed",
    };
  }
}

/**
 * Close the LanceDB connection (useful for cleanup)
 */
export function closeLanceDB(): void {
  lanceDBInstance = null;
}

