#!/usr/bin/env python3
"""
IwoooS SSH / Firewall / Network Access owner request draft 產生器。

本工具讀取 ssh-network-access repo-only 清冊，將 16 個 surface 轉成人工
送件前 request draft。它不 SSH、不 keyscan、不讀 live firewall、不 patch
known_hosts、不套用 NetworkPolicy、不改 NodePort、不啟動 WireGuard。
"""

from __future__ import annotations

import argparse
import json
import subprocess
import sys
from datetime import datetime, timedelta, timezone
from pathlib import Path
from typing import Any


TAIPEI = timezone(timedelta(hours=8))

REQUEST_FIELDS = [
    "request_id",
    "surface_id",
    "label",
    "expected_scope",
    "config_kind",
    "access_scope",
    "control_tier",
    "repo_source_path",
    "repo_sha256",
    "owner_role_or_team",
    "decision",
    "decision_reason",
    "affected_scope",
    "redacted_evidence_refs",
    "live_access_state_ref",
    "allowed_source_cidrs_ref",
    "maintenance_window",
    "rollback_owner",
    "validation_plan",
    "break_glass_owner",
    "change_freeze_rule",
    "followup_owner",
    "not_approval",
]

REQUIRED_OWNER_FIELDS = [
    "owner_role_or_team",
    "decision",
    "decision_reason",
    "affected_scope",
    "redacted_evidence_refs",
    "live_access_state_ref",
    "allowed_source_cidrs_ref",
    "maintenance_window",
    "rollback_owner",
    "validation_plan",
    "break_glass_owner",
    "change_freeze_rule",
    "followup_owner",
]

BLOCKED_ACTIONS = [
    "ssh_read",
    "ssh_write",
    "host_keyscan",
    "known_hosts_patch",
    "firewall_change",
    "port_close",
    "port_open",
    "network_policy_apply",
    "nodeport_change",
    "wireguard_change",
    "sudo_action",
    "deploy_ssh_action",
    "secret_value_collection",
    "ssh_key_collection",
    "active_scan",
    "runtime_gate_open",
]

WRITE_CAPABLE_KINDS = {
    "ci_deploy_ssh",
    "monitoring_ssh_deploy_script",
    "sudoers_policy",
    "alert_ssh_action_rules",
}


def git_short_sha(root: Path) -> str:
    try:
        result = subprocess.run(
            ["git", "rev-parse", "--short", "HEAD"],
            cwd=root,
            check=True,
            capture_output=True,
            text=True,
        )
        return result.stdout.strip()
    except Exception:
        return "unknown"


def load_json(path: Path) -> dict[str, Any]:
    return json.loads(path.read_text(encoding="utf-8"))


def request_for(surface: dict[str, Any]) -> dict[str, Any]:
    surface_id = surface["surface_id"]
    write_capable = surface["config_kind"] in WRITE_CAPABLE_KINDS
    return {
        "request_id": f"ssh_network_owner_request:{surface_id}",
        "status": "draft_not_dispatched",
        "surface_id": surface_id,
        "label": surface["label"],
        "expected_scope": surface["expected_scope"],
        "config_kind": surface["config_kind"],
        "access_scope": surface["access_scope"],
        "control_tier": surface["control_tier"],
        "repo_source_path": surface["source_path"],
        "repo_sha256": surface["sha256"],
        "source_line_count": surface["line_count"],
        "requires_live_evidence": surface["requires_live_evidence"],
        "write_capable_surface": write_capable,
        "source_inventory_ref": "docs/security/ssh-network-access-inventory.snapshot.json",
        "request_fields": REQUEST_FIELDS,
        "required_owner_fields": REQUIRED_OWNER_FIELDS,
        "blocked_actions": BLOCKED_ACTIONS,
        "owner_role_or_team": "pending_owner_role_or_team",
        "decision": "pending_owner_decision",
        "decision_reason": "pending_decision_reason",
        "affected_scope": "pending_affected_scope",
        "redacted_evidence_refs": [],
        "live_access_state_ref": None,
        "allowed_source_cidrs_ref": None,
        "maintenance_window": "pending_maintenance_window",
        "rollback_owner": "pending_rollback_owner",
        "validation_plan": "pending_validation_plan",
        "break_glass_owner": "pending_break_glass_owner",
        "change_freeze_rule": "pending_change_freeze_rule",
        "followup_owner": "pending_followup_owner",
        "not_approval": True,
        "request_sent": False,
        "recipient_confirmed": False,
        "owner_response_received": False,
        "owner_response_accepted": False,
        "live_evidence_received": False,
        "maintenance_window_accepted": False,
        "rollback_owner_accepted": False,
        "validation_plan_accepted": False,
        "host_write_authorized": False,
        "ssh_read_authorized": False,
        "ssh_write_authorized": False,
        "host_keyscan_authorized": False,
        "known_hosts_patch_authorized": False,
        "firewall_change_authorized": False,
        "port_change_authorized": False,
        "network_policy_apply_authorized": False,
        "nodeport_change_authorized": False,
        "wireguard_change_authorized": False,
        "sudo_action_authorized": False,
        "deploy_ssh_action_authorized": False,
        "secret_value_collection_allowed": False,
        "ssh_key_collection_allowed": False,
        "active_scan_authorized": False,
        "runtime_gate": False,
        "action_buttons_allowed": False,
    }


def build_report(root: Path, inventory: dict[str, Any], generated_at: str | None) -> dict[str, Any]:
    report_time = generated_at or datetime.now(TAIPEI).isoformat(timespec="seconds")
    requests = [request_for(surface) for surface in inventory["access_surfaces"]]
    write_capable_requests = [item for item in requests if item["write_capable_surface"]]
    live_evidence_requests = [item for item in requests if item["requires_live_evidence"]]

    return {
        "schema_version": "ssh_network_owner_request_draft_v1",
        "generated_at": report_time,
        "git_commit": git_short_sha(root),
        "source_inventory_schema_version": inventory.get("schema_version"),
        "source_inventory_status": inventory.get("status"),
        "status": "owner_request_draft_ready_not_dispatched",
        "summary": {
            "request_draft_count": len(requests),
            "write_capable_request_draft_count": len(write_capable_requests),
            "live_evidence_required_request_count": len(live_evidence_requests),
            "request_field_count": len(REQUEST_FIELDS),
            "required_owner_field_count": len(REQUIRED_OWNER_FIELDS),
            "blocked_action_count": len(BLOCKED_ACTIONS),
            "request_sent_count": 0,
            "recipient_confirmed_count": 0,
            "owner_response_received_count": 0,
            "owner_response_accepted_count": 0,
            "live_evidence_received_count": 0,
            "maintenance_window_accepted_count": 0,
            "rollback_owner_accepted_count": 0,
            "validation_plan_accepted_count": 0,
            "host_write_authorized_count": 0,
            "ssh_read_authorized_count": 0,
            "ssh_write_authorized_count": 0,
            "host_keyscan_authorized_count": 0,
            "known_hosts_patch_authorized_count": 0,
            "firewall_change_authorized_count": 0,
            "port_change_authorized_count": 0,
            "network_policy_apply_authorized_count": 0,
            "nodeport_change_authorized_count": 0,
            "wireguard_change_authorized_count": 0,
            "sudo_action_authorized_count": 0,
            "deploy_ssh_action_authorized_count": 0,
            "secret_value_collection_allowed_count": 0,
            "ssh_key_collection_allowed_count": 0,
            "active_scan_authorized_count": 0,
            "runtime_gate_count": 0,
            "action_button_count": 0,
        },
        "execution_boundaries": {
            "request_sent": False,
            "recipient_confirmed": False,
            "owner_response_received": False,
            "owner_response_accepted": False,
            "live_host_read_authorized": False,
            "live_evidence_received": False,
            "host_write_authorized": False,
            "ssh_read_authorized": False,
            "ssh_write_authorized": False,
            "host_keyscan_authorized": False,
            "known_hosts_patch_authorized": False,
            "firewall_change_authorized": False,
            "port_change_authorized": False,
            "network_policy_apply_authorized": False,
            "nodeport_change_authorized": False,
            "wireguard_change_authorized": False,
            "sudo_action_authorized": False,
            "deploy_ssh_action_authorized": False,
            "secret_value_collection_allowed": False,
            "ssh_key_collection_allowed": False,
            "active_scan_authorized": False,
            "runtime_execution_authorized": False,
            "action_buttons_allowed": False,
            "not_authorization": True,
        },
        "request_fields": REQUEST_FIELDS,
        "required_owner_fields": REQUIRED_OWNER_FIELDS,
        "blocked_actions": BLOCKED_ACTIONS,
        "request_drafts": requests,
        "next_steps": [
            "人工送件前確認 network / firewall / deploy owner role 與回覆窗口。",
            "owner 只能提供脫敏 live access state、allowed source CIDR metadata、maintenance window、rollback owner 與 validation plan。",
            "收到回覆後先做欄位完整性、敏感 payload 隔離、port close/open 影響範圍與 rollback gate 檢查，不得直接改 firewall 或套用 NetworkPolicy。",
        ],
    }


def main() -> int:
    parser = argparse.ArgumentParser(description="IwoooS SSH / network owner request draft 產生器")
    parser.add_argument("--root", default=".", help="repo root")
    parser.add_argument(
        "--inventory-report",
        default="docs/security/ssh-network-access-inventory.snapshot.json",
        help="ssh-network-access-inventory.py 輸出的 JSON",
    )
    parser.add_argument("--output", help="寫出 JSON 報告")
    parser.add_argument("--generated-at", help="固定報告時間，供 committed snapshot 使用")
    args = parser.parse_args()

    root = Path(args.root).resolve()
    inventory = load_json(root / args.inventory_report)
    report = build_report(root, inventory, args.generated_at)
    payload = json.dumps(report, ensure_ascii=False, indent=2, sort_keys=True)

    if args.output:
        output = Path(args.output)
        output.parent.mkdir(parents=True, exist_ok=True)
        output.write_text(payload + "\n", encoding="utf-8")
    else:
        print(payload)

    summary = report["summary"]
    print(
        "SSH_NETWORK_OWNER_REQUEST_DRAFT_OK "
        f"drafts={summary['request_draft_count']} "
        f"write_capable={summary['write_capable_request_draft_count']} "
        f"fields={summary['required_owner_field_count']} "
        f"sent={summary['request_sent_count']} "
        f"runtime_gate={summary['runtime_gate_count']}",
        file=sys.stderr,
    )
    return 0


if __name__ == "__main__":
    sys.exit(main())
