"""Execution-contract fixtures for contract-boundary acceptance tests."""

from anima.core.contracts import (
    ContractIdentity,
    ContractResolverProvenance,
    ContractSourceHashes,
    PersonaProvenance,
    PersonaSpawnability,
    RuntimeModelContext,
    RuntimeOutputPolicy,
    RuntimeToolAccess,
    SessionTransportState,
    SpawnabilityReason,
)
from anima.core.execution import (
    ExecutionIntent,
    ExecutionIntentDefaults,
    ExecutionIntentOverrides,
    ExecutionSessionState,
    ResolvedExecutionContract,
    ResolvedLifecyclePolicy,
    ResolvedRuntime,
)
from anima.core.schemas.enums import CompletionPolicy, LifecycleType, SideEffectPolicy
from anima.core.schemas.models import AgentStateData, JobSpec, PersonaSpec


PERSONA_SPEC = PersonaSpec(
    id="worker",
    description="Worker persona",
    prompt="You are a worker.",
    model="gpt-5.2",
)

JOB_SPEC = JobSpec(
    persona_ref="worker@sha256:persona",
    instance_id="inst_worker",
    max_turns=8,
    max_tokens=1200,
)

EXECUTION_INTENT = ExecutionIntent(
    persona_spec=PERSONA_SPEC,
    job_spec=JOB_SPEC,
    overrides=ExecutionIntentOverrides(
        requested_work_dir="/workspace/task",
        requested_cli_tools=("read_file", "write_file"),
        requested_mcp_tools=("browser.snapshot",),
        requested_completion_policy=CompletionPolicy.explicit,
    ),
    defaults=ExecutionIntentDefaults(
        default_timeout="30m",
        default_project_root="/workspace",
        default_side_effect_policy=SideEffectPolicy.allow,
    ),
)

SPAWNABLE_SNAPSHOT_CONTRACT = ResolvedExecutionContract(
    identity=ContractIdentity(
        instance_id="inst_worker",
        resolver=ContractResolverProvenance(
            resolver_version="resolver/0.1.0",
            source_hashes=ContractSourceHashes(
                persona_digest="sha256:persona",
                job_digest="sha256:job",
                cli_digest="sha256:cli",
            ),
        ),
    ),
    runtime=ResolvedRuntime(
        effective_model="gpt-5.2",
        timeout="30m",
        max_turns=8,
        max_tokens=1200,
        work_dir="/workspace/task",
        project_root="/workspace",
        filesystem_scope="project-root",
        sandbox_posture="workspace-write",
    ),
    tool_access=RuntimeToolAccess(
        declared_persona_tools=("read_file",),
        requested_cli_tools=("read_file", "write_file"),
        requested_mcp_tools=("browser.snapshot",),
        effective_tools=("read_file", "write_file", "browser.snapshot"),
        effective_presets=("default",),
        effective_side_effect_policy=SideEffectPolicy.allow,
    ),
    persona_provenance=PersonaProvenance(
        source="snapshot",
        reusable_persona_ref="worker@sha256:persona",
        materialization_status="materialized",
        inline_behavior="auto_materialize_snapshot",
        child_inheritance_policy="inherit_materialized_snapshot",
        spawnability=PersonaSpawnability(can_spawn=True),
    ),
    lifecycle_policy=ResolvedLifecyclePolicy(
        lifecycle=LifecycleType.ephemeral,
        completion_policy=CompletionPolicy.explicit,
        attachable=True,
    ),
    output_policy=RuntimeOutputPolicy(
        provider_profile="openai-compatible",
        per_call_tool_output_chars=12000,
        per_turn_tool_output_chars=24000,
        truncation_strategy="paginate_then_truncate",
    ),
    model_context=RuntimeModelContext(
        values={"instance_id": "inst_worker", "attachable": True}
    ),
)

NON_SPAWNABLE_INLINE_CONTRACT = ResolvedExecutionContract(
    identity=ContractIdentity(
        instance_id="inst_inline",
        resolver=ContractResolverProvenance(
            resolver_version="resolver/0.1.0",
            source_hashes=ContractSourceHashes(cli_digest="sha256:inline"),
        ),
    ),
    runtime=ResolvedRuntime(effective_model="gpt-5.2"),
    tool_access=RuntimeToolAccess(
        effective_side_effect_policy=SideEffectPolicy.read_only,
    ),
    persona_provenance=PersonaProvenance(
        source="inline",
        reusable_persona_ref=None,
        materialization_status="not_materialized",
        inline_behavior="reject_spawn",
        child_inheritance_policy="reject_child_inheritance",
        spawnability=PersonaSpawnability(
            can_spawn=False,
            reason=SpawnabilityReason(
                code="inline_persona_requires_materialization",
                message="This inline persona cannot spawn child agents until it is materialized.",
                user_action="Retry with a reusable persona snapshot or explicit child persona.",
                retryable=True,
                details={"source": "inline"},
            ),
        ),
    ),
    lifecycle_policy=ResolvedLifecyclePolicy(
        lifecycle=LifecycleType.ephemeral,
        completion_policy=CompletionPolicy.explicit,
        attachable=True,
    ),
    output_policy=RuntimeOutputPolicy(
        provider_profile="openai-compatible",
        truncation_strategy="truncate",
    ),
)

EXECUTION_SESSION_STATE = ExecutionSessionState(
    instance_id="inst_worker",
    transport=SessionTransportState(
        attach_mode="streaming",
        stream_active=True,
        transport_connected=True,
        socket_path="/tmp/anima.sock",
        terminal_snapshot_ref="terminal-snapshot-1",
    ),
)

MUTABLE_AGENT_STATE = AgentStateData(
    instance_id="inst_worker",
    persona_name="worker",
    trace_id="trace_worker",
    persona_ref="worker@sha256:persona",
    lifecycle=LifecycleType.ephemeral,
)
