/**
 * Sleep consolidation — DB 접근
 */
import { DatabaseUtils } from '../../../shared/utils/database.js';
export class ConsolidationRepository {
    db;
    constructor(db) {
        this.db = db;
    }
    getLookbackDays() {
        const raw = process.env.CONSOLIDATION_LOOKBACK_DAYS;
        const n = raw ? parseInt(raw, 10) : 90;
        return Number.isFinite(n) && n > 0 ? n : 90;
    }
    /**
     * 클러스터링 대상 에피소딕 (미통합, 비핀, lookback 이내)
     */
    findEpisodicCandidates(ownerIdFilter, lookbackDays) {
        const days = lookbackDays ?? this.getLookbackDays();
        const rows = DatabaseUtils.all(this.db, `
      SELECT
        id,
        content,
        importance,
        owner_id AS ownerId,
        created_at AS createdAt,
        pinned AS pinned,
        COALESCE(is_consolidated, 0) AS isConsolidated
      FROM memory_item
      WHERE type = 'episodic'
        AND COALESCE(is_consolidated, 0) = 0
        AND COALESCE(pinned, 0) = 0
        AND (is_deleted IS NULL OR is_deleted = 0)
        AND datetime(created_at) >= datetime('now', '-' || ? || ' days')
        AND (? IS NULL OR owner_id = ?)
      ORDER BY created_at DESC
    `, [days, ownerIdFilter ?? null, ownerIdFilter ?? null]);
        return rows.map(r => ({
            id: r.id,
            content: r.content,
            importance: r.importance,
            ownerId: r.ownerId,
            createdAt: r.createdAt,
            pinned: Boolean(r.pinned),
            isConsolidated: Boolean(r.isConsolidated)
        }));
    }
    /**
     * owner별 시맨틱 후보 (재요약 병합용)
     */
    findSemanticsByOwner(ownerId) {
        const rows = DatabaseUtils.all(this.db, `
      SELECT id, content, COALESCE(origin_source, '{}') AS originSource, owner_id AS ownerId
      FROM memory_item
      WHERE type = 'semantic'
        AND (is_deleted IS NULL OR is_deleted = 0)
        AND COALESCE(owner_id, '') = COALESCE(?, '')
      ORDER BY created_at ASC
    `, [ownerId ?? null]);
        return rows;
    }
    updateSemanticMemory(params) {
        DatabaseUtils.run(this.db, `
      UPDATE memory_item
      SET content = ?, origin_source = ?
      WHERE id = ? AND type = 'semantic'
    `, [params.content, params.originSourceJson, params.id]);
    }
    /**
     * memory_id → 파싱된 임베딩 벡터 (첫 행만, 임의 provider 우선 최신)
     */
    loadEmbeddingsMap(memoryIds) {
        const out = new Map();
        if (memoryIds.length === 0) {
            return out;
        }
        const placeholders = memoryIds.map(() => '?').join(',');
        const rows = DatabaseUtils.all(this.db, `
      SELECT memory_id, embedding
      FROM memory_embedding
      WHERE memory_id IN (${placeholders})
      ORDER BY memory_id, created_at DESC
    `, memoryIds);
        for (const row of rows) {
            if (out.has(row.memory_id)) {
                continue;
            }
            try {
                const vec = JSON.parse(row.embedding);
                if (Array.isArray(vec) && vec.length > 0) {
                    out.set(row.memory_id, vec);
                }
            }
            catch {
                /* skip */
            }
        }
        return out;
    }
    insertSemanticMemory(params) {
        DatabaseUtils.run(this.db, `
      INSERT INTO memory_item (
        id, type, content, importance, privacy_scope, origin_source,
        owner_id, created_at, is_consolidated) VALUES (?, 'semantic', ?, ?, ?, ?, ?, datetime('now'), 0)
    `, [
            params.id,
            params.content,
            params.importance,
            params.privacyScope ?? 'private',
            params.originSourceJson,
            params.ownerId
        ]);
    }
    markEpisodicsConsolidated(memoryIds, importanceCap = 0.1) {
        if (memoryIds.length === 0) {
            return;
        }
        const placeholders = memoryIds.map(() => '?').join(',');
        DatabaseUtils.run(this.db, `
      UPDATE memory_item
      SET is_consolidated = 1,
          importance = MIN(importance, ?)
      WHERE id IN (${placeholders})
        AND type = 'episodic'
    `, [importanceCap, ...memoryIds]);
    }
}
//# sourceMappingURL=consolidation-repository.js.map