/**
 * Runtime 存储层的共享命名常量与约束表达式。
 *
 * 三方言语义文件（sqlite.ts / postgres.ts / mysql.ts）各自手写 Drizzle table 定义，
 * 以保留列级类型安全；本文件集中跨方言一致的字符串（表名、索引名、CHECK 表达式），
 * 避免三份实现之间命名漂移。列定义不在此描述——直接在各方言 schema 里写，
 * Drizzle 的类型推导与 typecheck 会保证一致性。
 */

export const RUNTIME_TABLE_NAMES = {
  connections: "connections",
  oauthClientConfigs: "oauth_client_configs",
  oauthStates: "oauth_states",
  runtimeTokens: "runtime_tokens",
  runs: "runs",
  idempotencyRecords: "idempotency_records",
} as const;

/**
 * runs 表的 5 个二级索引 + idempotency_records 的过期索引。名字与历史
 * migrations/0001~0006.sql 保持一致，便于旧版 connect.sqlite 的无缝采纳。
 */
export const RUNTIME_INDEX_NAMES = {
  runsServiceStartedAtId: "runs_service_started_at_id_idx",
  runsActionIdStartedAtId: "runs_action_id_started_at_id_idx",
  runsCallerStartedAtId: "runs_caller_started_at_id_idx",
  runsOkStartedAtId: "runs_ok_started_at_id_idx",
  runsStartedAtId: "runs_started_at_id_idx",
  idempotencyRecordsExpiresAt: "idempotency_records_expires_at_idx",
} as const;

/**
 * idempotency_records 的两个 CHECK 约束（三方言通用 SQL 语法，标准 check 谓词）。
 */
export const IDEMPOTENCY_STATE_CHECK = "state in ('in_progress', 'completed')";

export const IDEMPOTENCY_STATE_RESPONSE_CHECK =
  "(state = 'in_progress' and response_value is null) or (state = 'completed' and response_value is not null)";
