// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.

import { APIResource } from '../../resource';
import * as ConfigsAPI from './configs';
import {
  ConfigCreateParams,
  ConfigDeleteParams,
  ConfigDeleteResponse,
  ConfigEditParams,
  ConfigGetParams,
  ConfigListParams,
  ConfigUpdateParams,
  Configs,
} from './configs';
import { SinglePage } from '../../pagination';

export class HyperdriveResource extends APIResource {
  configs: ConfigsAPI.Configs = new ConfigsAPI.Configs(this._client);
}

export class HyperdrivesSinglePage extends SinglePage<Hyperdrive> {}

/**
 * Connect to a database through a Workers VPC Service. TLS settings (mTLS,
 * sslmode) cannot be configured on the Hyperdrive when using a VPC Service origin;
 * TLS must be managed on the VPC Service itself.
 */
export type Configuration =
  | Configuration.HyperdriveInternetOrigin
  | Configuration.HyperdriveOverAccessOrigin
  | Configuration.HyperdriveVPCServiceOrigin;

export namespace Configuration {
  export interface HyperdriveInternetOrigin {
    /**
     * Defines the host (hostname or IP) of your origin database.
     */
    host: string;

    /**
     * Defines the port of your origin database. Defaults to 5432 for PostgreSQL or
     * 3306 for MySQL if not specified.
     */
    port: number;

    /**
     * Set the name of your origin database.
     */
    database?: string;

    /**
     * Specifies the URL scheme used to connect to your origin database.
     */
    scheme?: 'postgres' | 'postgresql' | 'mysql';

    /**
     * Set the user of your origin database.
     */
    user?: string;
  }

  export interface HyperdriveOverAccessOrigin {
    /**
     * Defines the Client ID of the Access token to use when connecting to the origin
     * database.
     */
    access_client_id: string;

    /**
     * Defines the host (hostname or IP) of your origin database.
     */
    host: string;

    /**
     * Set the name of your origin database.
     */
    database?: string;

    /**
     * Specifies the URL scheme used to connect to your origin database.
     */
    scheme?: 'postgres' | 'postgresql' | 'mysql';

    /**
     * Set the user of your origin database.
     */
    user?: string;
  }

  /**
   * Connect to a database through a Workers VPC Service. TLS settings (mTLS,
   * sslmode) cannot be configured on the Hyperdrive when using a VPC Service origin;
   * TLS must be managed on the VPC Service itself.
   */
  export interface HyperdriveVPCServiceOrigin {
    /**
     * The identifier of the Workers VPC Service to connect through. Hyperdrive will
     * egress through the specified VPC Service to reach the origin database.
     */
    service_id: string;

    /**
     * Set the name of your origin database.
     */
    database?: string;

    /**
     * Specifies the URL scheme used to connect to your origin database.
     */
    scheme?: 'postgres' | 'postgresql' | 'mysql';

    /**
     * Set the user of your origin database.
     */
    user?: string;
  }
}

export interface Hyperdrive {
  /**
   * Define configurations using a unique string identifier.
   */
  id: string;

  /**
   * The name of the Hyperdrive configuration. Used to identify the configuration in
   * the Cloudflare dashboard and API.
   */
  name: string;

  origin:
    | Hyperdrive.PublicDatabase
    | Hyperdrive.AccessProtectedDatabaseBehindCloudflareTunnel
    | Hyperdrive.DatabaseReachableThroughAWorkersVPC;

  caching?: Hyperdrive.HyperdriveHyperdriveCachingCommon | Hyperdrive.HyperdriveHyperdriveCachingEnabled;

  /**
   * Defines the creation time of the Hyperdrive configuration.
   */
  created_on?: string;

  /**
   * Defines the last modified time of the Hyperdrive configuration.
   */
  modified_on?: string;

  /**
   * mTLS configuration for the origin connection. Cannot be used with VPC Service
   * origins; TLS must be managed on the VPC Service.
   */
  mtls?: Hyperdrive.MTLS;

  /**
   * The (soft) maximum number of connections the Hyperdrive is allowed to make to
   * the origin database.
   *
   * Maximum allowed: 20 for free tier accounts, 100 for paid tier accounts. If not
   * specified, defaults to 20 for free tier and 60 for paid tier. Contact Cloudflare
   * if you need a higher limit.
   */
  origin_connection_limit?: number;
}

export namespace Hyperdrive {
  export interface PublicDatabase {
    /**
     * Set the name of your origin database.
     */
    database: string;

    /**
     * Defines the host (hostname or IP) of your origin database.
     */
    host: string;

    /**
     * Defines the port of your origin database. Defaults to 5432 for PostgreSQL or
     * 3306 for MySQL if not specified.
     */
    port: number;

    /**
     * Specifies the URL scheme used to connect to your origin database.
     */
    scheme: 'postgres' | 'postgresql' | 'mysql';

    /**
     * Set the user of your origin database.
     */
    user: string;
  }

  export interface AccessProtectedDatabaseBehindCloudflareTunnel {
    /**
     * Defines the Client ID of the Access token to use when connecting to the origin
     * database.
     */
    access_client_id: string;

    /**
     * Set the name of your origin database.
     */
    database: string;

    /**
     * Defines the host (hostname or IP) of your origin database.
     */
    host: string;

    /**
     * Specifies the URL scheme used to connect to your origin database.
     */
    scheme: 'postgres' | 'postgresql' | 'mysql';

    /**
     * Set the user of your origin database.
     */
    user: string;
  }

  export interface DatabaseReachableThroughAWorkersVPC {
    /**
     * Set the name of your origin database.
     */
    database: string;

    /**
     * Specifies the URL scheme used to connect to your origin database.
     */
    scheme: 'postgres' | 'postgresql' | 'mysql';

    /**
     * The identifier of the Workers VPC Service to connect through. Hyperdrive will
     * egress through the specified VPC Service to reach the origin database.
     */
    service_id: string;

    /**
     * Set the user of your origin database.
     */
    user: string;
  }

  export interface HyperdriveHyperdriveCachingCommon {
    /**
     * Set to true to disable caching of SQL responses. Default is false.
     */
    disabled?: boolean;
  }

  export interface HyperdriveHyperdriveCachingEnabled {
    /**
     * Set to true to disable caching of SQL responses. Default is false.
     */
    disabled?: boolean;

    /**
     * Specify the maximum duration (in seconds) items should persist in the cache.
     * Defaults to 60 seconds if not specified.
     */
    max_age?: number;

    /**
     * Specify the number of seconds the cache may serve a stale response. Defaults to
     * 15 seconds if not specified.
     */
    stale_while_revalidate?: number;
  }

  /**
   * mTLS configuration for the origin connection. Cannot be used with VPC Service
   * origins; TLS must be managed on the VPC Service.
   */
  export interface MTLS {
    /**
     * Define CA certificate ID obtained after uploading CA cert.
     */
    ca_certificate_id?: string;

    /**
     * Define mTLS certificate ID obtained after uploading client cert.
     */
    mtls_certificate_id?: string;

    /**
     * Set SSL mode to 'require', 'verify-ca', or 'verify-full' to verify the CA.
     */
    sslmode?: string;
  }
}

HyperdriveResource.Configs = Configs;

export declare namespace HyperdriveResource {
  export { type Configuration as Configuration, type Hyperdrive as Hyperdrive };

  export {
    Configs as Configs,
    type ConfigDeleteResponse as ConfigDeleteResponse,
    type ConfigCreateParams as ConfigCreateParams,
    type ConfigUpdateParams as ConfigUpdateParams,
    type ConfigListParams as ConfigListParams,
    type ConfigDeleteParams as ConfigDeleteParams,
    type ConfigEditParams as ConfigEditParams,
    type ConfigGetParams as ConfigGetParams,
  };
}
