import { z } from 'zod';
import { ExecutionRunPublicStateSchema } from './executionRuns.js';
import { SessionStoredMessageContentSchema } from './sessionMessages/sessionStoredMessageContent.js';
const TimestampMsSchema = z.number().int().min(0);
const Base64Schema = z.string();
const VersionedNullableStringSchema = z.object({
    value: z.string().nullable(),
    version: z.number().int(),
}).passthrough();
const VersionedStringSchema = z.object({
    value: z.string(),
    version: z.number().int(),
}).passthrough();
export const UpdateBodySchema = z.discriminatedUnion('t', [
    z.object({
        t: z.literal('new-message'),
        sid: z.string(),
        message: z
            .object({
            id: z.string(),
            seq: z.number().int().min(0),
            content: SessionStoredMessageContentSchema,
            localId: z.string().nullable(),
            sidechainId: z.string().nullable().optional(),
            createdAt: TimestampMsSchema,
            updatedAt: TimestampMsSchema,
        })
            .passthrough(),
    }).passthrough(),
    z.object({
        t: z.literal('message-updated'),
        sid: z.string(),
        message: z
            .object({
            id: z.string(),
            seq: z.number().int().min(0),
            content: SessionStoredMessageContentSchema,
            localId: z.string().nullable(),
            sidechainId: z.string().nullable().optional(),
            createdAt: TimestampMsSchema,
            updatedAt: TimestampMsSchema,
        })
            .passthrough(),
    }).passthrough(),
    z.object({
        t: z.literal('new-session'),
        id: z.string(),
        seq: z.number().int().min(0),
        metadata: Base64Schema,
        metadataVersion: z.number().int(),
        agentState: Base64Schema.nullable(),
        agentStateVersion: z.number().int(),
        dataEncryptionKey: Base64Schema.nullable(),
        active: z.boolean(),
        activeAt: TimestampMsSchema,
        createdAt: TimestampMsSchema,
        updatedAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('update-session'),
        id: z.string(),
        metadata: VersionedNullableStringSchema.optional(),
        agentState: VersionedNullableStringSchema.optional(),
        lastViewedSessionSeq: z.number().int().min(0).optional(),
        pendingPermissionRequestCount: z.number().int().min(0).optional(),
        pendingUserActionRequestCount: z.number().int().min(0).optional(),
    }).passthrough(),
    z.object({
        t: z.literal('pending-changed'),
        sid: z.string(),
        sessionId: z.string().optional(),
        pendingVersion: z.number().int().min(0),
        pendingCount: z.number().int().min(0),
        changedByAccountId: z.string().optional(),
    }).passthrough(),
    z.object({
        t: z.literal('automation-upsert'),
        automationId: z.string(),
        version: z.number().int().min(0),
        enabled: z.boolean(),
        updatedAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('automation-delete'),
        automationId: z.string(),
        deletedAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('automation-run-updated'),
        runId: z.string(),
        automationId: z.string(),
        state: z.enum(['queued', 'claimed', 'running', 'succeeded', 'failed', 'cancelled', 'expired']),
        scheduledAt: TimestampMsSchema,
        startedAt: TimestampMsSchema.nullable().optional(),
        finishedAt: TimestampMsSchema.nullable().optional(),
        updatedAt: TimestampMsSchema,
        machineId: z.string().nullable().optional(),
    }).passthrough(),
    z.object({
        t: z.literal('automation-assignment-updated'),
        machineId: z.string(),
        automationId: z.string(),
        enabled: z.boolean(),
        updatedAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('delete-session'),
        sid: z.string(),
    }).passthrough(),
    z.object({
        t: z.literal('update-account'),
        id: z.string(),
    }).passthrough(),
    z.object({
        t: z.literal('new-machine'),
        machineId: z.string(),
        seq: z.number().int().min(0),
        metadata: Base64Schema,
        metadataVersion: z.number().int(),
        daemonState: Base64Schema.nullable(),
        daemonStateVersion: z.number().int(),
        dataEncryptionKey: Base64Schema.nullable(),
        active: z.boolean(),
        activeAt: TimestampMsSchema,
        createdAt: TimestampMsSchema,
        updatedAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('update-machine'),
        machineId: z.string(),
        metadata: VersionedStringSchema.optional(),
        daemonState: VersionedStringSchema.optional(),
        activeAt: TimestampMsSchema.optional(),
        active: z.boolean().optional(),
        revokedAt: TimestampMsSchema.nullable().optional(),
    }).passthrough(),
    z.object({
        t: z.literal('new-artifact'),
        artifactId: z.string(),
        seq: z.number().int().min(0),
        header: Base64Schema,
        headerVersion: z.number().int(),
        body: Base64Schema,
        bodyVersion: z.number().int(),
        dataEncryptionKey: Base64Schema,
        createdAt: TimestampMsSchema,
        updatedAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('update-artifact'),
        artifactId: z.string(),
        header: VersionedStringSchema.optional(),
        body: VersionedStringSchema.optional(),
    }).passthrough(),
    z.object({
        t: z.literal('delete-artifact'),
        artifactId: z.string(),
    }).passthrough(),
    z.object({
        t: z.literal('relationship-updated'),
        uid: z.string(),
        status: z.enum(['none', 'requested', 'pending', 'friend', 'rejected']),
        timestamp: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('new-feed-post'),
        id: z.string(),
        body: z.unknown(),
        cursor: z.string(),
        createdAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('kv-batch-update'),
        changes: z.array(z.object({
            key: z.string(),
            value: z.string().nullable(),
            version: z.number().int(),
        }).passthrough()),
    }).passthrough(),
    z.object({
        t: z.literal('session-shared'),
        sessionId: z.string(),
        shareId: z.string(),
        sharedBy: z.object({
            id: z.string(),
            firstName: z.string().nullable(),
            lastName: z.string().nullable(),
            username: z.string().nullable(),
            avatar: z.unknown().nullable(),
        }).passthrough(),
        accessLevel: z.enum(['view', 'edit', 'admin']),
        encryptedDataKey: Base64Schema.optional(),
        createdAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('session-share-updated'),
        sessionId: z.string(),
        shareId: z.string(),
        accessLevel: z.enum(['view', 'edit', 'admin']),
        updatedAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('session-share-revoked'),
        sessionId: z.string(),
        shareId: z.string(),
    }).passthrough(),
    z.object({
        t: z.literal('public-share-created'),
        sessionId: z.string(),
        publicShareId: z.string(),
        token: z.string(),
        expiresAt: TimestampMsSchema.nullable(),
        maxUses: z.number().int().nullable(),
        isConsentRequired: z.boolean(),
        createdAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('public-share-updated'),
        sessionId: z.string(),
        publicShareId: z.string(),
        expiresAt: TimestampMsSchema.nullable(),
        maxUses: z.number().int().nullable(),
        isConsentRequired: z.boolean(),
        updatedAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        t: z.literal('public-share-deleted'),
        sessionId: z.string(),
    }).passthrough(),
]);
export const UpdateContainerSchema = z.object({
    id: z.string(),
    seq: z.number().int().min(0),
    createdAt: TimestampMsSchema,
    body: UpdateBodySchema,
}).passthrough();
export const EphemeralUpdateSchema = z.discriminatedUnion('type', [
    z.object({
        type: z.literal('activity'),
        id: z.string(),
        active: z.boolean(),
        activeAt: TimestampMsSchema,
        thinking: z.boolean().optional(),
    }).passthrough(),
    z.object({
        type: z.literal('execution-run-updated'),
        sessionId: z.string(),
        run: ExecutionRunPublicStateSchema,
    }).passthrough(),
    z.object({
        type: z.literal('machine-activity'),
        id: z.string(),
        active: z.boolean(),
        activeAt: TimestampMsSchema,
    }).passthrough(),
    z.object({
        type: z.literal('usage'),
        id: z.string(),
        key: z.string(),
        tokens: z.record(z.string(), z.number()),
        cost: z.record(z.string(), z.number()),
        timestamp: TimestampMsSchema,
    }).passthrough(),
    z.object({
        type: z.literal('machine-status'),
        machineId: z.string(),
        online: z.boolean(),
        timestamp: TimestampMsSchema,
    }).passthrough(),
]);
// Broadcast-safe events (cursorless).
//
// These are intended for cases where a single identical payload can be emitted to a shared room (e.g. `session:${sessionId}`)
// without carrying per-account cursors or recipient-specific secrets.
//
// Important: clients must treat these as optional hints/optimizations only, never as the sole source of truth.
export const SessionBroadcastBodySchema = z.discriminatedUnion('t', [
    z.object({
        t: z.literal('session-changed'),
        sessionId: z.string(),
    }).passthrough(),
]);
export const SessionBroadcastContainerSchema = z.object({
    id: z.string(),
    createdAt: TimestampMsSchema,
    body: SessionBroadcastBodySchema,
}).passthrough();
export const MessageAckResponseSchema = z.union([
    z.object({
        ok: z.literal(true),
        id: z.string(),
        seq: z.number().int().min(0),
        localId: z.string().nullable(),
        /**
         * Whether the server actually created a new transcript row.
         *
         * - true: new message created + broadcast emitted (subject to sender-skip rules).
         * - false: idempotent duplicate (sessionId, localId) already existed; no broadcast is emitted.
         *
         * Optional for backward compatibility with older servers.
         */
        didWrite: z.boolean().optional(),
        didUpdate: z.boolean().optional(),
    }).passthrough(),
    z.object({
        ok: z.literal(false),
        error: z.string(),
    }).passthrough(),
]);
export const UpdateMetadataAckResponseSchema = z.discriminatedUnion('result', [
    z.object({
        result: z.literal('success'),
        version: z.number().int(),
        metadata: z.string(),
    }).passthrough(),
    z.object({
        result: z.literal('version-mismatch'),
        version: z.number().int(),
        metadata: z.string(),
    }).passthrough(),
    z.object({
        result: z.literal('forbidden'),
    }).passthrough(),
    z.object({
        result: z.literal('error'),
    }).passthrough(),
]);
export const UpdateStateAckResponseSchema = z.discriminatedUnion('result', [
    z.object({
        result: z.literal('success'),
        version: z.number().int(),
        agentState: z.string().nullable(),
    }).passthrough(),
    z.object({
        result: z.literal('version-mismatch'),
        version: z.number().int(),
        agentState: z.string().nullable(),
    }).passthrough(),
    z.object({
        result: z.literal('forbidden'),
    }).passthrough(),
    z.object({
        result: z.literal('error'),
    }).passthrough(),
]);
//# sourceMappingURL=updates.js.map