from __future__ import annotations

import json

from src.services.ai_technology_report_cadence_readback import (
    load_latest_ai_technology_report_cadence_readback,
)


def test_ai_technology_report_cadence_readback_committed_snapshot_is_safe():
    payload = load_latest_ai_technology_report_cadence_readback()

    assert payload["schema_version"] == "ai_technology_report_cadence_readback_v1"
    assert payload["summary"]["overall_completion_percent"] == 42.2
    assert payload["summary"]["report_cadence_completion_percent"] == 100.0
    assert payload["summary"]["report_cadence_count"] == 3
    assert payload["summary"]["report_ready_count"] == 3
    assert payload["summary"]["chart_section_count"] == 6
    assert payload["summary"]["agent_status_report_count"] == 5
    assert payload["summary"]["post_report_analysis_packet_count"] == 3
    assert payload["summary"]["low_medium_auto_action_proposal_count"] == 6
    assert payload["summary"]["high_risk_owner_review_count"] == 0
    assert payload["summary"]["technology_count"] == 20
    assert payload["summary"]["source_count"] == 47
    assert payload["summary"]["source_failures"] == 0
    assert payload["summary"]["telegram_send_enabled"] is False
    assert payload["summary"]["live_delivery_count_24h"] == 0
    assert payload["summary"]["report_receipt_write_count_24h"] == 0
    assert payload["summary"]["auto_optimization_write_count"] == 0

    policy = payload["policy"]
    assert policy["read_only"] is True
    assert policy["raw_chat_history_synced"] is False
    assert policy["raw_report_payload_display_allowed"] is False
    assert policy["report_delivery_enabled"] is False
    assert policy["telegram_send_enabled"] is False
    assert policy["bot_api_call_enabled"] is False
    assert policy["report_receipt_write_enabled"] is False
    assert policy["ai_post_report_analysis_live_run_enabled"] is False
    assert policy["low_medium_runtime_auto_write_enabled"] is False
    assert policy["high_risk_owner_review_required"] is False
    assert policy["low_medium_high_controlled_auto_route_enabled"] is True
    assert policy["sdk_installation_approved"] is False
    assert policy["paid_api_calls_approved"] is False
    assert policy["production_routing_approved"] is False
    assert policy["model_provider_switch_approved"] is False
    assert policy["host_write_approved"] is False
    assert policy["openclaw_replacement_approved"] is False

    serialized = json.dumps(payload, ensure_ascii=False)
    assert "/Users/" not in serialized
    assert ".claude/projects" not in serialized
    assert ".codex" not in serialized
    assert "192.168." not in serialized
    assert "auth.json" not in serialized
    assert "批准！繼續" not in serialized


def test_ai_technology_report_cadence_readback_covers_reports_agents_and_risk():
    payload = load_latest_ai_technology_report_cadence_readback()

    cadences = {row["cadence"]: row for row in payload["report_cadences"]}
    assert set(cadences) == {"daily", "weekly", "monthly"}
    assert cadences["daily"]["owner_agent"] == "Hermes"
    assert cadences["weekly"]["reviewer_agent"] == "NemoTron"
    assert cadences["monthly"]["owner_agent"] == "OpenClaw"
    assert all(row["telegram_delivery_status"].startswith("no_send") for row in cadences.values())

    workloads = {row["agent"]: row for row in payload["agent_workload_reports"]}
    assert workloads["MarketRadar"]["work_unit_count"] == 47
    assert workloads["NemoTron"]["work_unit_count"] == 14
    assert workloads["OpenClaw"]["work_unit_count"] == 9
    assert "production baseline" in workloads["OpenClaw"]["latest_output"]

    packets = {row["report_id"]: row for row in payload["post_report_analysis_packets"]}
    assert packets["daily"]["risk_tier"] == "low"
    assert packets["weekly"]["risk_tier"] == "medium"
    assert packets["monthly"]["risk_tier"] == "high"
    assert packets["monthly"]["agent_decision"] == "controlled_auto_route"

    risks = {row["risk_tier"]: row for row in payload["risk_automation_policy"]}
    assert set(risks) == {"low", "medium", "high"}
    assert "不得 live send" in risks["low"]["blocked_without_approval"]
    assert "verifier" in risks["medium"]["blocked_without_approval"]
    assert "controlled apply queue" in risks["high"]["reporting_mode"]
