from __future__ import annotations

from oasyce.services.inbox import ConfirmationInbox
from oasyce.services.portfolio_ledger import get_portfolio_ledger_store
from oasyce.services.shared_desk import build_shared_desk_summary


def _actor(*, owner: str, device: str, agent: str, session: str):
    return {
        "principal": {
            "id": owner,
            "carrier_type": "owner_account",
            "carrier_id": owner,
            "delegate_type": "trusted_device",
            "delegate_id": device,
            "bridge_model": True,
        },
        "account": {
            "id": owner,
            "type": "owner_account",
            "mode": "attached_signing",
            "origin": "joined_existing",
        },
        "delegate": {
            "id": device,
            "type": "trusted_device",
            "authorization_status": "authorized",
            "can_sign": True,
        },
        "session": {
            "id": session,
            "agent_id": agent,
            "source": "test",
        },
        "owner_account": owner,
        "device_id": device,
        "agent_id": agent,
        "session_id": session,
    }


def test_shared_desk_merges_review_ready_budget_and_delegate_rows(tmp_path):
    owner = "oasyce1owner"
    actor_a = _actor(
        owner=owner,
        device="device-a",
        agent="researcher.agent",
        session="session-a",
    )
    actor_b = _actor(
        owner=owner,
        device="device-b",
        agent="publisher.agent",
        session="session-b",
    )
    runtime_identity = {
        "principal": actor_a["principal"],
        "account": actor_a["account"],
        "delegate": actor_a["delegate"],
        "session": actor_a["session"],
    }

    inbox = ConfirmationInbox(data_dir=str(tmp_path))
    purchase = inbox.add_pending_purchase(
        "cap_123",
        7.5,
        reason="Buy signal",
        coordination={"recommended_by": actor_a},
    )
    register = inbox.add_pending_register(
        "/tmp/repo/README.md",
        "Repo README",
        suggested_tags=["md"],
        sensitivity="public",
        confidence=0.92,
        coordination={"recommended_by": actor_b},
    )
    inbox.approve(register.item_id, actor=actor_b)

    ledger = get_portfolio_ledger_store(str(tmp_path))
    ledger.record(
        action="register",
        status="succeeded",
        item_id="ledger-1",
        account_id=owner,
        asset_id="asset_123",
        actor=actor_b,
        asset_class="open_source_source",
        price_model="free",
    )

    summary = build_shared_desk_summary(
        data_dir=str(tmp_path),
        inbox=inbox,
        runtime_identity=runtime_identity,
        mandate={
            "profile": "balanced",
            "objective": "grow a durable, versioned portfolio under safe defaults",
            "trade_mode": "budgeted",
            "review_mode": "sensitive_only",
            "trade_max_spend": 10.0,
        },
        ledger_summary=ledger.summary(),
    )

    assert summary["principal_id"] == owner
    assert summary["queues"]["review"]["total"] == 1
    assert summary["queues"]["execution_ready"]["total"] == 1
    assert summary["budget"]["open_purchase_exposure_oas"] == 7.5
    assert summary["budget"]["allocatable_budget_oas"] == 10.0
    assert summary["budget"]["budget_headroom_oas"] == 2.5
    assert summary["budget"]["budget_gap_oas"] == 0.0
    assert summary["recommendation"]["action"] == "review_candidates"

    desk_rows = {row["delegate_id"]: row for row in summary["delegates"]}
    assert desk_rows["device-a"]["pending_review"] == 1
    assert desk_rows["device-a"]["open_purchase_exposure_oas"] == 7.5
    assert desk_rows["device-a"]["allocated_budget_oas"] == 7.5
    assert desk_rows["device-a"]["recommendation"]["action"] == "review_candidates"
    assert desk_rows["device-b"]["execution_ready"] == 1
    assert desk_rows["device-b"]["executions"] == 1
    assert desk_rows["device-b"]["assets_created"] == 1
    assert desk_rows["device-b"]["recommendation"]["action"] == "run_ready_queue"
