from __future__ import annotations

from oasyce.services.review_engine import build_operating_review_summary


def test_review_engine_prioritizes_exceptions_and_lifecycle_cleanup():
    summary = build_operating_review_summary(
        runtime_identity={
            "principal": {
                "id": "oasyce1owner",
                "carrier_type": "owner_account",
            },
            "account": {
                "id": "oasyce1owner",
            },
        },
        mandate={
            "profile": "balanced",
        },
        portfolio_outcomes={
            "principal_result": {
                "net_oas": -7.5,
                "live_assets": 3,
                "deprecated_assets": 1,
                "archived_assets": 0,
            },
            "asset_feed": [
                {
                    "state": "deprecated",
                    "asset_id": "asset_old",
                    "suggested_name": "Legacy Dataset",
                    "asset_class": "generic_asset",
                    "price_model": "auto",
                },
                {
                    "state": "live",
                    "asset_id": "asset_src",
                    "suggested_name": "Repo Source",
                    "asset_class": "open_source_source",
                    "price_model": "free",
                },
            ],
        },
        exceptions={
            "counts": {
                "critical": 1,
                "warning": 0,
                "info": 0,
            },
            "items": [
                {
                    "kind": "failed_actions",
                    "severity": "critical",
                    "recommendation": {
                        "action": "inspect_recent_operations",
                        "command": "oas agent status",
                        "target_section": "operations",
                        "priority": 100,
                    },
                }
            ],
        },
        shared_desk={
            "queues": {
                "review": {"total": 1},
                "execution_ready": {"total": 1},
            },
            "budget": {
                "budget_gap_oas": 0.0,
            },
            "delegates": [
                {
                    "delegate_id": "device-main",
                    "failed": 1,
                    "failed_execution": 1,
                    "pending_review": 0,
                    "execution_ready": 0,
                    "assets_created": 1,
                    "net_oas": -7.5,
                    "allocated_budget_oas": 0.0,
                    "budget_gap_oas": 0.0,
                }
            ],
        },
    )

    assert summary["summary"]["status"] == "critical"
    assert summary["top_recommendation"]["action"] == "inspect_recent_operations"
    assert summary["asset_reviews"][0]["recommendation"]["action"] == "archive_asset"
    assert summary["delegate_reviews"][0]["recommendation"]["action"] == "tighten_delegate_review"
