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

import { APIResource } from '../../core/resource';
import * as HyperdriveAPI from './hyperdrive';
import { HyperdrivesSinglePage } from './hyperdrive';
import { APIPromise } from '../../core/api-promise';
import { PagePromise, SinglePage } from '../../core/pagination';
import { RequestOptions } from '../../internal/request-options';
import { path } from '../../internal/utils/path';

export class BaseConfigs extends APIResource {
  static override readonly _key: readonly ['hyperdrive', 'configs'] = Object.freeze([
    'hyperdrive',
    'configs',
  ] as const);

  /**
   * Creates and returns a new Hyperdrive configuration.
   *
   * @example
   * ```ts
   * const hyperdrive = await client.hyperdrive.configs.create({
   *   account_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *   name: 'example-hyperdrive',
   *   origin: {
   *     database: 'postgres',
   *     host: 'database.example.com',
   *     password: 'password',
   *     port: 5432,
   *     scheme: 'postgres',
   *     user: 'postgres',
   *   },
   * });
   * ```
   */
  create(params: ConfigCreateParams, options?: RequestOptions): APIPromise<HyperdriveAPI.Hyperdrive> {
    const { account_id, ...body } = params;
    return (
      this._client.post(path`/accounts/${account_id}/hyperdrive/configs`, {
        body,
        ...options,
      }) as APIPromise<{ result: HyperdriveAPI.Hyperdrive }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Updates and returns the specified Hyperdrive configuration.
   *
   * @example
   * ```ts
   * const hyperdrive = await client.hyperdrive.configs.update(
   *   '023e105f4ecef8ad9ca31a8372d0c353',
   *   {
   *     account_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *     name: 'example-hyperdrive',
   *     origin: {
   *       database: 'postgres',
   *       host: 'database.example.com',
   *       password: 'password',
   *       port: 5432,
   *       scheme: 'postgres',
   *       user: 'postgres',
   *     },
   *   },
   * );
   * ```
   */
  update(
    hyperdriveID: string,
    params: ConfigUpdateParams,
    options?: RequestOptions,
  ): APIPromise<HyperdriveAPI.Hyperdrive> {
    const { account_id, ...body } = params;
    return (
      this._client.put(path`/accounts/${account_id}/hyperdrive/configs/${hyperdriveID}`, {
        body,
        ...options,
      }) as APIPromise<{ result: HyperdriveAPI.Hyperdrive }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Returns a list of Hyperdrives.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const hyperdrive of client.hyperdrive.configs.list(
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * )) {
   *   // ...
   * }
   * ```
   */
  list(
    params: ConfigListParams,
    options?: RequestOptions,
  ): PagePromise<HyperdrivesSinglePage, HyperdriveAPI.Hyperdrive> {
    const { account_id } = params;
    return this._client.getAPIList(
      path`/accounts/${account_id}/hyperdrive/configs`,
      SinglePage<HyperdriveAPI.Hyperdrive>,
      options,
    );
  }

  /**
   * Deletes the specified Hyperdrive.
   *
   * @example
   * ```ts
   * const config = await client.hyperdrive.configs.delete(
   *   '023e105f4ecef8ad9ca31a8372d0c353',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  delete(
    hyperdriveID: string,
    params: ConfigDeleteParams,
    options?: RequestOptions,
  ): APIPromise<ConfigDeleteResponse | null> {
    const { account_id } = params;
    return (
      this._client.delete(
        path`/accounts/${account_id}/hyperdrive/configs/${hyperdriveID}`,
        options,
      ) as APIPromise<{ result: ConfigDeleteResponse | null }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Patches and returns the specified Hyperdrive configuration. Custom caching
   * settings are not kept if caching is disabled.
   *
   * @example
   * ```ts
   * const hyperdrive = await client.hyperdrive.configs.edit(
   *   '023e105f4ecef8ad9ca31a8372d0c353',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  edit(
    hyperdriveID: string,
    params: ConfigEditParams,
    options?: RequestOptions,
  ): APIPromise<HyperdriveAPI.Hyperdrive> {
    const { account_id, ...body } = params;
    return (
      this._client.patch(path`/accounts/${account_id}/hyperdrive/configs/${hyperdriveID}`, {
        body,
        ...options,
      }) as APIPromise<{ result: HyperdriveAPI.Hyperdrive }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Returns the specified Hyperdrive configuration.
   *
   * @example
   * ```ts
   * const hyperdrive = await client.hyperdrive.configs.get(
   *   '023e105f4ecef8ad9ca31a8372d0c353',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  get(
    hyperdriveID: string,
    params: ConfigGetParams,
    options?: RequestOptions,
  ): APIPromise<HyperdriveAPI.Hyperdrive> {
    const { account_id } = params;
    return (
      this._client.get(
        path`/accounts/${account_id}/hyperdrive/configs/${hyperdriveID}`,
        options,
      ) as APIPromise<{ result: HyperdriveAPI.Hyperdrive }>
    )._thenUnwrap((obj) => obj.result);
  }
}
export class Configs extends BaseConfigs {}

export type ConfigDeleteResponse = unknown;

export interface ConfigCreateParams {
  /**
   * Path param: Define configurations using a unique string identifier.
   */
  account_id: string;

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

  /**
   * Body param
   */
  origin:
    | ConfigCreateParams.PublicDatabase
    | ConfigCreateParams.AccessProtectedDatabaseBehindCloudflareTunnel
    | ConfigCreateParams.DatabaseReachableThroughAWorkersVPC;

  /**
   * Body param
   */
  caching?:
    | ConfigCreateParams.HyperdriveHyperdriveCachingCommon
    | ConfigCreateParams.HyperdriveHyperdriveCachingEnabled;

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

  /**
   * Body param: 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 ConfigCreateParams {
  export interface PublicDatabase {
    /**
     * Set the name of your origin database.
     */
    database: string;

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

    /**
     * Set the password needed to access your origin database. The API never returns
     * this write-only value.
     */
    password: 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;

    /**
     * Defines the Client Secret of the Access Token to use when connecting to the
     * origin database. The API never returns this write-only value.
     */
    access_client_secret: string;

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

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

    /**
     * Set the password needed to access your origin database. The API never returns
     * this write-only value.
     */
    password: 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;

    /**
     * Set the password needed to access your origin database. The API never returns
     * this write-only value.
     */
    password: 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;
  }
}

export interface ConfigUpdateParams {
  /**
   * Path param: Define configurations using a unique string identifier.
   */
  account_id: string;

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

  /**
   * Body param
   */
  origin:
    | ConfigUpdateParams.PublicDatabase
    | ConfigUpdateParams.AccessProtectedDatabaseBehindCloudflareTunnel
    | ConfigUpdateParams.DatabaseReachableThroughAWorkersVPC;

  /**
   * Body param
   */
  caching?:
    | ConfigUpdateParams.HyperdriveHyperdriveCachingCommon
    | ConfigUpdateParams.HyperdriveHyperdriveCachingEnabled;

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

  /**
   * Body param: 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 ConfigUpdateParams {
  export interface PublicDatabase {
    /**
     * Set the name of your origin database.
     */
    database: string;

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

    /**
     * Set the password needed to access your origin database. The API never returns
     * this write-only value.
     */
    password: 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;

    /**
     * Defines the Client Secret of the Access Token to use when connecting to the
     * origin database. The API never returns this write-only value.
     */
    access_client_secret: string;

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

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

    /**
     * Set the password needed to access your origin database. The API never returns
     * this write-only value.
     */
    password: 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;

    /**
     * Set the password needed to access your origin database. The API never returns
     * this write-only value.
     */
    password: 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;
  }
}

export interface ConfigListParams {
  /**
   * Define configurations using a unique string identifier.
   */
  account_id: string;
}

export interface ConfigDeleteParams {
  /**
   * Define configurations using a unique string identifier.
   */
  account_id: string;
}

export interface ConfigEditParams {
  /**
   * Path param: Define configurations using a unique string identifier.
   */
  account_id: string;

  /**
   * Body param
   */
  caching?:
    | ConfigEditParams.HyperdriveHyperdriveCachingCommon
    | ConfigEditParams.HyperdriveHyperdriveCachingEnabled;

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

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

  /**
   * Body param: 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.
   */
  origin?:
    | ConfigEditParams.HyperdriveHyperdriveDatabase
    | ConfigEditParams.HyperdriveInternetOrigin
    | ConfigEditParams.HyperdriveOverAccessOrigin
    | ConfigEditParams.HyperdriveVPCServiceOrigin;

  /**
   * Body param: 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 ConfigEditParams {
  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;
  }

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

    /**
     * Set the password needed to access your origin database. The API never returns
     * this write-only value.
     */
    password?: 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 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;
  }

  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 Client Secret of the Access Token to use when connecting to the
     * origin database. The API never returns this write-only value.
     */
    access_client_secret: string;

    /**
     * Defines the host (hostname or IP) of your origin database.
     */
    host: 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;
  }
}

export interface ConfigGetParams {
  /**
   * Define configurations using a unique string identifier.
   */
  account_id: string;
}

export declare namespace Configs {
  export {
    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,
  };
}

export { type HyperdrivesSinglePage };
