import type {
  DurableGatewayJournal,
  GatewayJournalBeginResult,
  GatewayJournalCompareAndSetInput,
  GatewayJournalRecord,
  GatewayJournalState,
} from "./gateway.js";

export type PostgresGatewayJournalState = GatewayJournalState;
export type PostgresGatewayJournalRecord = GatewayJournalRecord;

export interface PostgresQueryResult<
  Row extends Record<string, unknown> = Record<string, unknown>,
> {
  readonly rows: Row[];
  readonly rowCount?: number | null;
}

/**
 * Structural query interface compatible with a node-postgres Pool or Client.
 * The implementation does not import or install a PostgreSQL driver.
 */
export interface PostgresQueryable {
  query<
    Row extends Record<string, unknown> = Record<string, unknown>,
  >(
    text: string,
    values?: unknown[],
  ): Promise<PostgresQueryResult<Row>>;
}

export interface PostgresGatewayJournalOptions {
  /**
   * A query endpoint that always reaches the same writable PostgreSQL primary.
   * Do not route get() calls to replicas.
   */
  database: PostgresQueryable;
  /**
   * Stable deployment namespace. It prevents accidental key collisions but is
   * not a database authorization boundary.
   */
  namespace: string;
}

export interface PostgresGatewayJournalReadiness {
  readonly ready: true;
  readonly implementation_version: typeof POSTGRES_GATEWAY_JOURNAL_VERSION;
  readonly schema_version: 1;
  readonly relation: "mandateshield_gate.gateway_journal_v1";
  readonly server_version_num: number;
  readonly primary: true;
  readonly transaction_isolation: "read committed";
  readonly default_transaction_read_only: "off";
  readonly transaction_read_only: "off";
  readonly synchronous_commit:
    | "on"
    | "remote_write"
    | "remote_apply";
  /**
   * Always false: the inspection cannot prove that every executor shares this
   * endpoint or that the customer's failover topology is linearizable.
   */
  readonly topology_attested: false;
}

export type PostgresGatewayJournalBeginResult =
  GatewayJournalBeginResult;

export type PostgresGatewayJournalCompareAndSetInput =
  GatewayJournalCompareAndSetInput;

export interface PostgresGatewayJournal
  extends DurableGatewayJournal {
  readonly implementationVersion:
    typeof POSTGRES_GATEWAY_JOURNAL_VERSION;
  readonly namespace: string;
  checkReadiness(): Promise<PostgresGatewayJournalReadiness>;
}

export interface PostgresGatewayJournalErrorOptions {
  code?: string;
  operation?: string | null;
  cause?: unknown;
}

export declare class MandateShieldPostgresGatewayJournalError
  extends Error {
  constructor(
    message: string,
    options?: PostgresGatewayJournalErrorOptions,
  );
  readonly code: string;
  readonly operation: string | null;
}

export declare const POSTGRES_GATEWAY_JOURNAL_VERSION: "1.0.0";
export declare const POSTGRES_GATEWAY_JOURNAL_SCHEMA:
  "mandateshield_gate";
export declare const POSTGRES_GATEWAY_JOURNAL_TABLE:
  "gateway_journal_v1";

/**
 * Creates the first-party PostgreSQL implementation of the structural
 * DurableGatewayJournal contract.
 *
 * The module performs no migration, retry, deletion, logging, or topology
 * attestation. Install the versioned SQL separately and call checkReadiness()
 * before constructing a production Gate. Readiness fails closed unless every
 * required schema constraint has the expected kind, validated state, and
 * normalized PostgreSQL definition.
 */
export declare function createPostgresGatewayJournal(
  options: PostgresGatewayJournalOptions,
): PostgresGatewayJournal;
