from __future__ import annotations

from types import SimpleNamespace

import oasyce.services.portfolio_engine as portfolio_engine
from oasyce.services.asset_lifecycle import ASSET_STATE_CANDIDATE, ASSET_STATE_DRAFT
from oasyce.services.portfolio_engine import (
    PORTFOLIO_ASSET_CLASS_OPEN_SOURCE,
    PORTFOLIO_ASSET_CLASS_RESTRICTED,
    evaluate_candidate_policy,
    resolve_register_policy,
)


def test_resolve_register_policy_defaults_public_open_source_to_free(monkeypatch, tmp_path):
    file_path = tmp_path / "tool.py"
    file_path.write_text("print('hello')\n", encoding="utf-8")
    monkeypatch.setattr(
        portfolio_engine,
        "resolve_source_asset_pricing",
        lambda path, requested_price_model="auto": SimpleNamespace(
            resolved_price_model="free",
            reason="public_git_open_source_defaults_free",
            metadata={
                "source_origin": "public_git_open_source",
                "source_repo_url": "https://github.com/acme/demo",
                "source_license": "mit",
            },
        ),
    )

    decision = resolve_register_policy(str(file_path), requested_price_model="auto")

    assert decision.resolved_price_model == "free"
    assert decision.asset_class == PORTFOLIO_ASSET_CLASS_OPEN_SOURCE
    assert decision.publish_action == "new_asset"
    assert decision.reason == "public_git_open_source_defaults_free"
    assert decision.metadata["portfolio_policy_reason"] == "public_git_open_source_defaults_free"


def test_evaluate_candidate_policy_drafts_sensitive_files(tmp_path):
    file_path = tmp_path / "secret.csv"
    file_path.write_text("ssn\n", encoding="utf-8")

    decision = evaluate_candidate_policy(
        file_path=str(file_path),
        sensitivity="sensitive",
        confidence=0.82,
    )

    assert decision.lifecycle_state == ASSET_STATE_DRAFT
    assert decision.asset_class == PORTFOLIO_ASSET_CLASS_RESTRICTED
    assert decision.publish_action == "manual_review"
    assert decision.reason == "sensitive_requires_review"
    assert decision.metadata["portfolio_policy_reason"] == "sensitive_requires_review"


def test_evaluate_candidate_policy_inherits_register_pricing(monkeypatch, tmp_path):
    file_path = tmp_path / "repo.py"
    file_path.write_text("print('repo')\n", encoding="utf-8")
    monkeypatch.setattr(
        portfolio_engine,
        "resolve_register_policy",
        lambda path, requested_price_model="auto": SimpleNamespace(
            resolved_price_model="free",
            asset_class=PORTFOLIO_ASSET_CLASS_OPEN_SOURCE,
            publish_action="new_asset",
            reason="public_git_open_source_defaults_free",
            metadata={
                "asset_class": PORTFOLIO_ASSET_CLASS_OPEN_SOURCE,
                "recommended_price_model": "free",
                "portfolio_policy_reason": "public_git_open_source_defaults_free",
            },
        ),
    )

    decision = evaluate_candidate_policy(
        file_path=str(file_path),
        sensitivity="public",
        confidence=0.97,
    )

    assert decision.lifecycle_state == ASSET_STATE_CANDIDATE
    assert decision.recommended_price_model == "free"
    assert decision.asset_class == PORTFOLIO_ASSET_CLASS_OPEN_SOURCE
    assert decision.metadata["publish_action"] == "candidate"
