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

import { APIResource } from '../../../../core/resource';
import * as Shared from '../../../shared';
import { CloudflareTunnelsV4PagePaginationArray } from '../../../shared';
import * as ConfigurationsAPI from './configurations';
import {
  BaseConfigurations,
  ConfigurationGetParams,
  ConfigurationGetResponse,
  ConfigurationUpdateParams,
  ConfigurationUpdateResponse,
  Configurations,
} from './configurations';
import * as ConnectionsAPI from './connections';
import {
  BaseConnections,
  Client,
  ClientsSinglePage,
  ConnectionDeleteParams,
  ConnectionDeleteResponse,
  ConnectionGetParams,
  Connections,
} from './connections';
import * as ConnectorsAPI from './connectors';
import { BaseConnectors, ConnectorGetParams, Connectors } from './connectors';
import * as ManagementAPI from './management';
import { BaseManagement, Management, ManagementCreateParams, ManagementCreateResponse } from './management';
import * as TokenAPI from './token';
import { BaseToken, Token, TokenGetParams, TokenGetResponse } from './token';
import { APIPromise } from '../../../../core/api-promise';
import {
  PagePromise,
  V4PagePaginationArray,
  type V4PagePaginationArrayParams,
} from '../../../../core/pagination';
import { RequestOptions } from '../../../../internal/request-options';
import { path } from '../../../../internal/utils/path';

export class BaseCloudflared extends APIResource {
  static override readonly _key: readonly ['zeroTrust', 'tunnels', 'cloudflared'] = Object.freeze([
    'zeroTrust',
    'tunnels',
    'cloudflared',
  ] as const);

  /**
   * Creates a remotely or locally managed Cloudflare Tunnel in an account. After
   * creation, retrieve its token and run cloudflared to establish the connector
   * connection.
   *
   * @example
   * ```ts
   * const cloudflareTunnel =
   *   await client.zeroTrust.tunnels.cloudflared.create({
   *     account_id: '699d98642c564d2e855e9661899b7252',
   *     name: 'blog',
   *   });
   * ```
   */
  create(params: CloudflaredCreateParams, options?: RequestOptions): APIPromise<Shared.CloudflareTunnel> {
    const { account_id, ...body } = params;
    return (
      this._client.post(path`/accounts/${account_id}/cfd_tunnel`, { body, ...options }) as APIPromise<{
        result: Shared.CloudflareTunnel;
      }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Lists and filters Cloudflare Tunnels in an account.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const cloudflareTunnel of client.zeroTrust.tunnels.cloudflared.list(
   *   { account_id: '699d98642c564d2e855e9661899b7252' },
   * )) {
   *   // ...
   * }
   * ```
   */
  list(
    params: CloudflaredListParams,
    options?: RequestOptions,
  ): PagePromise<CloudflareTunnelsV4PagePaginationArray, Shared.CloudflareTunnel> {
    const { account_id, ...query } = params;
    return this._client.getAPIList(
      path`/accounts/${account_id}/cfd_tunnel`,
      V4PagePaginationArray<Shared.CloudflareTunnel>,
      { query, ...options },
    );
  }

  /**
   * Permanently deletes a Cloudflare Tunnel from an account. The tunnel must have no
   * active connections.
   *
   * @example
   * ```ts
   * const cloudflareTunnel =
   *   await client.zeroTrust.tunnels.cloudflared.delete(
   *     'f70ff985-a4ef-4643-bbbc-4a0ed4fc8415',
   *     { account_id: '699d98642c564d2e855e9661899b7252' },
   *   );
   * ```
   */
  delete(
    tunnelID: string,
    params: CloudflaredDeleteParams,
    options?: RequestOptions,
  ): APIPromise<Shared.CloudflareTunnel> {
    const { account_id } = params;
    return (
      this._client.delete(path`/accounts/${account_id}/cfd_tunnel/${tunnelID}`, options) as APIPromise<{
        result: Shared.CloudflareTunnel;
      }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Updates the name or secret of an existing Cloudflare Tunnel.
   *
   * @example
   * ```ts
   * const cloudflareTunnel =
   *   await client.zeroTrust.tunnels.cloudflared.edit(
   *     'f70ff985-a4ef-4643-bbbc-4a0ed4fc8415',
   *     { account_id: '699d98642c564d2e855e9661899b7252' },
   *   );
   * ```
   */
  edit(
    tunnelID: string,
    params: CloudflaredEditParams,
    options?: RequestOptions,
  ): APIPromise<Shared.CloudflareTunnel> {
    const { account_id, ...body } = params;
    return (
      this._client.patch(path`/accounts/${account_id}/cfd_tunnel/${tunnelID}`, {
        body,
        ...options,
      }) as APIPromise<{ result: Shared.CloudflareTunnel }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Fetches a single Cloudflare Tunnel.
   *
   * @example
   * ```ts
   * const cloudflareTunnel =
   *   await client.zeroTrust.tunnels.cloudflared.get(
   *     'f70ff985-a4ef-4643-bbbc-4a0ed4fc8415',
   *     { account_id: '699d98642c564d2e855e9661899b7252' },
   *   );
   * ```
   */
  get(
    tunnelID: string,
    params: CloudflaredGetParams,
    options?: RequestOptions,
  ): APIPromise<Shared.CloudflareTunnel> {
    const { account_id } = params;
    return (
      this._client.get(path`/accounts/${account_id}/cfd_tunnel/${tunnelID}`, options) as APIPromise<{
        result: Shared.CloudflareTunnel;
      }>
    )._thenUnwrap((obj) => obj.result);
  }
}
export class Cloudflared extends BaseCloudflared {
  configurations: ConfigurationsAPI.Configurations = new ConfigurationsAPI.Configurations(this._client);
  connections: ConnectionsAPI.Connections = new ConnectionsAPI.Connections(this._client);
  token: TokenAPI.Token = new TokenAPI.Token(this._client);
  connectors: ConnectorsAPI.Connectors = new ConnectorsAPI.Connectors(this._client);
  management: ManagementAPI.Management = new ManagementAPI.Management(this._client);
}

export interface CloudflaredCreateParams {
  /**
   * Path param: Cloudflare account ID
   */
  account_id: string;

  /**
   * Body param: A user-friendly name for a tunnel.
   */
  name: string;

  /**
   * Body param: Indicates if this is a locally or remotely configured tunnel. If
   * `local`, manage the tunnel using a YAML file on the origin machine. If
   * `cloudflare`, manage the tunnel on the Zero Trust dashboard.
   */
  config_src?: 'local' | 'cloudflare';

  /**
   * Body param: Sets the password required to run a locally-managed tunnel. Must be
   * at least 32 bytes and encoded as a base64 string.
   */
  tunnel_secret?: string;
}

export interface CloudflaredListParams extends V4PagePaginationArrayParams {
  /**
   * Path param: Cloudflare account ID
   */
  account_id: string;

  /**
   * Query param
   */
  exclude_prefix?: string;

  /**
   * Query param: If provided, include only resources that were created (and not
   * deleted) before this time. URL encoded.
   */
  existed_at?: string;

  /**
   * Query param
   */
  include_prefix?: string;

  /**
   * Query param: If `true`, only include deleted tunnels. If `false`, exclude
   * deleted tunnels. If empty, all tunnels will be included.
   */
  is_deleted?: boolean;

  /**
   * Query param: A user-friendly name for a tunnel.
   */
  name?: string;

  /**
   * Query param: The status of the tunnel. Valid values are `inactive` (tunnel has
   * never been run), `degraded` (tunnel is active and able to serve traffic but in
   * an unhealthy state), `healthy` (tunnel is active and able to serve traffic), or
   * `down` (tunnel can not serve traffic as it has no connections to the Cloudflare
   * Edge).
   */
  status?: 'inactive' | 'degraded' | 'healthy' | 'down';

  /**
   * Query param: UUID of the tunnel.
   */
  uuid?: string;

  /**
   * Query param
   */
  was_active_at?: string;

  /**
   * Query param
   */
  was_inactive_at?: string;
}

export interface CloudflaredDeleteParams {
  /**
   * Cloudflare account ID
   */
  account_id: string;
}

export interface CloudflaredEditParams {
  /**
   * Path param: Cloudflare account ID
   */
  account_id: string;

  /**
   * Body param: A user-friendly name for a tunnel.
   */
  name?: string;

  /**
   * Body param: Sets the password required to run a locally-managed tunnel. Must be
   * at least 32 bytes and encoded as a base64 string.
   */
  tunnel_secret?: string;
}

export interface CloudflaredGetParams {
  /**
   * Cloudflare account ID
   */
  account_id: string;
}

Cloudflared.Configurations = Configurations;
Cloudflared.BaseConfigurations = BaseConfigurations;
Cloudflared.Connections = Connections;
Cloudflared.BaseConnections = BaseConnections;
Cloudflared.Token = Token;
Cloudflared.BaseToken = BaseToken;
Cloudflared.Connectors = Connectors;
Cloudflared.BaseConnectors = BaseConnectors;
Cloudflared.Management = Management;
Cloudflared.BaseManagement = BaseManagement;

export declare namespace Cloudflared {
  export {
    type CloudflaredCreateParams as CloudflaredCreateParams,
    type CloudflaredListParams as CloudflaredListParams,
    type CloudflaredDeleteParams as CloudflaredDeleteParams,
    type CloudflaredEditParams as CloudflaredEditParams,
    type CloudflaredGetParams as CloudflaredGetParams,
  };

  export {
    Configurations as Configurations,
    BaseConfigurations as BaseConfigurations,
    type ConfigurationUpdateResponse as ConfigurationUpdateResponse,
    type ConfigurationGetResponse as ConfigurationGetResponse,
    type ConfigurationUpdateParams as ConfigurationUpdateParams,
    type ConfigurationGetParams as ConfigurationGetParams,
  };

  export {
    Connections as Connections,
    BaseConnections as BaseConnections,
    type Client as Client,
    type ConnectionDeleteResponse as ConnectionDeleteResponse,
    type ClientsSinglePage as ClientsSinglePage,
    type ConnectionDeleteParams as ConnectionDeleteParams,
    type ConnectionGetParams as ConnectionGetParams,
  };

  export {
    Token as Token,
    BaseToken as BaseToken,
    type TokenGetResponse as TokenGetResponse,
    type TokenGetParams as TokenGetParams,
  };

  export {
    Connectors as Connectors,
    BaseConnectors as BaseConnectors,
    type ConnectorGetParams as ConnectorGetParams,
  };

  export {
    Management as Management,
    BaseManagement as BaseManagement,
    type ManagementCreateResponse as ManagementCreateResponse,
    type ManagementCreateParams as ManagementCreateParams,
  };
}

export { type CloudflareTunnelsV4PagePaginationArray };
