import { createClient } from '@supabase/supabase-js';
import { SupabaseVectorStore } from '@langchain/community/vectorstores/supabase';
import { GigaChatEmbeddings } from 'langchain-gigachat';
import { Agent } from 'node:https';

const httpsAgent = new Agent({
    rejectUnauthorized: false,
});

let vectorStoreInstance: SupabaseVectorStore | null = null;

export function getVectorStore(): SupabaseVectorStore {
    if (!vectorStoreInstance) {
        const client = createClient(
            process.env.RAG_SUPABASE_URL!,
            process.env.RAG_SUPABASE_SERVICE_ROLE_KEY!,
        );

        vectorStoreInstance = new SupabaseVectorStore(
            new GigaChatEmbeddings({
                credentials: process.env.GIGA_AUTH,
                httpsAgent,
            }),
            {
                client,
                tableName: 'slon',
                queryName: 'match_slon'
            }
        );
    }

    return vectorStoreInstance;
} 