// @ts-nocheck
/**
 * Drizzle 声明式 schema 与项目 src/tsconfig.json 的 `isolatedDeclarations` 风格不兼容。
 * 本文件由独立的 `src/server/storage/drizzle/schema/tsconfig.json` 管理；运行时类型安全由
 * dialect/store 层保证。
 */
import { sql } from "drizzle-orm";
import { boolean, check, index, pgTable, primaryKey, text } from "drizzle-orm/pg-core";
import { IDEMPOTENCY_STATE_CHECK, IDEMPOTENCY_STATE_RESPONSE_CHECK, RUNTIME_INDEX_NAMES } from "./columns.ts";

/**
 * Runtime storage schema for the PostgreSQL dialect (self-hosted Node backend).
 *
 * Wire format matches the SQLite schema byte-for-byte in JS terms: all `value` columns
 * are text (not jsonb) so dialect behavior is identical and ciphertext round-trips
 * unchanged; uuid primary keys are text; runs.ok is a native boolean.
 */

export const connections: any = pgTable(
  "connections",
  {
    id: text("id").notNull().unique(),
    service: text("service").notNull(),
    connectionName: text("connection_name").notNull(),
    value: text("value").notNull(),
    updatedAt: text("updated_at").notNull(),
  },
  (table) => [primaryKey({ columns: [table.service, table.connectionName] })],
);

export const oauthClientConfigs: any = pgTable("oauth_client_configs", {
  service: text("service").primaryKey(),
  value: text("value").notNull(),
  updatedAt: text("updated_at").notNull(),
});

export const oauthStates: any = pgTable("oauth_states", {
  state: text("state").primaryKey(),
  value: text("value").notNull(),
  createdAt: text("created_at").notNull(),
});

export const runtimeTokens: any = pgTable("runtime_tokens", {
  id: text("id").primaryKey(),
  name: text("name").notNull(),
  tokenHash: text("token_hash").notNull().unique(),
  createdAt: text("created_at").notNull(),
  lastUsedAt: text("last_used_at"),
  revokedAt: text("revoked_at"),
});

export const runs: any = pgTable(
  "runs",
  {
    id: text("id").primaryKey(),
    actionId: text("action_id").notNull(),
    startedAt: text("started_at").notNull(),
    completedAt: text("completed_at").notNull(),
    ok: boolean("ok").notNull(),
    value: text("value").notNull(),
    service: text("service"),
    caller: text("caller"),
  },
  (table) => [
    index(RUNTIME_INDEX_NAMES.runsServiceStartedAtId).on(
      table.service,
      sql`${table.startedAt} desc`,
      sql`${table.id} desc`,
    ),
    index(RUNTIME_INDEX_NAMES.runsActionIdStartedAtId).on(
      table.actionId,
      sql`${table.startedAt} desc`,
      sql`${table.id} desc`,
    ),
    index(RUNTIME_INDEX_NAMES.runsCallerStartedAtId).on(
      table.caller,
      sql`${table.startedAt} desc`,
      sql`${table.id} desc`,
    ),
    index(RUNTIME_INDEX_NAMES.runsOkStartedAtId).on(table.ok, sql`${table.startedAt} desc`, sql`${table.id} desc`),
    index(RUNTIME_INDEX_NAMES.runsStartedAtId).on(sql`${table.startedAt} desc`, sql`${table.id} desc`),
  ],
);

export const idempotencyRecords: any = pgTable(
  "idempotency_records",
  {
    keyHash: text("key_hash").primaryKey(),
    claimId: text("claim_id").notNull(),
    requestHash: text("request_hash").notNull(),
    state: text("state").notNull(),
    responseValue: text("response_value"),
    createdAt: text("created_at").notNull(),
    expiresAt: text("expires_at").notNull(),
  },
  (table) => [
    index(RUNTIME_INDEX_NAMES.idempotencyRecordsExpiresAt).on(table.expiresAt),
    check("idempotency_state_check", sql.raw(IDEMPOTENCY_STATE_CHECK)),
    check("idempotency_state_response_check", sql.raw(IDEMPOTENCY_STATE_RESPONSE_CHECK)),
  ],
);

export const postgresRuntimeSchema: any = {
  connections,
  oauthClientConfigs,
  oauthStates,
  runtimeTokens,
  runs,
  idempotencyRecords,
};

export type PostgresRuntimeSchema = any;
