"""Inspect a small saved fal O3 image-reference request without submitting it."""

import json
import sys
from urllib.parse import urlparse


def inspect_request(value: object) -> dict:
    if not isinstance(value, dict):
        raise ValueError("Expected a JSON object")
    allowed = {"prompt", "image_urls", "duration", "aspect_ratio", "generate_audio"}
    unknown = set(value) - allowed
    if unknown:
        raise ValueError(f"Outside this limited exercise: {sorted(unknown)}")
    prompt = value.get("prompt")
    if not isinstance(prompt, str) or not prompt.strip():
        raise ValueError("A nonempty prompt is required")
    duration = value.get("duration")
    if duration not in {str(i) for i in range(3, 16)}:
        raise ValueError("Use a documented duration string from 3 through 15")
    if value.get("aspect_ratio") not in {"16:9", "9:16", "1:1"}:
        raise ValueError("Choose a documented aspect ratio")
    if not isinstance(value.get("generate_audio"), bool):
        raise ValueError("Choose audio explicitly")
    refs = value.get("image_urls")
    if not isinstance(refs, list) or not 1 <= len(refs) <= 2:
        raise ValueError("This exercise inspects one or two image references")
    for index, url in enumerate(refs, 1):
        if not isinstance(url, str):
            raise ValueError("Reference URLs must be strings")
        parsed = urlparse(url)
        if parsed.scheme != "https" or not parsed.hostname:
            raise ValueError("Use HTTPS reference URLs")
        if f"@Image{index}" not in prompt:
            raise ValueError(f"Name @Image{index} in the prompt")
    return {
        "local_subset_checks_pass": True,
        "reference_count": len(refs),
        "remote_files_checked": False,
        "provider_schema_fully_validated": False,
        "submitted": False,
        "quote_verified": False,
        "generation_id": None,
    }


if __name__ == "__main__":
    with open(sys.argv[1], encoding="utf-8") as source:
        print(json.dumps(inspect_request(json.load(source)), indent=2))
