import os
import sys
import time
from pathlib import Path

PROJECT_ROOT = Path(__file__).resolve().parent

# Make sure core module is importable
sys.path.append(str(PROJECT_ROOT))
from app.services.parser.hybrid_parser import HybridParser

def main():
    target_pdf = PROJECT_ROOT / "../Literature" / "Proses Pembuatan Pupuk Kompos dan Penggunaan Pupuk Kompos  di TPS 3R.pdf"
    output_dir = PROJECT_ROOT / "Markdown" / "chunking-integration" / "TestOutput"
    output_dir.mkdir(parents=True, exist_ok=True)
    
    output_file = output_dir / "Reviving_output.md"
    
    print(f"=== Memulai Deep Testing Hybrid Parser ===")
    print(f"Target PDF: {target_pdf}")
    print(f"Output File: {output_file}")
    
    if not target_pdf.exists():
        print(f"ERROR: File PDF tidak ditemukan: {target_pdf}")
        sys.exit(1)
        
    parser = HybridParser()
    
    print("\n[+] Memulai proses parsing...")
    start_time = time.time()
    
    try:
        markdown_result = parser.parse_pdf(str(target_pdf))
        end_time = time.time()
        
        char_count = len(markdown_result)
        
        print(f"\n[+] Parsing Selesai dalam {end_time - start_time:.2f} detik")
        print(f"[+] Total Karakter Ekstrak: {char_count}")
        
        # Simpan hasil
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write(markdown_result)
            
        print(f"[+] Hasil telah disimpan ke: {output_file}")
        print(f"\n=== Kesimpulan: ✅ DEEP TESTING BERHASIL ===")
        
    except Exception as e:
        import traceback
        print(f"\n[-] ERROR: Proses parsing gagal: {type(e).__name__}: {e}")
        print(traceback.format_exc())
        print(f"\n=== Kesimpulan: ❌ DEEP TESTING GAGAL ===")
        sys.exit(1)

if __name__ == "__main__":
    main()
