#!/usr/bin/env python3
"""Check job edit state and placements for debugging render order issues."""
import json
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "backend"))
from dotenv import load_dotenv
load_dotenv(os.path.join(os.path.dirname(__file__), "..", "backend", ".env"))

from pymongo import MongoClient
from app.core.config import settings

db = MongoClient(settings.mongodb_uri)[settings.mongodb_db]

job_ids = [
    "69a0675634e072d880ef7205",  # de-v1fv
    "69a00adb3c66801f7d23fe5b",  # de-v2fv
]

for job_id in job_ids:
    job = db.jobs.find_one({"_id": job_id}, {"title": 1, "outputs": 1, "status": 1})
    if not job:
        print(f"Job {job_id} not found")
        continue

    print("=" * 70)
    print(f"Job: {job_id}")
    print(f"Title: {job.get('title')}")
    print(f"Status: {job.get('status')}")

    outputs = job.get("outputs", {})
    for lang, data in outputs.items():
        print(f"\nLanguage: {lang}")
        print(f"Keys: {list(data.keys())}")
        # Print full outputs structure (truncate large values)
        for key, val in data.items():
            if isinstance(val, dict):
                print(f"\n  {key}:")
                for k2, v2 in val.items():
                    if isinstance(v2, list) and len(v2) > 0:
                        print(f"    {k2}: [{len(v2)} items]")
                        for item in v2:
                            if isinstance(item, dict):
                                summary = {k: (str(v)[:60] if isinstance(v, str) else v) for k, v in item.items()}
                                print(f"      {summary}")
                    else:
                        print(f"    {k2}: {str(v2)[:100]}")
            elif isinstance(val, str) and len(val) > 100:
                print(f"  {key}: {val[:100]}...")
            else:
                print(f"  {key}: {val}")

    print()
