import { t } from 'elysia'

export const ObservabilityModel = {
  createEventBody: t.Object({
    source: t.String(),
    code: t.String(),
    severity: t.String(),
    category: t.String(),
    message: t.String(),
    attrs: t.Optional(t.Record(t.String(), t.Unknown())),
    chatSessionId: t.Optional(t.String()),
    runId: t.Optional(t.String()),
    messageId: t.Optional(t.String()),
    traceId: t.Optional(t.String()),
    dedupeKey: t.Optional(t.String()),
    parentEventId: t.Optional(t.String()),
    occurredAt: t.Optional(t.Number()),
    recordedAt: t.Optional(t.Number()),
  }),

  createEventResponse: t.Object({
    ok: t.Literal(true),
  }),

  runtimeSampleBody: t.Object({
    source: t.Literal('desktop-main'),
    sampledAt: t.Number(),
    main: t.Record(t.String(), t.Unknown()),
    appMetrics: t.Array(t.Record(t.String(), t.Unknown())),
    windows: t.Array(t.Record(t.String(), t.Unknown())),
    diagnostics: t.Optional(t.Record(t.String(), t.Unknown())),
  }),

  heapSnapshotBody: t.Object({
    token: t.Optional(t.String()),
  }),

  heapSnapshotResponse: t.Object({
    ok: t.Literal(true),
    path: t.String(),
    pid: t.Number(),
    startedAt: t.Number(),
    completedAt: t.Number(),
    durationMs: t.Number(),
  }),

  event: t.Object({
    id: t.String(),
    schemaVersion: t.Number(),
    source: t.String(),
    code: t.String(),
    severity: t.String(),
    category: t.String(),
    message: t.String(),
    attrs: t.Optional(t.Record(t.String(), t.Unknown())),
    chatSessionId: t.Optional(t.String()),
    runId: t.Optional(t.String()),
    messageId: t.Optional(t.String()),
    traceId: t.Optional(t.String()),
    dedupeKey: t.Optional(t.String()),
    parentEventId: t.Optional(t.String()),
    occurredAt: t.Number(),
    recordedAt: t.Number(),
  }),

  incident: t.Object({
    id: t.String(),
    dedupeKey: t.String(),
    code: t.String(),
    severity: t.String(),
    status: t.Union([t.Literal('open'), t.Literal('resolved')]),
    source: t.String(),
    message: t.String(),
    chatSessionId: t.Optional(t.String()),
    runId: t.Optional(t.String()),
    messageId: t.Optional(t.String()),
    firstOccurredAt: t.Number(),
    lastOccurredAt: t.Number(),
    lastRecordedAt: t.Number(),
    count: t.Number(),
    lastEventId: t.Optional(t.String()),
    attrs: t.Optional(t.Record(t.String(), t.Unknown())),
  }),

  errorPattern: t.Object({
    patternId: t.String(),
    source: t.String(),
    code: t.String(),
    category: t.String(),
    severity: t.String(),
    runtimeKind: t.Optional(t.String()),
    providerTargetId: t.Optional(t.String()),
    modelId: t.Optional(t.String()),
    messageFingerprint: t.String(),
    messagePreview: t.String(),
    count: t.Number(),
    firstSeenAt: t.Number(),
    lastSeenAt: t.Number(),
    sampleRunIds: t.Array(t.String()),
    sampleTraceIds: t.Array(t.String()),
    sampleMessages: t.Array(t.String()),
  }),

  bundle: t.Object({
    schema: t.String(),
    exportedAt: t.Number(),
    metadata: t.Record(t.String(), t.Unknown()),
    redaction: t.Record(t.String(), t.Unknown()),
    events: t.Array(t.Object({
      id: t.String(),
      schemaVersion: t.Number(),
      source: t.String(),
      code: t.String(),
      severity: t.String(),
      category: t.String(),
      message: t.String(),
      attrs: t.Optional(t.Record(t.String(), t.Unknown())),
      chatSessionId: t.Optional(t.String()),
      runId: t.Optional(t.String()),
      messageId: t.Optional(t.String()),
      traceId: t.Optional(t.String()),
      dedupeKey: t.Optional(t.String()),
      parentEventId: t.Optional(t.String()),
      occurredAt: t.Number(),
      recordedAt: t.Number(),
    })),
    incidents: t.Array(t.Object({
      id: t.String(),
      dedupeKey: t.String(),
      code: t.String(),
      severity: t.String(),
      status: t.Union([t.Literal('open'), t.Literal('resolved')]),
      source: t.String(),
      message: t.String(),
      chatSessionId: t.Optional(t.String()),
      runId: t.Optional(t.String()),
      messageId: t.Optional(t.String()),
      firstOccurredAt: t.Number(),
      lastOccurredAt: t.Number(),
      lastRecordedAt: t.Number(),
      count: t.Number(),
      lastEventId: t.Optional(t.String()),
      attrs: t.Optional(t.Record(t.String(), t.Unknown())),
    })),
    errorPatterns: t.Array(t.Object({
      patternId: t.String(),
      source: t.String(),
      code: t.String(),
      category: t.String(),
      severity: t.String(),
      runtimeKind: t.Optional(t.String()),
      providerTargetId: t.Optional(t.String()),
      modelId: t.Optional(t.String()),
      messageFingerprint: t.String(),
      messagePreview: t.String(),
      count: t.Number(),
      firstSeenAt: t.Number(),
      lastSeenAt: t.Number(),
      sampleRunIds: t.Array(t.String()),
      sampleTraceIds: t.Array(t.String()),
      sampleMessages: t.Array(t.String()),
    })),
    timeline: t.Array(t.Record(t.String(), t.Unknown())),
    logs: t.Record(t.String(), t.Unknown()),
  }),

  eventsQuery: t.Object({
    chatSessionId: t.Optional(t.String()),
    runId: t.Optional(t.String()),
    code: t.Optional(t.String()),
    severity: t.Optional(t.String()),
    since: t.Optional(t.String()),
    until: t.Optional(t.String()),
    limit: t.Optional(t.String()),
  }),

  incidentsQuery: t.Object({
    dedupeKey: t.Optional(t.String()),
    chatSessionId: t.Optional(t.String()),
    runId: t.Optional(t.String()),
    code: t.Optional(t.String()),
    status: t.Optional(t.Union([t.Literal('open'), t.Literal('resolved')])),
    limit: t.Optional(t.String()),
  }),

  errorPatternsQuery: t.Object({
    chatSessionId: t.Optional(t.String()),
    runId: t.Optional(t.String()),
    code: t.Optional(t.String()),
    runtimeKind: t.Optional(t.String()),
    providerTargetId: t.Optional(t.String()),
    sinceUnix: t.Optional(t.String()),
    limit: t.Optional(t.String()),
  }),

  exportQuery: t.Object({
    chatSessionId: t.Optional(t.String()),
    runId: t.Optional(t.String()),
    sinceUnix: t.Optional(t.String()),
  }),

  flushResponse: t.Object({
    ok: t.Literal(true),
  }),

  runtimeSnapshot: t.Object({
    timestamp: t.Number(),
    server: t.Object({
      pid: t.Number(),
      uptimeSeconds: t.Number(),
      memory: t.Object({
        rssMB: t.Number(),
        heapUsedMB: t.Number(),
        heapTotalMB: t.Number(),
        externalMB: t.Number(),
        arrayBuffersMB: t.Number(),
      }),
      cpu: t.Object({
        percent: t.Nullable(t.Number()),
        userMicros: t.Number(),
        systemMicros: t.Number(),
        sampleMs: t.Nullable(t.Number()),
        usedMicros: t.Nullable(t.Number()),
        windowReady: t.Boolean(),
      }),
      node: t.Object({
        activeHandles: t.Number(),
        activeRequests: t.Number(),
      }),
    }),
    chatRuntime: t.Object({
      activeRuns: t.Array(t.Object({
        runId: t.String(),
        sessionId: t.String(),
        messageId: t.String(),
        providerTargetKind: t.Nullable(t.String()),
        providerTargetId: t.Nullable(t.String()),
        modelId: t.Nullable(t.String()),
      })),
      runStreams: t.Array(t.Object({
        runId: t.String(),
        latestCursor: t.Number(),
        publishedChunkCount: t.Number(),
        textDeltaCount: t.Number(),
        reasoningDeltaCount: t.Number(),
        toolInputDeltaCount: t.Number(),
        toolOutputCount: t.Number(),
        maxDeltaChars: t.Number(),
      })),
    }),
    providerRuntime: t.Object({
      hosts: t.Array(t.Object({
        hostId: t.String(),
        runtimeKind: t.String(),
        providerTargetId: t.String(),
        scopeId: t.String(),
        refCount: t.Number(),
        pinnedCount: t.Number(),
        hasResource: t.Boolean(),
        expiresAt: t.Number(),
        updatedAt: t.Number(),
      })),
    }),
    pty: t.Object({
      terminals: t.Array(t.Object({
        id: t.String(),
        role: t.Union([t.Literal('cli-tui'), t.Literal('bottom-panel')]),
        pid: t.Number(),
        executable: t.String(),
        cwd: t.String(),
        running: t.Boolean(),
        startedAt: t.Number(),
        cols: t.Number(),
        rows: t.Number(),
        rssMB: t.Nullable(t.Number()),
        cpuPercent: t.Nullable(t.Number()),
        descendantCount: t.Nullable(t.Number()),
      })),
      totals: t.Object({
        cliTuiRssMB: t.Number(),
        bottomPanelRssMB: t.Number(),
        cliTuiCpuPercent: t.Number(),
        bottomPanelCpuPercent: t.Number(),
      }),
      timestamp: t.Number(),
    }),
    chronicle: t.Object({
      running: t.Boolean(),
      pid: t.Nullable(t.Number()),
      rssMB: t.Nullable(t.Number()),
      cpuPercent: t.Nullable(t.Number()),
    }),
    opencodeServer: t.Object({
      running: t.Boolean(),
      pid: t.Nullable(t.Number()),
      url: t.Nullable(t.String()),
      startedAt: t.Nullable(t.Number()),
      uptimeSeconds: t.Nullable(t.Number()),
      rssMB: t.Nullable(t.Number()),
      cpuPercent: t.Nullable(t.Number()),
    }),
    kimiServer: t.Object({
      running: t.Boolean(),
      pid: t.Nullable(t.Number()),
      rssMB: t.Nullable(t.Number()),
      cpuPercent: t.Nullable(t.Number()),
      url: t.Nullable(t.String()),
    }),
    codexAppServer: t.Object({
      running: t.Boolean(),
      pid: t.Nullable(t.Number()),
      rssMB: t.Nullable(t.Number()),
      cpuPercent: t.Nullable(t.Number()),
    }),
    desktop: t.Object({
      latestSamples: t.Array(t.Object({
        source: t.Literal('desktop-main'),
        sampledAt: t.Number(),
        main: t.Record(t.String(), t.Unknown()),
        appMetrics: t.Array(t.Record(t.String(), t.Unknown())),
        windows: t.Array(t.Record(t.String(), t.Unknown())),
        diagnostics: t.Optional(t.Record(t.String(), t.Unknown())),
      })),
    }),
    drilldowns: t.Object({
      renderer: t.Object({
        rendererWindows: t.Array(t.Record(t.String(), t.Unknown())),
        topChatSessions: t.Array(t.Record(t.String(), t.Unknown())),
        activeStreamingMessages: t.Array(t.Record(t.String(), t.Unknown())),
        runDisplayMetaMessages: t.Array(t.Record(t.String(), t.Unknown())),
      }),
      browserPanel: t.Object({
        panel: t.Nullable(t.Record(t.String(), t.Unknown())),
        limits: t.Nullable(t.Record(t.String(), t.Unknown())),
        activeThreads: t.Array(t.Record(t.String(), t.Unknown())),
        liveTabs: t.Array(t.Record(t.String(), t.Unknown())),
        runtimes: t.Array(t.Record(t.String(), t.Unknown())),
      }),
      runStreams: t.Object({
        topRuns: t.Array(t.Record(t.String(), t.Unknown())),
      }),
      providerRuntime: t.Object({
        topHosts: t.Array(t.Record(t.String(), t.Unknown())),
      }),
    }),
    observability: t.Object({
      queueDepth: t.Number(),
      recentEvents: t.Number(),
      droppedEvents: t.Number(),
      pendingFlush: t.Boolean(),
    }),
  }),
}
