from __future__ import annotations

from oasyce.services.portfolio_outcomes import build_portfolio_outcome_summary


def test_build_portfolio_outcome_summary_rolls_up_principal_delegate_and_assets():
    summary = build_portfolio_outcome_summary(
        runtime_identity={
            "principal": {"id": "principal-1", "carrier_type": "owner_account"},
            "account": {"id": "oasyce1owner"},
        },
        ledger_summary={
            "totals": {
                "spent_oas": 7.5,
                "earned_oas": 1.25,
                "net_oas": -6.25,
                "succeeded": 3,
                "failed": 1,
                "assets_created": 2,
                "purchases": 1,
            },
            "contributions": {
                "delegates": [
                    {
                        "principal_id": "principal-1",
                        "delegate_id": "device-a",
                        "executions": 3,
                        "succeeded": 2,
                        "failed": 1,
                        "spent_oas": 7.5,
                        "earned_oas": 1.25,
                        "net_oas": -6.25,
                        "assets_created": 1,
                        "purchases": 1,
                        "last_timestamp": 10,
                        "agent_counts": {"autonomy.scheduler": 2, "dashboard.operator": 1},
                    }
                ]
            },
        },
        lifecycle_summary={
            "counts": {
                "observed": 4,
                "candidate": 2,
                "draft": 1,
                "live": 3,
                "deprecated": 1,
                "archived": 0,
            },
            "recent": [
                {
                    "state": "candidate",
                    "state_key": "local:candidate",
                    "suggested_name": "Draft asset",
                },
                {
                    "state": "deprecated",
                    "state_key": "asset:deprecated",
                    "asset_id": "asset_old",
                    "suggested_name": "Legacy dataset",
                    "asset_class": "generic_asset",
                    "price_model": "auto",
                    "updated_at": 11,
                },
                {
                    "state": "live",
                    "state_key": "asset:live",
                    "asset_id": "asset_live",
                    "suggested_name": "Repo release",
                    "asset_class": "open_source_source",
                    "price_model": "free",
                    "portfolio_policy_reason": "public_git_open_source_defaults_free",
                    "updated_at": 12,
                    "executed_by": {"agent_id": "dashboard.operator"},
                },
            ],
        },
    )

    principal = summary["principal_result"]
    assert principal["principal_id"] == "principal-1"
    assert principal["account_id"] == "oasyce1owner"
    assert principal["net_oas"] == -6.25
    assert principal["live_assets"] == 3
    assert principal["deprecated_assets"] == 1

    asset_outcomes = summary["asset_outcomes"]
    assert asset_outcomes["created"] == 2
    assert asset_outcomes["failed_actions"] == 1

    delegate = summary["delegate_contributions"][0]
    assert delegate["delegate_id"] == "device-a"
    assert delegate["executions"] == 3
    assert delegate["top_agents"][0]["agent_id"] == "autonomy.scheduler"

    asset_feed = summary["asset_feed"]
    assert len(asset_feed) == 2
    assert asset_feed[0]["state"] == "deprecated"
    assert asset_feed[0]["asset_id"] == "asset_old"
    assert asset_feed[1]["state"] == "live"
    assert asset_feed[1]["price_model"] == "free"
