"""Generate PDF for performance checks"""
import asyncio
import sqlite3
import os
import sys

sys.path.insert(0, r'D:\Workspace\claudeCode\AutonetSellCar.com\backend')
os.chdir(r'D:\Workspace\claudeCode\AutonetSellCar.com\backend')

from app.services.pdf_service import capture_performance_check_pdf

async def generate_pdfs():
    conn = sqlite3.connect('autonet.db')
    cursor = conn.cursor()

    # Get records without PDF
    cursor.execute("""
        SELECT car_id, check_number FROM car_performance_checks
        WHERE check_number IS NOT NULL AND check_number != ''
        AND (pdf_path IS NULL OR pdf_path = '')
    """)
    records = cursor.fetchall()

    print(f"Found {len(records)} records to generate PDF")

    for car_id, check_number in records:
        print(f"\nGenerating PDF for Car ID: {car_id}, Check#: {check_number}")

        pdf_path = await capture_performance_check_pdf(check_number, car_id)

        if pdf_path:
            print(f"  SUCCESS: {pdf_path}")
            # Update database
            cursor.execute("UPDATE car_performance_checks SET pdf_path = ? WHERE car_id = ?", (pdf_path, car_id))
            conn.commit()
        else:
            print(f"  FAILED")

    conn.close()
    print("\nDone!")

if __name__ == "__main__":
    asyncio.run(generate_pdfs())
