"""Typed CLI boundary for the disposable weekly-operations scenario."""

from __future__ import annotations

import argparse
import os
from collections.abc import Awaitable, Callable
from pathlib import Path

import anyio

ScenarioRunner = Callable[[Path, bool], Awaitable[None]]


class DisposableCliError(RuntimeError):
    """The disposable CLI preflight rejected unsafe inputs."""


class CliArguments(argparse.Namespace):
    network_disabled: bool = False
    scenario: str = ""
    verify_privacy: bool = False
    evidence_dir: Path | None = None


def main(repository: Path, scenario_runner: ScenarioRunner) -> int:
    """Parse the no-network contract and run one typed scenario."""
    parser = argparse.ArgumentParser(
        description="Run the no-network NutriCoach v1.4 weekly-operations Telegram E2E."
    )
    _ = parser.add_argument("--network-disabled", action="store_true", required=True)
    _ = parser.add_argument(
        "--scenario", choices=("weekly-operations",), default="weekly-operations"
    )
    _ = parser.add_argument("--verify-privacy", action="store_true")
    _ = parser.add_argument("--evidence-dir", type=Path)
    args = parser.parse_args(namespace=CliArguments())
    credentials_present = any(
        os.environ.get(name) for name in ("TELEGRAM_BOT_TOKEN", "TELEGRAM_TOKEN")
    )
    if not args.network_disabled or credentials_present:
        raise DisposableCliError("network and Telegram credentials must be disabled")
    evidence = args.evidence_dir or (
        repository / ".omo/evidence/nutricoach-v140-weekly-operations/task-11"
    )
    anyio.run(scenario_runner, evidence, args.verify_privacy)
    print("DISPOSABLE_WEEKLY_OPERATIONS_PASS")
    return 0
