from typing import Any, Dict


def validate_policy(policy: Dict[str, Any]) -> None:
    if "version" not in policy:
        raise ValueError("Policy must have a 'version' field")

    if "rules" not in policy or not isinstance(policy["rules"], list):
        raise ValueError("Policy must have a 'rules' list")

    for i, rule in enumerate(policy["rules"]):
        validate_rule(rule, i)


def validate_rule(rule: Dict[str, Any], index: int) -> None:
    if "id" not in rule:
        raise ValueError(f"Rule {index} must have an 'id' field")

    if "when" not in rule:
        raise ValueError(f"Rule {rule.get('id', index)} must have a 'when' field")

    if "action" not in rule:
        raise ValueError(f"Rule {rule.get('id', index)} must have an 'action' field")

    if rule["action"] not in ["block", "warn", "require_approval"]:
        raise ValueError(
            f"Rule {rule.get('id', index)} has invalid action: {rule['action']}"
        )

    if "message" not in rule:
        raise ValueError(f"Rule {rule.get('id', index)} must have a 'message' field")
