import json
from django.utils import timezone
from apps.editor.models import Project

p = Project.objects.get(pk=8)
src = p.ai_suggestions.filter(is_active=True).order_by("-updated_at", "-created_at").first()
if src is None:
    print("ERROR: no active AISuggestion")
else:
    backup_path = "/home/altoquet/maxeditor/backend/backups/aisuggestion_p8_id1_20260508-145705.json"
    with open(backup_path, "r", encoding="utf-8") as f:
        snap = json.load(f)
    n_before = len((src.structured_response or {}).get("alternatives") or [])
    src.structured_response = snap["structured_response"]
    src.raw_response = snap.get("raw_response", "")
    src.save(update_fields=["structured_response", "raw_response", "updated_at"])
    n_after = len((src.structured_response or {}).get("alternatives") or [])
    print(f"OK restore: alts {n_before} -> {n_after}")
