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

import { APIResource } from '../../../resource';
import { isRequestOptions } from '../../../core';
import * as Core from '../../../core';
import * as CredentialsAPI from './credentials';
import {
  CredentialUpdateParams,
  CredentialUpdateResponse,
  Credentials as CredentialsAPICredentials,
} from './credentials';
import { V4PagePaginationArray, type V4PagePaginationArrayParams } from '../../../pagination';

export class Configuration extends APIResource {
  credentials: CredentialsAPI.Credentials = new CredentialsAPI.Credentials(this._client);

  /**
   * Create a new Token Validation configuration
   *
   * @example
   * ```ts
   * const tokenConfig =
   *   await client.tokenValidation.configuration.create({
   *     zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *     credentials: {
   *       keys: [
   *         {
   *           alg: 'ES256',
   *           crv: 'P-256',
   *           kid: '38013f13-c266-4eec-a72a-92ec92779f21',
   *           kty: 'EC',
   *           x: 'KN53JRwN3wCjm2o39bvZUX2VdrsHzS8pxOAGjm8m7EQ',
   *           y: 'lnkkzIxaveggz-HFhcMWW15nxvOj0Z_uQsXbpK0GFcY',
   *         },
   *       ],
   *     },
   *     description:
   *       'Long description for Token Validation Configuration',
   *     title: 'Example Token Validation Configuration',
   *     token_sources: [
   *       'http.request.headers["x-auth"][0]',
   *       'http.request.cookies["Authorization"][0]',
   *     ],
   *     token_type: 'JWT',
   *   });
   * ```
   */
  create(params: ConfigurationCreateParams, options?: Core.RequestOptions): Core.APIPromise<TokenConfig> {
    const { zone_id = this._client.zoneId, ...body } = params;
    return (
      this._client.post(`/zones/${zone_id}/token_validation/config`, {
        body,
        ...options,
      }) as Core.APIPromise<{ result: TokenConfig }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Lists all token validation configurations for this zone
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const tokenConfig of client.tokenValidation.configuration.list(
   *   { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * )) {
   *   // ...
   * }
   * ```
   */
  list(
    params?: ConfigurationListParams,
    options?: Core.RequestOptions,
  ): Core.PagePromise<TokenConfigsV4PagePaginationArray, TokenConfig>;
  list(options?: Core.RequestOptions): Core.PagePromise<TokenConfigsV4PagePaginationArray, TokenConfig>;
  list(
    params: ConfigurationListParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.PagePromise<TokenConfigsV4PagePaginationArray, TokenConfig> {
    if (isRequestOptions(params)) {
      return this.list({}, params);
    }
    const { zone_id = this._client.zoneId, ...query } = params;
    return this._client.getAPIList(
      `/zones/${zone_id}/token_validation/config`,
      TokenConfigsV4PagePaginationArray,
      { query, ...options },
    );
  }

  /**
   * Delete Token Configuration
   *
   * @example
   * ```ts
   * const configuration =
   *   await client.tokenValidation.configuration.delete(
   *     '4a7ee8d3-dd63-4ceb-9d5f-c27831854ce7',
   *     { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  delete(
    configId: string,
    params?: ConfigurationDeleteParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<ConfigurationDeleteResponse>;
  delete(configId: string, options?: Core.RequestOptions): Core.APIPromise<ConfigurationDeleteResponse>;
  delete(
    configId: string,
    params: ConfigurationDeleteParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.APIPromise<ConfigurationDeleteResponse> {
    if (isRequestOptions(params)) {
      return this.delete(configId, {}, params);
    }
    const { zone_id = this._client.zoneId } = params;
    return (
      this._client.delete(
        `/zones/${zone_id}/token_validation/config/${configId}`,
        options,
      ) as Core.APIPromise<{ result: ConfigurationDeleteResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Edit fields of an existing Token Configuration
   *
   * @example
   * ```ts
   * const response =
   *   await client.tokenValidation.configuration.edit(
   *     '4a7ee8d3-dd63-4ceb-9d5f-c27831854ce7',
   *     { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  edit(
    configId: string,
    params: ConfigurationEditParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<ConfigurationEditResponse> {
    const { zone_id = this._client.zoneId, ...body } = params;
    return (
      this._client.patch(`/zones/${zone_id}/token_validation/config/${configId}`, {
        body,
        ...options,
      }) as Core.APIPromise<{ result: ConfigurationEditResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Get a single Token Configuration
   *
   * @example
   * ```ts
   * const tokenConfig =
   *   await client.tokenValidation.configuration.get(
   *     '4a7ee8d3-dd63-4ceb-9d5f-c27831854ce7',
   *     { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  get(
    configId: string,
    params?: ConfigurationGetParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<TokenConfig>;
  get(configId: string, options?: Core.RequestOptions): Core.APIPromise<TokenConfig>;
  get(
    configId: string,
    params: ConfigurationGetParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.APIPromise<TokenConfig> {
    if (isRequestOptions(params)) {
      return this.get(configId, {}, params);
    }
    const { zone_id = this._client.zoneId } = params;
    return (
      this._client.get(`/zones/${zone_id}/token_validation/config/${configId}`, options) as Core.APIPromise<{
        result: TokenConfig;
      }>
    )._thenUnwrap((obj) => obj.result);
  }
}

export class TokenConfigsV4PagePaginationArray extends V4PagePaginationArray<TokenConfig> {}

export interface TokenConfig {
  /**
   * UUID.
   */
  id: string;

  created_at: string;

  credentials: TokenConfig.Credentials;

  description: string;

  last_updated: string;

  title: string;

  token_sources: Array<string>;

  token_type: 'JWT';
}

export namespace TokenConfig {
  export interface Credentials {
    keys: Array<
      | Credentials.APIShieldCredentialsJWTKeyRSA
      | Credentials.APIShieldCredentialsJWTKeyEcEs256
      | Credentials.APIShieldCredentialsJWTKeyEcEs384
    >;
  }

  export namespace Credentials {
    /**
     * JSON representation of an RSA key.
     */
    export interface APIShieldCredentialsJWTKeyRSA {
      /**
       * Algorithm
       */
      alg: 'RS256' | 'RS384' | 'RS512' | 'PS256' | 'PS384' | 'PS512';

      /**
       * RSA exponent
       */
      e: string;

      /**
       * Key ID
       */
      kid: string;

      /**
       * Key Type
       */
      kty: 'RSA';

      /**
       * RSA modulus
       */
      n: string;
    }

    /**
     * JSON representation of an ES256 key
     */
    export interface APIShieldCredentialsJWTKeyEcEs256 {
      /**
       * Algorithm
       */
      alg: 'ES256';

      /**
       * Curve
       */
      crv: 'P-256';

      /**
       * Key ID
       */
      kid: string;

      /**
       * Key Type
       */
      kty: 'EC';

      /**
       * X EC coordinate
       */
      x: string;

      /**
       * Y EC coordinate
       */
      y: string;
    }

    /**
     * JSON representation of an ES384 key
     */
    export interface APIShieldCredentialsJWTKeyEcEs384 {
      /**
       * Algorithm
       */
      alg: 'ES384';

      /**
       * Curve
       */
      crv: 'P-384';

      /**
       * Key ID
       */
      kid: string;

      /**
       * Key Type
       */
      kty: 'EC';

      /**
       * X EC coordinate
       */
      x: string;

      /**
       * Y EC coordinate
       */
      y: string;
    }
  }
}

export interface ConfigurationDeleteResponse {
  /**
   * UUID.
   */
  id?: string;
}

export interface ConfigurationEditResponse {
  /**
   * UUID.
   */
  id?: string;

  description?: string;

  title?: string;

  token_sources?: Array<string>;
}

export interface ConfigurationCreateParams {
  /**
   * Path param: Identifier.
   */
  zone_id?: string;

  /**
   * Body param
   */
  credentials: ConfigurationCreateParams.Credentials;

  /**
   * Body param
   */
  description: string;

  /**
   * Body param
   */
  title: string;

  /**
   * Body param
   */
  token_sources: Array<string>;

  /**
   * Body param
   */
  token_type: 'JWT';
}

export namespace ConfigurationCreateParams {
  export interface Credentials {
    keys: Array<
      | Credentials.APIShieldCredentialsJWTKeyRSA
      | Credentials.APIShieldCredentialsJWTKeyEcEs256
      | Credentials.APIShieldCredentialsJWTKeyEcEs384
    >;
  }

  export namespace Credentials {
    /**
     * JSON representation of an RSA key.
     */
    export interface APIShieldCredentialsJWTKeyRSA {
      /**
       * Algorithm
       */
      alg: 'RS256' | 'RS384' | 'RS512' | 'PS256' | 'PS384' | 'PS512';

      /**
       * RSA exponent
       */
      e: string;

      /**
       * Key ID
       */
      kid: string;

      /**
       * Key Type
       */
      kty: 'RSA';

      /**
       * RSA modulus
       */
      n: string;
    }

    /**
     * JSON representation of an ES256 key
     */
    export interface APIShieldCredentialsJWTKeyEcEs256 {
      /**
       * Algorithm
       */
      alg: 'ES256';

      /**
       * Curve
       */
      crv: 'P-256';

      /**
       * Key ID
       */
      kid: string;

      /**
       * Key Type
       */
      kty: 'EC';

      /**
       * X EC coordinate
       */
      x: string;

      /**
       * Y EC coordinate
       */
      y: string;
    }

    /**
     * JSON representation of an ES384 key
     */
    export interface APIShieldCredentialsJWTKeyEcEs384 {
      /**
       * Algorithm
       */
      alg: 'ES384';

      /**
       * Curve
       */
      crv: 'P-384';

      /**
       * Key ID
       */
      kid: string;

      /**
       * Key Type
       */
      kty: 'EC';

      /**
       * X EC coordinate
       */
      x: string;

      /**
       * Y EC coordinate
       */
      y: string;
    }
  }
}

export interface ConfigurationListParams extends V4PagePaginationArrayParams {
  /**
   * Path param: Identifier.
   */
  zone_id?: string;
}

export interface ConfigurationDeleteParams {
  /**
   * Identifier.
   */
  zone_id?: string;
}

export interface ConfigurationEditParams {
  /**
   * Path param: Identifier.
   */
  zone_id?: string;

  /**
   * Body param
   */
  description?: string;

  /**
   * Body param
   */
  title?: string;

  /**
   * Body param
   */
  token_sources?: Array<string>;
}

export interface ConfigurationGetParams {
  /**
   * Identifier.
   */
  zone_id?: string;
}

Configuration.TokenConfigsV4PagePaginationArray = TokenConfigsV4PagePaginationArray;
Configuration.Credentials = CredentialsAPICredentials;

export declare namespace Configuration {
  export {
    type TokenConfig as TokenConfig,
    type ConfigurationDeleteResponse as ConfigurationDeleteResponse,
    type ConfigurationEditResponse as ConfigurationEditResponse,
    TokenConfigsV4PagePaginationArray as TokenConfigsV4PagePaginationArray,
    type ConfigurationCreateParams as ConfigurationCreateParams,
    type ConfigurationListParams as ConfigurationListParams,
    type ConfigurationDeleteParams as ConfigurationDeleteParams,
    type ConfigurationEditParams as ConfigurationEditParams,
    type ConfigurationGetParams as ConfigurationGetParams,
  };

  export {
    CredentialsAPICredentials as Credentials,
    type CredentialUpdateResponse as CredentialUpdateResponse,
    type CredentialUpdateParams as CredentialUpdateParams,
  };
}
