# src/agentsh_secure_sandbox/policies/presets.py
# mypy: disable-error-code="list-item"
"""Built-in policy presets for common use cases.

Mirrors the TypeScript @agentsh/secure-sandbox presets exactly.
"""

from __future__ import annotations

from .merge import merge
from .schema import PolicyDefinition


def agent_default(
    extensions: PolicyDefinition | None = None,
) -> PolicyDefinition:
    """Comprehensive policy for AI coding agents.

    This is the DEFAULT policy used when no policy is specified.
    """
    base = PolicyDefinition(
        file=[
            {"allow": "/workspace/**", "ops": ["read", "write", "create"]},
            # Git/version-control credentials
            {"deny": ["/workspace/.git/config", "/workspace/.netrc"]},
            # Secrets and credentials
            {"deny": ["**/.env", "**/.env.*", "**/credentials*", "**/*.pem", "**/*.key"]},
            {"deny": ["~/.ssh/**", "/proc/*/environ"]},
            # Cloud provider credentials
            {"deny": ["~/.aws/**", "~/.gcp/**", "~/.azure/**", "~/.config/gcloud/**"]},
            # Shell config injection (persistence)
            {"deny": ["~/.bashrc", "~/.zshrc", "~/.profile", "~/.bash_profile"]},
            # Credential stores
            {"deny": ["~/.gitconfig", "~/.netrc", "~/.curlrc", "~/.wgetrc"]},
            # PATH hijacking
            {"deny": "~/.local/bin/**"},
            # Agent config files — allow reads (project context), deny writes (prompt injection)
            {
                "deny": ["**/.cursorrules", "**/CLAUDE.md", "**/copilot-instructions.md"],
                "ops": ["write", "create", "delete"],
            },
        ],
        network=[
            {
                "allow": [
                    "registry.npmjs.org",
                    "registry.yarnpkg.com",
                    "pypi.org",
                    "files.pythonhosted.org",
                    "crates.io",
                    "static.crates.io",
                    "index.crates.io",
                    "proxy.golang.org",
                    "sum.golang.org",
                    "github.com",
                    "raw.githubusercontent.com",
                ],
                "ports": [443],
            },
            {"deny": "*"},
        ],
        commands=[
            # Allow safe commands (order matters — first match wins)
            {
                "allow": [
                    "bash",
                    "sh",
                    "echo",
                    "cat",
                    "head",
                    "tail",
                    "grep",
                    "find",
                    "ls",
                    "wc",
                    "sort",
                    "uniq",
                    "diff",
                    "pwd",
                    "date",
                    "which",
                    "whoami",
                    "id",
                    "uname",
                    "printf",
                    "test",
                    "true",
                    "false",
                    "mkdir",
                    "cp",
                    "mv",
                    "rm",
                    "touch",
                    "chmod",
                    "tr",
                    "cut",
                    "sed",
                    "awk",
                    "tee",
                    "xargs",
                    "basename",
                    "dirname",
                    "realpath",
                    "base64",
                    "md5sum",
                    "sha256sum",
                    "tar",
                    "gzip",
                    "gunzip",
                ],
            },
            # Allow dev tools
            {
                "allow": [
                    "git",
                    "node",
                    "npm",
                    "npx",
                    "yarn",
                    "pnpm",
                    "bun",
                    "python",
                    "python3",
                    "pip",
                    "pip3",
                    "cargo",
                    "rustc",
                    "go",
                    "make",
                    "cmake",
                ],
            },
            # Deny dangerous commands
            {"deny": ["env", "printenv", "sudo", "su", "doas"]},
            {"deny": ["shutdown", "reboot", "halt", "poweroff"]},
            {"deny": ["nc", "ncat", "netcat", "socat", "telnet"]},
            {"deny": ["git push --force", "git reset --hard"]},
            {
                "redirect": ["curl", "wget"],
                "to": {"cmd": "agentsh-fetch", "args": ["--audit"]},
            },
        ],
    )
    return merge(base, extensions) if extensions else base


def dev_safe(
    extensions: PolicyDefinition | None = None,
) -> PolicyDefinition:
    """Permissive defaults for local development. Not recommended for production."""
    base = PolicyDefinition(
        file=[
            {"allow": "/workspace/**", "ops": ["read", "write", "create"]},
            {"deny": ["**/.env", "**/.env.*", "**/credentials*", "**/*.pem", "**/*.key"]},
            {"deny": ["~/.ssh/**", "/proc/*/environ"]},
            {"deny": ["~/.aws/**", "~/.gcp/**", "~/.azure/**", "~/.config/gcloud/**"]},
            {"deny": ["~/.bashrc", "~/.zshrc", "~/.profile", "~/.bash_profile"]},
            {"deny": ["~/.gitconfig", "~/.netrc", "~/.curlrc", "~/.wgetrc"]},
        ],
        network=[
            {
                "allow": ["registry.npmjs.org", "registry.yarnpkg.com"],
                "ports": [443],
            },
        ],
        commands=[{"deny": ["env", "printenv", "shutdown", "reboot"]}],
    )
    return merge(base, extensions) if extensions else base


def ci_strict(
    extensions: PolicyDefinition | None = None,
) -> PolicyDefinition:
    """Locked down for CI/CD runners."""
    base = PolicyDefinition(
        file=[
            {"allow": "/workspace/**"},
            {"deny": ["**/.env", "**/.env.*", "**/credentials*", "**/*.pem", "**/*.key"]},
            {"deny": ["~/.aws/**", "~/.gcp/**", "~/.azure/**", "~/.config/gcloud/**"]},
            {"deny": "/**"},
        ],
        network=[
            {
                "allow": [
                    "registry.npmjs.org",
                    "registry.yarnpkg.com",
                    "pypi.org",
                    "files.pythonhosted.org",
                    "crates.io",
                    "static.crates.io",
                    "index.crates.io",
                    "proxy.golang.org",
                    "sum.golang.org",
                ],
                "ports": [443],
            },
            {"deny": "*"},
        ],
        commands=[{"deny": ["env", "printenv", "shutdown", "reboot", "sudo"]}],
    )
    return merge(base, extensions) if extensions else base


def agent_sandbox(
    extensions: PolicyDefinition | None = None,
) -> PolicyDefinition:
    """Maximum restriction for untrusted code. Read-only workspace, no network."""
    base = PolicyDefinition(
        file=[
            {"allow": "/workspace/**", "ops": ["read"]},
            {"deny": "/**"},
        ],
        network=[{"deny": "*"}],
        commands=[{"deny": ["env", "printenv", "sudo", "su", "shutdown", "reboot"]}],
    )
    return merge(base, extensions) if extensions else base
