from __future__ import annotations

from oasyce.services.asset_lifecycle import (
    ASSET_STATE_ARCHIVED,
    ASSET_STATE_CANDIDATE,
    ASSET_STATE_DRAFT,
    ASSET_STATE_LIVE,
    ASSET_STATE_OBSERVED,
    AssetLifecycleStore,
)


def test_asset_lifecycle_local_state_flow(tmp_path):
    store = AssetLifecycleStore(data_dir=str(tmp_path))
    target = tmp_path / "repo" / "main.py"
    target.parent.mkdir(parents=True)
    target.write_text("print('hi')\n")

    observed = store.observe(
        file_path=str(target),
        source="scheduler",
        suggested_name="Main",
        tags=["py", "code"],
    )
    assert observed.state == ASSET_STATE_OBSERVED

    candidate = store.candidate(
        file_path=str(target),
        source="scheduler",
        suggested_name="Main",
        tags=["py", "code"],
        metadata={
            "asset_class": "open_source_source",
            "recommended_price_model": "free",
            "portfolio_policy_reason": "public_git_open_source_defaults_free",
            "discovered_by": {
                "owner_account": "oasyce1owner",
                "device_id": "device-a",
                "agent_id": "autonomy.scheduler",
                "session_id": "scan-1",
            },
            "recommended_by": {
                "owner_account": "oasyce1owner",
                "device_id": "device-a",
                "agent_id": "autonomy.scheduler",
                "session_id": "scan-1",
            },
        },
    )
    assert candidate.state == ASSET_STATE_CANDIDATE

    draft = store.draft(
        file_path=str(target),
        source="inbox",
        reason="needs_confirmation",
    )
    assert draft.state == ASSET_STATE_DRAFT

    summary = store.summary()
    assert summary["counts"][ASSET_STATE_DRAFT] == 1
    assert summary["pricing_counts"]["free"] == 1
    assert summary["asset_class_counts"]["open_source_source"] == 1
    assert summary["policy_counts"]["public_git_open_source_defaults_free"] == 1
    assert summary["coordination_counts"]["principals"]["oasyce1owner"] == 2
    assert summary["coordination_counts"]["devices"]["device-a"] == 2
    assert summary["coordination_counts"]["agents"]["autonomy.scheduler"] == 2
    assert summary["recent"][0]["file_path"] == str(target)
    assert summary["recent"][0]["discovered_by"]["account"]["id"] == "oasyce1owner"
    assert summary["recent"][0]["discovered_by"]["agent_id"] == "autonomy.scheduler"


def test_asset_lifecycle_live_and_archive_states(tmp_path):
    store = AssetLifecycleStore(data_dir=str(tmp_path))

    live = store.live(
        asset_id="OAS_123",
        file_path=str(tmp_path / "asset.py"),
        owner="alice",
        source="register",
        tags=["code"],
    )
    assert live.state == ASSET_STATE_LIVE

    archived = store.archived(asset_id="OAS_123", reason="manual_archive")
    assert archived.state == ASSET_STATE_ARCHIVED

    summary = store.summary()
    assert summary["counts"][ASSET_STATE_ARCHIVED] == 1
    assert summary["recent"][0]["asset_id"] == "OAS_123"
