import { describe, expect, it } from 'vitest';
import {
  AgentActionLogSchema,
  AgentCheckpointSchema,
  AgentApprovalCheckpointSchema,
  AgentDispatchRequestSchema,
  AgentRunSchema,
  AgentSessionSchema,
  AgentTaskSchema,
  AgentTodoSchema,
} from './agent-runtime.js';

describe('agent runtime contract baseline', () => {
  it('validates a dispatched Cowork session with task and approval checkpoint', () => {
    const session = AgentSessionSchema.parse({
      sessionId: 'sess_cowork_1',
      productId: 'cowork',
      userId: 'saravana',
      status: 'waiting-approval',
      startedAt: '2026-04-03T16:00:00.000Z',
      updatedAt: '2026-04-03T16:05:00.000Z',
      resumable: true,
      currentTaskId: 'task_cowork_1',
      memoryRefs: ['mem_proj_1'],
      artifactRefs: ['art_note_1'],
      approvalRefs: ['approval_1'],
      dispatchContext: {
        originSurface: 'browser',
        originProductId: 'notelett',
        dispatchMode: 'remote',
        initiatedAt: '2026-04-03T15:59:00.000Z',
      },
    });

    const task = AgentTaskSchema.parse({
      taskId: 'task_cowork_1',
      sessionId: session.sessionId,
      title: 'Investigate imported roadmap risks',
      intent: 'Audit the imported note and produce findings',
      status: 'blocked',
      priority: 'high',
      createdAt: '2026-04-03T16:00:00.000Z',
      updatedAt: '2026-04-03T16:05:00.000Z',
    });

    const approval = AgentApprovalCheckpointSchema.parse({
      approvalId: 'approval_1',
      sessionId: session.sessionId,
      runId: 'run_cowork_1',
      actionLabel: 'Delete temporary export',
      riskLevel: 'high',
      status: 'requested',
      requestedAt: '2026-04-03T16:05:00.000Z',
      resolvedAt: null,
      resolverSurface: null,
    });

    expect(session.dispatchContext?.originSurface).toBe('browser');
    expect(task.status).toBe('blocked');
    expect(approval.status).toBe('requested');
  });

  it('validates a scheduled FlowMonk run with todos and action logs', () => {
    const dispatch = AgentDispatchRequestSchema.parse({
      dispatchId: 'dispatch_flow_1',
      targetProductId: 'flowmonk',
      targetExecutor: 'flowmonk',
      userId: 'saravana',
      title: 'Execute weekly planning refresh',
      intent: 'Rebuild plan, routines, and habits for next week',
      artifactRefs: ['art_plan_template_1'],
      memoryRefs: ['mem_goal_1'],
      dispatchContext: {
        originSurface: 'web',
        originProductId: 'flowmonk',
        dispatchMode: 'scheduled',
        initiatedAt: '2026-04-03T17:00:00.000Z',
      },
    });

    const run = AgentRunSchema.parse({
      runId: 'run_flow_1',
      sessionId: 'sess_flow_1',
      productId: 'flowmonk',
      status: 'waiting-approval',
      startedAt: '2026-04-03T17:01:00.000Z',
      completedAt: null,
      checkpointArtifactId: 'art_plan_1',
      correlationId: 'corr_phase2',
    });

    const todo = AgentTodoSchema.parse({
      todoId: 'todo_flow_1',
      sessionId: 'sess_flow_1',
      text: 'Generate routines from approved plan',
      status: 'in-progress',
      createdAt: '2026-04-03T17:01:30.000Z',
      updatedAt: '2026-04-03T17:02:00.000Z',
    });

    const actionLog = AgentActionLogSchema.parse({
      actionLogId: 'alog_flow_1',
      sessionId: 'sess_flow_1',
      runId: 'run_flow_1',
      eventName: 'agent.run.started',
      occurredAt: '2026-04-03T17:01:00.000Z',
      actorType: 'agent',
      correlationId: 'corr_phase2',
      payload: {
        dispatchId: dispatch.dispatchId,
        title: dispatch.title,
      },
    });

    expect(dispatch.dispatchContext.dispatchMode).toBe('scheduled');
    expect(run.status).toBe('waiting-approval');
    expect(run.checkpointArtifactId).toBe('art_plan_1');
    expect(todo.status).toBe('in-progress');
    expect(actionLog.eventName).toBe('agent.run.started');
  });

  it('accepts queued runs as a first-class runtime state', () => {
    const run = AgentRunSchema.parse({
      runId: 'run_queued_1',
      sessionId: 'sess_queued_1',
      productId: 'clawcowork',
      status: 'queued',
      startedAt: '2026-04-04T10:00:00.000Z',
      completedAt: null,
      checkpointArtifactId: null,
      correlationId: 'corr_queued_1',
    });

    expect(run.status).toBe('queued');
  });

  it('validates checkpoint summaries used for resume review', () => {
    const checkpoint = AgentCheckpointSchema.parse({
      checkpointId: 'ckpt_cowork_1',
      sessionId: 'sess_cowork_1',
      runId: 'run_cowork_1',
      productId: 'clawcowork',
      userId: 'saravana',
      createdAt: '2026-04-04T12:00:00.000Z',
      statusAtCapture: 'waiting-approval',
      currentTaskId: 'task_cowork_1',
      checkpointArtifactId: 'artifact://notelett/note-1',
      todoIds: ['todo_cowork_1'],
      artifactRefs: ['artifact://notelett/note-1'],
      memoryRefs: [],
      approvalRefs: ['approval_1'],
      dispatchContext: {
        originSurface: 'desktop',
        originProductId: 'clawcowork',
        dispatchMode: 'interactive',
        initiatedAt: '2026-04-04T11:58:00.000Z',
      },
      resumeToken: 'task_cowork_1',
      stateSummary: {
        title: 'Investigate imported roadmap risks',
        summary: 'Paused for approval after scanning the repository and proposing changes.',
        lastActionAt: '2026-04-04T12:00:00.000Z',
      },
    });

    expect(checkpoint.statusAtCapture).toBe('waiting-approval');
    expect(checkpoint.resumeToken).toBe('task_cowork_1');
    expect(checkpoint.checkpointArtifactId).toBe('artifact://notelett/note-1');
  });
});
