import time
import os
import hashlib
from app.services.parser.hybrid_parser import HybridParser

def get_md5_hash(filepath: str) -> str:
    hasher = hashlib.md5()
    with open(filepath, 'rb') as f:
        buf = f.read()
        hasher.update(buf)
    return hasher.hexdigest()

def run_test():
    parser = HybridParser()
    test_pdf = "../Literature/PaperQA Retrieval-Augmented Generative Agent for Scientific Research.pdf"
    
    print(f"Testing PDF: {test_pdf}")
    print(f"MD5 Hash: {get_md5_hash(test_pdf)}")
    
    start_time = time.time()
    result = parser.parse_pdf(test_pdf)
    end_time = time.time()
    
    duration = end_time - start_time
    print(f"Extraction completed in: {duration:.4f} seconds")
    print(f"Extracted Length: {len(result)} characters")
    
    output_file = "sample_parsing_output.md"
    with open(output_file, "w", encoding="utf-8") as f:
        f.write(result)
    print(f"Hasil ekstraksi Markdown telah disimpan ke: {output_file}")
    
    if duration < 5:
        print("PERFORMANCE TEST PASSED (< 5 seconds)")
    else:
        print("PERFORMANCE TEST FAILED (>= 5 seconds)")

if __name__ == "__main__":
    run_test()
