#!/usr/bin/env python3
"""Review 1.0 safe defaults in mugi_config.toml and app preferences.

Exits 0 when all shipped high-risk features default to safe/off/bounded states.
"""
from __future__ import annotations

import os
import sys

_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


def _load_toml(path: str) -> dict:
    try:
        import tomllib
    except ImportError:
        import tomli as tomllib  # type: ignore
    with open(path, "rb") as f:
        return tomllib.load(f)


def main() -> int:
    config_path = os.path.join(_REPO, "mugi_config.toml")
    if not os.path.isfile(config_path):
        config_path = os.path.join(_REPO, "mugi_config.toml.example")
    if not os.path.isfile(config_path):
        print(f"ERROR: missing mugi_config.toml or mugi_config.toml.example", file=sys.stderr)
        return 2
    cfg = _load_toml(config_path)
    failures: list[str] = []

    mesh = cfg.get("mesh") or {}
    if mesh.get("enabled", False):
        failures.append("[mesh].enabled should default false for 1.0")

    kanban = cfg.get("kanban") or {}
    if kanban.get("mesh_eligible_agent_creation", False):
        failures.append("[kanban].mesh_eligible_agent_creation should default false")
    if kanban.get("surface_mesh_inbound", False):
        failures.append("[kanban].surface_mesh_inbound should default false")

    drives = cfg.get("drives") or {}
    if str(drives.get("autonomy_mode", "bounded")).strip().lower() == "aggressive":
        failures.append("[drives].autonomy_mode should not default to aggressive")

    web_act = cfg.get("web_act") or {}
    if web_act.get("enabled", False):
        failures.append("[web_act].enabled should default false")

    council = cfg.get("council") or {}
    # Council trigger is prefs-side; config section may only hold caps.
    if council.get("enabled", False):
        failures.append("[council].enabled should default false if present in toml")

    if failures:
        print("SAFE DEFAULTS: FAIL")
        for f in failures:
            print(f"  - {f}")
        return 1

    print("SAFE DEFAULTS: PASS")
    return 0


if __name__ == "__main__":
    raise SystemExit(main())
