from app.agents.schemas.event import AgentEvent
from app.agents.schemas.task import AgentTask, CollaborationBudget, InterruptRecord, RecoveryRecord, TaskResult


def test_agent_task_accepts_day1_fields():
    task = AgentTask(
        task_id="task-1",
        title="Verify foundation",
        status="in_progress",
        owner_agent_id="executor",
        role="verifier",
        goal="check output",
        expected_evidence=[{"type": "assertion"}],
        evidence=[{"type": "log"}],
        result_summary="running",
    )

    assert task.task_id == "task-1"
    assert task.owner_agent_id == "executor"
    assert task.status == "in_progress"
    assert task.expected_evidence == [{"type": "assertion"}]
    assert task.evidence == [{"type": "log"}]
    assert task.result_summary == "running"


def test_agent_task_accepts_day3_runtime_fields():
    task = AgentTask(
        task_id="task-2",
        title="Recover interrupted collaboration",
        owner_agent_id="executor",
        parent_task_id="task-1",
        child_task_ids=["task-2a"],
        thread_id="thread-1",
        message_id="msg-1",
        message_index=2,
        interrupt_records=[
            InterruptRecord(
                interrupt_id="interrupt-1",
                reason="manual stop",
                requested_by="coordinator",
            )
        ],
        recovery_records=[
            RecoveryRecord(
                recovery_id="recovery-1",
                source_interrupt_id="interrupt-1",
                resumed_from_task_id="task-2",
                resumed_from_thread_id="thread-1",
                strategy="resume_from_checkpoint",
            )
        ],
        collaboration_budget=CollaborationBudget(
            mode="collaboration",
            max_parallel_tasks=2,
            remaining_parallel_tasks=1,
            max_tool_calls=4,
            remaining_tool_calls=3,
            max_iterations=5,
            remaining_iterations=4,
            escalation_threshold=1,
            metadata={"max_spawn_depth": 2},
        ),
    )

    assert task.parent_task_id == "task-1"
    assert task.child_task_ids == ["task-2a"]
    assert task.thread_id == "thread-1"
    assert task.message_id == "msg-1"
    assert task.message_index == 2
    assert task.interrupt_records[0].interrupt_id == "interrupt-1"
    assert task.recovery_records[0].recovery_id == "recovery-1"
    assert task.collaboration_budget.mode == "collaboration"
    assert task.collaboration_budget.metadata == {"max_spawn_depth": 2}


def test_agent_event_accepts_day1_fields():
    event = AgentEvent(
        event_id="evt-1",
        event_type="agent.verify.completed",
        conversation_id="conv-1",
        agent_id="executor",
        sub_commander_id="executor_tasks",
        task_id="task-1",
        payload={"status": "passed"},
        severity="info",
    )

    assert event.event_id == "evt-1"
    assert event.event_type == "agent.verify.completed"
    assert event.conversation_id == "conv-1"
    assert event.payload == {"status": "passed"}
    assert event.severity == "info"


def test_agent_event_accepts_day3_trace_fields():
    event = AgentEvent(
        event_id="evt-2",
        event_type="agent.collaboration.budget.updated",
        conversation_id="conv-1",
        agent_id="coordinator",
        task_id="task-2",
        parent_task_id="task-1",
        child_task_id="task-2a",
        thread_id="thread-1",
        message_id="msg-3",
        interrupt_id="interrupt-1",
        recovery_id="recovery-1",
        payload={"remaining_parallel_tasks": 1},
        severity="warning",
    )

    assert event.parent_task_id == "task-1"
    assert event.child_task_id == "task-2a"
    assert event.thread_id == "thread-1"
    assert event.message_id == "msg-3"
    assert event.interrupt_id == "interrupt-1"
    assert event.recovery_id == "recovery-1"
    assert event.severity == "warning"


def test_task_result_supports_collaboration_result_fields():
    result = TaskResult(
        task_id="task-1",
        status="completed",
        summary="retrieval finished",
        evidence=[{"type": "source"}],
        owner_agent_id="librarian",
        next_action="handoff_to_analyst",
    )

    assert result.status == "completed"
    assert result.owner_agent_id == "librarian"
    assert result.next_action == "handoff_to_analyst"


def test_task_result_supports_day3_thread_budget_and_recovery_fields():
    result = TaskResult(
        task_id="task-2",
        status="blocked",
        owner_agent_id="executor",
        parent_task_id="task-1",
        child_task_ids=["task-2a"],
        thread_id="thread-1",
        message_id="msg-4",
        message_index=4,
        interrupt_records=[{"interrupt_id": "interrupt-1", "reason": "budget exceeded"}],
        recovery_records=[{"recovery_id": "recovery-1", "strategy": "resume_after_budget_reset"}],
        budget_snapshot=CollaborationBudget(
            mode="collaboration",
            max_parallel_tasks=2,
            remaining_parallel_tasks=0,
            max_tool_calls=4,
            remaining_tool_calls=0,
        ),
        next_action="resume_after_budget_reset",
    )

    assert result.parent_task_id == "task-1"
    assert result.child_task_ids == ["task-2a"]
    assert result.thread_id == "thread-1"
    assert result.message_id == "msg-4"
    assert result.message_index == 4
    assert result.interrupt_records[0].interrupt_id == "interrupt-1"
    assert result.recovery_records[0].recovery_id == "recovery-1"
    assert result.budget_snapshot.mode == "collaboration"
    assert result.budget_snapshot.remaining_parallel_tasks == 0
    assert result.next_action == "resume_after_budget_reset"
