/**
 * 벡터 성능 테스트 리포지토리 구현
 * 성능 테스트 데이터베이스 접근 로직 분리
 */
import Database from 'better-sqlite3';
import { VECTOR_SEARCH_CONFIG } from '../../../shared/config/vector-search.config.js';
import { PIIMasker } from '../../../shared/utils/pii-masker.js';
import { logger } from '../../../shared/utils/logger.js';
export class VectorPerformanceRepositoryImpl {
    db = null;
    isVecAvailable = false;
    constructor(db) {
        this.db = db;
        this.checkVecAvailability();
    }
    /**
     * VEC 사용 가능 여부 확인
     */
    checkVecAvailability() {
        if (!this.db) {
            this.isVecAvailable = false;
            return;
        }
        try {
            const tableStatement = this.db.prepare(`
        SELECT name FROM sqlite_master 
        WHERE type='table' AND name IN (
          'memory_item_vec',
          'memory_item_vec_tfidf',
          'memory_item_vec_minilm', 
          'memory_item_vec_openai',
          'memory_item_vec_gemini'
        )
      `);
            const tableRows = typeof tableStatement.all === 'function'
                ? tableStatement.all()
                : [];
            const tableCheck = Array.isArray(tableRows)
                ? tableRows
                : [];
            if (tableCheck.length === 0) {
                this.isVecAvailable = false;
                return;
            }
            // VEC 함수 사용 가능 여부 확인
            try {
                const testTableEntry = tableCheck.find((table) => typeof table === 'object' && table !== null && typeof table.name === 'string');
                const testTable = testTableEntry?.name ?? 'memory_item_vec_tfidf';
                const testStatement = this.db.prepare(`
          SELECT distance FROM ${testTable} 
          WHERE embedding MATCH ? 
          LIMIT 0
        `);
                if (typeof testStatement.get !== 'function') {
                    this.isVecAvailable = false;
                    return;
                }
                testStatement.get(JSON.stringify(new Array(VECTOR_SEARCH_CONFIG.defaultDimensions).fill(0)));
                this.isVecAvailable = true;
            }
            catch (vecError) {
                this.isVecAvailable = false;
            }
        }
        catch (error) {
            this.isVecAvailable = false;
        }
    }
    /**
     * 성능 테스트 실행
     */
    async runPerformanceTest(queryVector, iterations) {
        if (!this.db || !this.isVecAvailable) {
            return { averageTime: 0, minTime: 0, maxTime: 0, results: 0, successRate: 0 };
        }
        const times = [];
        let resultCount = 0;
        let successCount = 0;
        for (let i = 0; i < iterations; i++) {
            try {
                const startTime = Date.now();
                const results = await this.executeSearch(queryVector);
                const endTime = Date.now();
                times.push(endTime - startTime);
                if (i === 0)
                    resultCount = results.length;
                successCount++;
            }
            catch (error) {
                const maskedError = error instanceof Error ? PIIMasker.maskError(error) : { message: String(error), name: 'Error' };
                logger.warn('성능 테스트 회차 실패', {
                    iteration: i + 1,
                    error: maskedError.message
                });
                times.push(0);
            }
        }
        const averageTime = times.reduce((a, b) => a + b, 0) / times.length;
        const minTime = Math.min(...times.filter(t => t > 0));
        const maxTime = Math.max(...times);
        const successRate = successCount / iterations;
        logger.info('벡터 검색 성능 테스트', {
            averageTime: averageTime.toFixed(2),
            iterations,
            successRate: (successRate * 100).toFixed(1)
        });
        return {
            averageTime,
            minTime: minTime || 0,
            maxTime,
            results: resultCount,
            successRate
        };
    }
    /**
     * 실제 검색 실행 (성능 테스트용)
     */
    async executeSearch(queryVector) {
        if (!this.db)
            return [];
        try {
            const tableName = VECTOR_SEARCH_CONFIG.tableNames.tfidf;
            const query = `
        SELECT 
          vec.rowid as memory_id,
          vec.distance as similarity
        FROM ${tableName} vec
        WHERE vec.embedding MATCH ?
        ORDER BY vec.distance ASC
        LIMIT 10
      `;
            const statement = this.db.prepare(query);
            if (typeof statement.all !== 'function') {
                logger.warn('성능 테스트 검색을 실행할 수 없습니다: all() 메서드가 없습니다');
                return [];
            }
            const rawResults = statement.all(JSON.stringify(queryVector));
            return Array.isArray(rawResults) ? rawResults : [];
        }
        catch (error) {
            throw new Error(`검색 실행 실패: ${error}`);
        }
    }
}
//# sourceMappingURL=vector-performance.repository.js.map