
import os
import base64
import fitz  # PyMuPDF
from openai import OpenAI

# Configuration
PDF_PATH = '/mnt/pdfs/İŞ MAKİNASI GRUBU/JCB/TELESKOPİK YÜKLEYİCİLER/330/330 SERVİS MANUELİ.pdf'
PAGE_NUM = 940 # The page we saw earlier with "Driveline" info
# Using the key found in usta_web.py for this test session
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY", "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")

def encode_image(image_bytes):
    return base64.b64encode(image_bytes).decode('utf-8')

def test_vision_prompt():
    if not os.path.exists(PDF_PATH):
        print(f"File not found: {PDF_PATH}")
        return

    # 1. Render Page as Image
    print(f"Rendering page {PAGE_NUM} from {os.path.basename(PDF_PATH)}...")
    doc = fitz.open(PDF_PATH)
    page = doc[PAGE_NUM-1] # 0-indexed
    pix = page.get_pixmap(matrix=fitz.Matrix(2, 2)) # 2x zoom for clarity
    img_data = pix.tobytes("png")
    doc.close()
    
    base64_img = encode_image(img_data)
    
    # 2. Define the "Master Prompt"
    system_prompt = """
    You are an expert Technical Documentation Parsers. Your goal is to extract structured information from images of technical service manuals.
    
    Identify and extract the following elements into structured Markdown:
    
    1. **Tables**: Identify any tabular data (specs, torques, parts lists). Convert them into Markdown tables.
    2. **Diagnostics**: If there are troubleshooting flowcharts or steps, transcribe them logically.
    3. **Technical Specs**: Extract specific values (pressure, dimensions, torque) as key-value pairs.
    4. **Visual Context**: Briefly describe any diagrams or photos (e.g., "Exploded view of hydraulic pump").
    
    OUTPUT FORMAT:
    # Summary
    [Brief description]
    
    # Tables
    [Markdown tables]
    
    # Technical Data
    - Key: Value
    
    # Visual Description
    [Description]
    """
    
    print("\nSending to AI (simulating Gemini via OpenAI compatible endpoint or just printing plan if no key)...")
    
    if not OPENAI_API_KEY:
        print("❌ SKIPPING AI CALL: OPENAI_API_KEY not set.")
        return

    client = OpenAI(api_key=OPENAI_API_KEY)
    
    try:
        response = client.chat.completions.create(
            model="gpt-4o-mini", # Fallback to mini
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": [
                    {"type": "text", "text": "Analyze this technical manual page."},
                    {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_img}"}}
                ]}
            ],
            max_tokens=2000
        )
        
        print("\n=== AI RESPONSE ===")
        print(response.choices[0].message.content)
        
    except Exception as e:
        print(f"Error: {e}")

if __name__ == "__main__":
    test_vision_prompt()
