/**
 * Schema definitions for session persistence types.
 * Uses Zod for runtime validation and type inference.
 */
import { z } from 'zod';
// ============================================================================
// Base Schemas
// ============================================================================
/**
 * Agent operating mode.
 * - 'agent': Free-form coding assistance (default mode)
 * - 'spec': Structured spec-driven development workflow
 */
export const AgentModeSchema = z.enum(['agent', 'spec']);
export const ToolCallStatusSchema = z.enum([
    'pending',
    'awaiting_approval',
    'approved',
    'denied',
    'executing',
    'completed',
    'failed',
]);
export const ToolKindSchema = z.enum([
    'read',
    'edit',
    'execute',
    'search',
    'delete',
    'move',
    'fetch',
    'think',
    'switch_mode',
    'other',
]);
// ============================================================================
// Context Item Schema
// ============================================================================
export const ContextItemSchema = z.object({
    name: z.string(),
    description: z.string().optional(),
    content: z.string(),
    uri: z.string().optional(),
});
// ============================================================================
// Message Payload Schemas
// ============================================================================
export const UserMessagePayloadSchema = z.object({
    type: z.literal('user'),
    content: z.string(),
    contextItems: z.array(ContextItemSchema).optional(),
    source: z.enum(['chat', 'hook', 'api']).optional(),
    images: z.array(z.object({ data: z.string(), mimeType: z.string() })).optional(),
});
export const AssistantMessagePayloadSchema = z.object({
    type: z.literal('assistant'),
    content: z.string(),
    operationType: z.enum(['Say', 'Reasoning', 'Print']).optional(),
    executionId: z.string().optional(),
    subExecutionId: z.string().optional(),
});
export const ToolCallPayloadSchema = z.object({
    type: z.literal('tool_call'),
    toolCallId: z.string(),
    toolName: z.string(),
    args: z.record(z.unknown()),
    status: ToolCallStatusSchema,
    title: z.string().optional(),
    kind: ToolKindSchema.optional(),
    executionId: z.string().optional(),
    actionType: z.string().optional(),
    filePath: z.string().optional(),
    preSnapshotId: z.string().optional(),
    postSnapshotId: z.string().optional(),
    subExecutionId: z.string().optional(),
});
export const ToolResultPayloadSchema = z.object({
    type: z.literal('tool_result'),
    toolCallId: z.string(),
    content: z.string(),
    success: z.boolean(),
    durationMs: z.number().optional(),
    executionId: z.string().optional(),
    metadata: z.record(z.unknown()).optional(),
    subExecutionId: z.string().optional(),
});
export const SystemMessagePayloadSchema = z.object({
    type: z.literal('system'),
    content: z.string(),
});
export const ErrorPayloadSchema = z.object({
    type: z.literal('error'),
    message: z.string(),
    code: z.string().optional(),
    details: z.record(z.unknown()).optional(),
});
export const ModeChangePayloadSchema = z.object({
    type: z.literal('mode_change'),
    fromMode: AgentModeSchema,
    toMode: AgentModeSchema,
    reason: z.string().optional(),
});
export const SessionEventPayloadSchema = z.object({
    type: z.literal('session_event'),
    category: z.enum(['session_start', 'session_restore', 'session_pause', 'session_resume']),
    context: z.record(z.unknown()).optional(),
});
export const SubAgentStartPayloadSchema = z.object({
    type: z.literal('sub_agent_start'),
    parentExecutionId: z.string(),
    subSessionId: z.string(),
    subAgentName: z.string(),
    prompt: z.string(),
    explanation: z.string(),
});
export const SubAgentCompletePayloadSchema = z.object({
    type: z.literal('sub_agent_complete'),
    parentExecutionId: z.string(),
    subSessionId: z.string(),
    response: z.string(),
    status: z.enum(['success', 'error', 'cancelled']),
    errorMessage: z.string().optional(),
});
export const SubAgentProgressPayloadSchema = z.object({
    type: z.literal('sub_agent_progress'),
    parentExecutionId: z.string(),
    subSessionId: z.string(),
    message: z.string(),
});
export const SteeringInclusionPayloadSchema = z.object({
    type: z.literal('steering_inclusion'),
    documents: z.array(z.string()),
    executionId: z.string().optional(),
    subExecutionId: z.string().optional(),
});
// ============================================================================
// Combined Message Payload Schema
// ============================================================================
export const MessagePayloadSchema = z.discriminatedUnion('type', [
    UserMessagePayloadSchema,
    AssistantMessagePayloadSchema,
    ToolCallPayloadSchema,
    ToolResultPayloadSchema,
    SystemMessagePayloadSchema,
    ErrorPayloadSchema,
    ModeChangePayloadSchema,
    SessionEventPayloadSchema,
    SubAgentStartPayloadSchema,
    SubAgentCompletePayloadSchema,
    SubAgentProgressPayloadSchema,
    SteeringInclusionPayloadSchema,
]);
// ============================================================================
// Session Metadata Schema
// ============================================================================
export const SessionMetadataSchema = z.object({
    schemaVersion: z.string(),
    id: z.string(),
    title: z.string(),
    agentMode: AgentModeSchema,
    workspacePaths: z.array(z.string()),
    createdAt: z.string(),
    lastModifiedAt: z.string(),
    lastCheckpointId: z.string().optional(),
    parentSessionId: z.string().optional(),
    parentExecutionId: z.string().optional(),
});
// ============================================================================
// Persisted Message Schema
// ============================================================================
export const PersistedMessageSchema = z.object({
    id: z.string(),
    timestamp: z.string(),
    payload: MessagePayloadSchema,
});
// ============================================================================
// Persisted Session Schema
// ============================================================================
export const PersistedSessionSchema = z.object({
    metadata: SessionMetadataSchema,
    messages: z.array(PersistedMessageSchema),
});
// ============================================================================
// Session Summary and Listing Types
// ============================================================================
export const SessionSummarySchema = z.object({
    id: z.string(),
    title: z.string(),
    agentMode: AgentModeSchema,
    createdAt: z.string(),
    lastModifiedAt: z.string(),
    workspacePaths: z.array(z.string()),
});
export const SessionSummariesSchema = z.array(SessionSummarySchema);
export const ListSessionsResponseSchema = z.object({ sessions: SessionSummariesSchema });
export const ListSessionsOptionsSchema = z.object({
    limit: z.number().optional(),
    sortBy: z.enum(['createdAt', 'lastModifiedAt']).optional(),
    sortOrder: z.enum(['asc', 'desc']).optional(),
});
export const SCHEMA_VERSION = '0.0.1';
