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

import { APIResource } from '../../../../core/resource';
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 BaseConnections extends APIResource {
  static override readonly _key: readonly ['zeroTrust', 'tunnels', 'cloudflared', 'connections'] =
    Object.freeze(['zeroTrust', 'tunnels', 'cloudflared', 'connections'] as const);

  /**
   * Removes a connection (aka Cloudflare Tunnel Connector) from a Cloudflare Tunnel
   * independently of its current state. If no connector id (client_id) is provided
   * all connectors will be removed. We recommend running this command after rotating
   * tokens.
   *
   * @example
   * ```ts
   * const connection =
   *   await client.zeroTrust.tunnels.cloudflared.connections.delete(
   *     'f70ff985-a4ef-4643-bbbc-4a0ed4fc8415',
   *     { account_id: '699d98642c564d2e855e9661899b7252' },
   *   );
   * ```
   */
  delete(
    tunnelID: string,
    params: ConnectionDeleteParams,
    options?: RequestOptions,
  ): APIPromise<ConnectionDeleteResponse | null> {
    const { account_id, client_id } = params;
    return (
      this._client.delete(path`/accounts/${account_id}/cfd_tunnel/${tunnelID}/connections`, {
        query: { client_id },
        ...options,
      }) as APIPromise<{ result: ConnectionDeleteResponse | null }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Lists the connections for a Cloudflare Tunnel, including connector IDs,
   * cloudflared versions, and Cloudflare locations.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const client of client.zeroTrust.tunnels.cloudflared.connections.get(
   *   'f70ff985-a4ef-4643-bbbc-4a0ed4fc8415',
   *   { account_id: '699d98642c564d2e855e9661899b7252' },
   * )) {
   *   // ...
   * }
   * ```
   */
  get(
    tunnelID: string,
    params: ConnectionGetParams,
    options?: RequestOptions,
  ): PagePromise<ClientsSinglePage, Client> {
    const { account_id } = params;
    return this._client.getAPIList(
      path`/accounts/${account_id}/cfd_tunnel/${tunnelID}/connections`,
      SinglePage<Client>,
      options,
    );
  }
}
export class Connections extends BaseConnections {}

export type ClientsSinglePage = SinglePage<Client>;

/**
 * A client (typically cloudflared) that maintains connections to a Cloudflare data
 * center.
 */
export interface Client {
  /**
   * UUID of the Cloudflare Tunnel connection.
   */
  id?: string;

  /**
   * The cloudflared OS architecture used to establish this connection.
   */
  arch?: string;

  /**
   * The version of the remote tunnel configuration. Used internally to sync
   * cloudflared with the Zero Trust dashboard.
   */
  config_version?: number;

  /**
   * The Cloudflare Tunnel connections between your origin and Cloudflare's edge.
   */
  conns?: Array<Client.Conn>;

  /**
   * Features enabled for the Cloudflare Tunnel.
   */
  features?: Array<string>;

  /**
   * Timestamp of when the tunnel connection was started.
   */
  run_at?: string;

  /**
   * The cloudflared version used to establish this connection.
   */
  version?: string;
}

export namespace Client {
  export interface Conn {
    /**
     * UUID of the Cloudflare Tunnel connection.
     */
    id?: string;

    /**
     * UUID of the Cloudflare Tunnel connector.
     */
    client_id?: string;

    /**
     * The cloudflared version used to establish this connection.
     */
    client_version?: string;

    /**
     * The Cloudflare data center used for this connection.
     */
    colo_name?: string;

    /**
     * @deprecated This functionality has been removed. The is_pending_reconnect field
     * will now always report false.
     */
    is_pending_reconnect?: boolean;

    /**
     * Timestamp of when the connection was established.
     */
    opened_at?: string;

    /**
     * The public IP address of the host running cloudflared.
     */
    origin_ip?: string;

    /**
     * UUID of the Cloudflare Tunnel connection.
     */
    uuid?: string;
  }
}

export type ConnectionDeleteResponse = unknown;

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

  /**
   * Query param: UUID of the Cloudflare Tunnel connector.
   */
  client_id?: string;
}

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

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