import { z } from 'zod';

export const AgentDispatchContextSchema = z.object({
  originSurface: z.enum(['browser', 'mobile', 'desktop', 'web', 'product-api']),
  originProductId: z.string().min(1),
  dispatchMode: z.enum(['interactive', 'queued', 'scheduled', 'remote']),
  initiatedAt: z.string().datetime(),
});

export const AgentSessionStatusSchema = z.enum([
  'active',
  'paused',
  'waiting-approval',
  'completed',
  'failed',
  'cancelled',
]);

export const AgentSessionSchema = z.object({
  sessionId: z.string().min(1),
  productId: z.string().min(1),
  userId: z.string().min(1),
  status: AgentSessionStatusSchema,
  startedAt: z.string().datetime(),
  updatedAt: z.string().datetime(),
  resumable: z.boolean(),
  currentTaskId: z.string().min(1).nullable().optional(),
  memoryRefs: z.array(z.string().min(1)),
  artifactRefs: z.array(z.string().min(1)),
  approvalRefs: z.array(z.string().min(1)),
  dispatchContext: AgentDispatchContextSchema.nullable().optional(),
});

export const AgentTaskStatusSchema = z.enum([
  'queued',
  'running',
  'blocked',
  'completed',
  'failed',
  'cancelled',
]);

export const AgentTaskSchema = z.object({
  taskId: z.string().min(1),
  sessionId: z.string().min(1),
  title: z.string().min(1),
  intent: z.string().min(1),
  status: AgentTaskStatusSchema,
  priority: z.string().min(1).nullable().optional(),
  createdAt: z.string().datetime(),
  updatedAt: z.string().datetime(),
});

export const AgentTodoStatusSchema = z.enum(['open', 'in-progress', 'done', 'dropped']);

export const AgentTodoSchema = z.object({
  todoId: z.string().min(1),
  sessionId: z.string().min(1),
  text: z.string().min(1),
  status: AgentTodoStatusSchema,
  createdAt: z.string().datetime(),
  updatedAt: z.string().datetime(),
});

export const AgentCheckpointStatusSchema = z.enum([
  'queued',
  'running',
  'paused',
  'waiting-approval',
  'completed',
  'failed',
  'cancelled',
]);

export const AgentCheckpointSchema = z.object({
  checkpointId: z.string().min(1),
  sessionId: z.string().min(1),
  runId: z.string().min(1).nullable().optional(),
  productId: z.string().min(1),
  userId: z.string().min(1),
  createdAt: z.string().datetime(),
  statusAtCapture: AgentCheckpointStatusSchema,
  currentTaskId: z.string().min(1).nullable().optional(),
  checkpointArtifactId: z.string().min(1).nullable().optional(),
  todoIds: z.array(z.string().min(1)),
  artifactRefs: z.array(z.string().min(1)),
  memoryRefs: z.array(z.string().min(1)),
  approvalRefs: z.array(z.string().min(1)),
  dispatchContext: AgentDispatchContextSchema.nullable().optional(),
  resumeToken: z.string().min(1).nullable().optional(),
  stateSummary: z.object({
    title: z.string().min(1),
    summary: z.string().min(1),
    lastActionAt: z.string().datetime().nullable().optional(),
  }),
});

export const AgentRunStatusSchema = z.enum([
  'queued',
  'running',
  'paused',
  'waiting-approval',
  'completed',
  'failed',
  'cancelled',
]);

export const AgentRunSchema = z.object({
  runId: z.string().min(1),
  sessionId: z.string().min(1),
  productId: z.string().min(1),
  status: AgentRunStatusSchema,
  startedAt: z.string().datetime(),
  completedAt: z.string().datetime().nullable().optional(),
  checkpointArtifactId: z.string().min(1).nullable().optional(),
  correlationId: z.string().min(1).nullable().optional(),
});

export const AgentApprovalCheckpointSchema = z.object({
  approvalId: z.string().min(1),
  sessionId: z.string().min(1),
  runId: z.string().min(1),
  actionLabel: z.string().min(1),
  riskLevel: z.enum(['low', 'medium', 'high', 'critical']),
  status: z.enum(['requested', 'approved', 'denied', 'expired']),
  requestedAt: z.string().datetime(),
  resolvedAt: z.string().datetime().nullable().optional(),
  resolverSurface: z.enum(['mobile', 'web', 'desktop']).nullable().optional(),
});

export const AgentDispatchRequestSchema = z.object({
  dispatchId: z.string().min(1),
  targetProductId: z.string().min(1),
  targetExecutor: z.enum(['cowork', 'jarvisjr', 'flowmonk', 'generic-agent']),
  userId: z.string().min(1),
  title: z.string().min(1),
  intent: z.string().min(1),
  artifactRefs: z.array(z.string().min(1)).default([]),
  memoryRefs: z.array(z.string().min(1)).default([]),
  dispatchContext: AgentDispatchContextSchema,
});

export const AgentActionLogSchema = z.object({
  actionLogId: z.string().min(1),
  sessionId: z.string().min(1),
  runId: z.string().min(1),
  eventName: z.string().min(1),
  occurredAt: z.string().datetime(),
  actorType: z.enum(['user', 'agent', 'system', 'device']),
  correlationId: z.string().min(1).nullable().optional(),
  payload: z.record(z.unknown()),
});

export type AgentDispatchContext = z.infer<typeof AgentDispatchContextSchema>;
export type AgentSession = z.infer<typeof AgentSessionSchema>;
export type AgentTask = z.infer<typeof AgentTaskSchema>;
export type AgentTodo = z.infer<typeof AgentTodoSchema>;
export type AgentCheckpoint = z.infer<typeof AgentCheckpointSchema>;
export type AgentRun = z.infer<typeof AgentRunSchema>;
export type AgentApprovalCheckpoint = z.infer<typeof AgentApprovalCheckpointSchema>;
export type AgentDispatchRequest = z.infer<typeof AgentDispatchRequestSchema>;
export type AgentActionLog = z.infer<typeof AgentActionLogSchema>;
