/**
 * CL-7: 재인덱싱 API 기본 동작 테스트
 * reindex/route.ts의 핵심 로직 검증
 */

import { describe, it, expect } from 'vitest';

// ── 재인덱싱 요청 body 검증 로직 ──────────────────────────────────
function validateReindexRequest(body: any): { valid: boolean; error?: string } {
    if (!body || typeof body !== 'object') {
        return { valid: false, error: 'Invalid request body' };
    }
    if (!body.productId || typeof body.productId !== 'string' || body.productId.trim().length === 0) {
        return { valid: false, error: 'productId required' };
    }
    return { valid: true };
}

// ── 재인덱싱 Job 생성 데이터 구성 로직 ──────────────────────────
function buildReindexJobData(meta: {
    driveFileId: string;
    companyId: string;
    companyName: string;
    productId: string;
    productName: string;
    category: string;
    effectiveDateRange?: { start: string; end?: string };
}): Record<string, any> {
    return {
        type: 'index_pdf',
        status: 'pending',
        driveFileId: meta.driveFileId,
        companyId: meta.companyId,
        companyName: meta.companyName,
        productId: meta.productId,
        productName: meta.productName,
        category: meta.category,
        effectiveDate: meta.effectiveDateRange?.start || '',
        isReindex: true,
        blueGreenMode: true,
        targetCollection: 'insurance_chunks_staging',
    };
}

describe('CL-7: 재인덱싱 API 기본 동작', () => {

    describe('요청 body 검증', () => {
        it('유효한 productId → valid', () => {
            const result = validateReindexRequest({ productId: '삼성life_퍼펙트종신_202403' });
            expect(result.valid).toBe(true);
        });

        it('productId 없음 → invalid', () => {
            const result = validateReindexRequest({});
            expect(result.valid).toBe(false);
            expect(result.error).toBe('productId required');
        });

        it('productId 빈 문자열 → invalid', () => {
            const result = validateReindexRequest({ productId: '' });
            expect(result.valid).toBe(false);
        });

        it('productId 공백만 → invalid', () => {
            const result = validateReindexRequest({ productId: '   ' });
            expect(result.valid).toBe(false);
        });

        it('null body → invalid', () => {
            const result = validateReindexRequest(null);
            expect(result.valid).toBe(false);
        });
    });

    describe('Job 데이터 구성', () => {
        it('메타데이터로 올바른 Job 데이터 생성', () => {
            const meta = {
                driveFileId: 'file123',
                companyId: '삼성life',
                companyName: '삼성생명',
                productId: '삼성life_퍼펙트종신_202403',
                productName: '퍼펙트종신보험',
                category: 'life',
                effectiveDateRange: { start: '2024-03' },
            };

            const jobData = buildReindexJobData(meta);

            expect(jobData.type).toBe('index_pdf');
            expect(jobData.status).toBe('pending');
            expect(jobData.isReindex).toBe(true);
            expect(jobData.blueGreenMode).toBe(true);
            expect(jobData.targetCollection).toBe('insurance_chunks_staging');
            expect(jobData.effectiveDate).toBe('2024-03');
            expect(jobData.productId).toBe('삼성life_퍼펙트종신_202403');
        });

        it('effectiveDateRange 없으면 effectiveDate는 빈 문자열', () => {
            const meta = {
                driveFileId: 'file456',
                companyId: 'kb손해',
                companyName: 'KB손해보험',
                productId: 'kb_암보험_202406',
                productName: 'KB암보험',
                category: 'non_life',
            };

            const jobData = buildReindexJobData(meta);

            expect(jobData.effectiveDate).toBe('');
        });

        it('Blue-Green 모드가 항상 true', () => {
            const meta = {
                driveFileId: 'file789',
                companyId: 'test',
                companyName: 'Test',
                productId: 'test_product',
                productName: 'Test Product',
                category: 'variable',
            };

            const jobData = buildReindexJobData(meta);

            expect(jobData.blueGreenMode).toBe(true);
            expect(jobData.targetCollection).toBe('insurance_chunks_staging');
        });
    });
});
