#!/usr/bin/env python3
"""
Test Parser and Chunker - Pengujian Mandiri Ekstraksi & Pemotongan PDF
Tujuan: Membaca PDF asli, melakukan parsing ke Markdown, melakukan chunking, 
dan menghasilkan output serta metrik performa ke terminal.
"""

import sys
import os
import time
import json
from pathlib import Path

# SETUP ENVIRONMENT & PATH
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))

from app.services.parser.hybrid_parser import HybridParser
from app.services.parser.smart_chunker import generate_chunks

# KONFIGURASI PENGUJIAN
TARGET_PDF_FILENAME = "The Hybrid Multimodal Graph Index (HMGI): A ComprehensiveFramework for Integrated Relational and Vector Search.pdf"
PDF_PATH = PROJECT_ROOT / "Literature" / TARGET_PDF_FILENAME

OUTPUT_PARSING_DIR = PROJECT_ROOT / "archive" / "Testing" / "Parsing"
OUTPUT_CHUNKING_DIR = PROJECT_ROOT / "archive" / "Testing" / "Chunking"

OUTPUT_PARSING_DIR.mkdir(parents=True, exist_ok=True)
OUTPUT_CHUNKING_DIR.mkdir(parents=True, exist_ok=True)

OUTPUT_MARKDOWN = OUTPUT_PARSING_DIR / f"{TARGET_PDF_FILENAME.replace('.pdf', '')}_parsed.md"
OUTPUT_JSON_CHUNKS = OUTPUT_CHUNKING_DIR / f"{TARGET_PDF_FILENAME.replace('.pdf', '')}_chunks.json"


def main():
    print("-" * 80)
    print("PENGUJIAN PARSING & CHUNKING MANDIRI")
    print("-" * 80)
    print(f"Target File : {PDF_PATH}")
    
    if not PDF_PATH.exists():
        print(f"ERROR: File tidak ditemukan di {PDF_PATH}")
        sys.exit(1)

    # 1. PARSING PDF KE MARKDOWN
    print("\n[1] Memulai Proses Parsing (PDF -> Markdown)...")
    parser = HybridParser()
    
    start_parse = time.time()
    try:
        markdown_result = parser.parse_pdf(str(PDF_PATH))
    except Exception as e:
        print(f"ERROR saat Parsing: {e}")
        sys.exit(1)
    end_parse = time.time()
    parse_duration = end_parse - start_parse

    #Simpan hasil parsing ke file Markdown
    with open(OUTPUT_MARKDOWN, "w", encoding="utf-8") as f:
        f.write(markdown_result)

    # 2. CHUNKING (MARKDOWN -> JSON)
    print("\n[2] Memulai Proses Chunking (Markdown -> JSON)...")
    
    start_chunk = time.time()
    try:
        # Menggunakan ukuran chunk default dari generate_chunks
        # (chunk_size=1500, chunk_overlap=150) untuk pengujian ini
        chunks = generate_chunks(
            markdown_text=markdown_result,
            file_hash="dummy_hash_for_testing",
            title=TARGET_PDF_FILENAME
        )
    except Exception as e:
        print(f"ERROR saat Chunking: {e}")
        sys.exit(1)
    end_chunk = time.time()
    chunk_duration = end_chunk - start_chunk
    # Simpan hasil chunking ke file JSON
    with open(OUTPUT_JSON_CHUNKS, "w", encoding="utf-8") as f:
        json.dump(chunks, f, ensure_ascii=False, indent=2)

    # 3. REPORTING PERFORMA & METRIK
    print("\n" + "-" * 80)
    print("LAPORAN PERFORMA")
    print("-" * 80)
    print(f"Durasi Parsing    : {parse_duration:.2f} detik")
    print(f"Karakter Ekstrak  : {len(markdown_result):,} karakter")
    print("-" * 80)
    print(f"Durasi Chunking   : {chunk_duration:.2f} detik")
    print(f"Jumlah Chunk      : {len(chunks)} potong")
    print(f"Rata-rata Ukuran  : {len(markdown_result) // max(len(chunks), 1):,} karakter / chunk")
    print("-" * 80)
    print("LOKASI OUTPUT:")
    print(f"- Markdown : {OUTPUT_MARKDOWN}")
    print(f"- JSON     : {OUTPUT_JSON_CHUNKS}")
    print("-" * 80 + "\n")


if __name__ == "__main__":
    main()
