"""AI content hook boundary validation — Phase 11.10."""
from typing import Any

from app.validators.integrations import FORBIDDEN_OWNERSHIP_KEYS, validate_json_object

AI_FORBIDDEN_OWNERSHIP_KEYS = FORBIDDEN_OWNERSHIP_KEYS | frozenset(
    {
        "model_id",
        "model_weights",
        "inference_engine",
        "llm_provider",
        "openai_api_key",
    }
)


def _reject_forbidden(value: dict, field: str) -> None:
    from app.validators.foundation import ValidationError

    banned = AI_FORBIDDEN_OWNERSHIP_KEYS.intersection(value.keys())
    if banned:
        raise ValidationError(
            "کلیدهای مالکیت سرویس‌های دیگر در payload مجاز نیست",
            {"field": field, "forbidden_keys": sorted(banned)},
        )


def validate_json_object(value: Any, field: str) -> dict | None:
    if value is None:
        return None
    if not isinstance(value, dict):
        from app.validators.foundation import ValidationError

        raise ValidationError(f"{field} باید object باشد", {"field": field})
    _reject_forbidden(value, field)
    return value


def validate_metadata(value: Any) -> dict | None:
    return validate_json_object(value, "metadata_json")


def validate_config(value: Any) -> dict | None:
    return validate_json_object(value, "config_json")


def validate_payload(value: Any) -> dict | None:
    return validate_json_object(value, "payload_json")
