/**
 * 백링크 초기 집계 배치 스크립트
 * 
 * 기존 문서들의 incomingLinkCount를 한번에 계산하여 업데이트합니다.
 * Cloud Functions 트리거 배포 전 기존 데이터에 대해 1회 실행합니다.
 * 
 * 사용법: npx ts-node scripts/aggregate-backlinks.ts
 * 
 * 생성일: 2026-02-10 20:47
 */

import * as admin from 'firebase-admin';
import * as path from 'path';

// Firebase Admin 초기화 (서비스 계정 키 사용)
const serviceAccountPath = path.resolve(__dirname, '../serviceAccountKey.json');

try {
    const serviceAccount = require(serviceAccountPath);
    admin.initializeApp({
        credential: admin.credential.cert(serviceAccount)
    });
} catch {
    // 서비스 계정 키가 없으면 기본 인증 사용 (gcloud auth)
    admin.initializeApp();
    console.log('⚠️ serviceAccountKey.json 없음 — 기본 인증 사용');
}

const db = admin.firestore();

interface DocumentData {
    title?: string;
    outgoingLinkIds?: string[];
    outgoingLinks?: string[];
    docType?: string;
    incomingLinkCount?: number;
}

async function aggregateBacklinks() {
    console.log('📊 백링크 집계 시작...\n');

    // 1. 모든 문서 조회
    const snapshot = await db.collection('documents').get();
    const docs: { id: string; data: DocumentData }[] = [];

    snapshot.forEach(doc => {
        docs.push({ id: doc.id, data: doc.data() as DocumentData });
    });

    console.log(`📄 총 ${docs.length}개 문서 조회됨`);

    // 2. ID→제목 매핑 테이블 구축
    const titleToId: Record<string, string> = {};
    docs.forEach(doc => {
        if (doc.data.title) {
            titleToId[doc.data.title] = doc.id;
        }
    });

    // 3. incomingLinkCount 집계
    const linkCountMap: Record<string, number> = {};

    docs.forEach(doc => {
        // outgoingLinkIds (ID 기반 링크)
        doc.data.outgoingLinkIds?.forEach(linkId => {
            if (linkId !== doc.id) { // 자기 자신 제외
                linkCountMap[linkId] = (linkCountMap[linkId] || 0) + 1;
            }
        });

        // outgoingLinks (제목 기반 링크) → ID로 변환
        doc.data.outgoingLinks?.forEach(linkTitle => {
            const targetId = titleToId[linkTitle];
            if (targetId && targetId !== doc.id) {
                linkCountMap[targetId] = (linkCountMap[targetId] || 0) + 1;
            }
        });
    });

    // 4. 결과 출력
    const sortedEntries = Object.entries(linkCountMap)
        .sort(([, a], [, b]) => b - a);

    console.log('\n🔗 집계 결과:');
    console.log('─'.repeat(50));
    sortedEntries.forEach(([docId, count]) => {
        const docData = docs.find(d => d.id === docId);
        console.log(`  ${count}건 ← ${docData?.data.title || docId}`);
    });
    console.log('─'.repeat(50));

    // 5. Firestore 업데이트 (배치)
    const BATCH_SIZE = 500; // Firestore 배치 제한
    let batchCount = 0;
    let updateCount = 0;

    // 모든 문서에 대해 incomingLinkCount 설정 (0인 것도 명시적으로)
    for (let i = 0; i < docs.length; i += BATCH_SIZE) {
        const batch = db.batch();
        const chunk = docs.slice(i, i + BATCH_SIZE);

        chunk.forEach(doc => {
            const count = linkCountMap[doc.id] || 0;
            if (doc.data.incomingLinkCount !== count) {
                batch.update(
                    db.collection('documents').doc(doc.id),
                    { incomingLinkCount: count }
                );
                updateCount++;
            }
        });

        await batch.commit();
        batchCount++;
    }

    console.log(`\n✅ 완료: ${updateCount}개 문서 업데이트 (${batchCount}개 배치)`);
}

// 실행
aggregateBacklinks()
    .then(() => process.exit(0))
    .catch(err => {
        console.error('❌ 집계 실패:', err);
        process.exit(1);
    });
