import * as admin from 'firebase-admin';
import * as fs from 'fs';
import * as path from 'path';

// Initialize Firebase Admin
// 로컬 배포 환경에서 ADC 탐색 체인이 GCE 서버를 룩업하며 생기는 10초 타임아웃 방지
if (!admin.apps.length) {
    let credential: admin.credential.Credential | undefined;

    // 로컬 특정 경로에 키 파일이 있으면 직접 읽어서 주입 (실제 CF 환경에선 이 파일이 없어 무시됨)
    const localKeyPath = path.resolve(__dirname, '../../temp.j2h/insuwiki-j2h-902be7d0b6f5.json');
    if (fs.existsSync(localKeyPath)) {
        try { 
            credential = admin.credential.cert(JSON.parse(fs.readFileSync(localKeyPath, 'utf8'))); 
        } catch { /* 무시 */ }
    }

    admin.initializeApp(
        credential
            ? { credential, projectId: 'insuwiki-j2h' }
            : undefined   // Cloud Functions 런타임: 자동 감지
    );
}

// Export all functions
export * from './cleanupLocks';
export * from './aggregateBacklinks';
export * from './backup';
export * from './ragQuery';
export * from './pdfIndexing';            // Task B: PDF 인덱싱 파이프라인 (v2 §14-1)
export * from './crawlYoutubeChannels';   // Task I: 유튜브 자동 수집 (v2 §12)
export * from './generateYoutubeChannelInsight';
export * from './staticMatching';         // Task 820.1: AI 노드 자동 연결 - 정적 매칭
export * from './embeddingMatching';      // Task 829.1: AI 노드 자동 연결 - 임베딩 유사도 추천
export * from './setCustomClaims';        // Custom Claims 동기화 (users 문서 변경 트리거)
export * from './backfillClaims';         // 기존 유저 Custom Claims 백필
export * from './reviewOnCreate';          // 리뷰 생성 트리거 (상태 전이)
export * from './onDocumentUpdate';         // 문서 수정 감지 (needs_re_review 전환)
export * from './reliabilityBatchRecalc';  // Phase 3: 신뢰도 점수 일일 배치 재계산
export * from './backlogScheduler';        // Phase 3: 검토자 적체 모니터링 (6시간 주기)
export * from './versionRetentionScheduler'; // Phase 3: 버전 보존 정책 (일일 정리)
