// 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 BaseDetections extends APIResource {
  static override readonly _key: readonly ['leakedCredentialChecks', 'detections'] = Object.freeze([
    'leakedCredentialChecks',
    'detections',
  ] as const);

  /**
   * Create a detection location for credentials that the default scan locations do
   * not cover, using Rules language expressions such as
   * `lookup_json_string(http.request.body.raw, "user")`. Only the username
   * expression is required, and Leaked Credential Checks must be enabled on the
   * zone.
   *
   * @example
   * ```ts
   * const detection =
   *   await client.leakedCredentialChecks.detections.create({
   *     zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *   });
   * ```
   */
  create(params: DetectionCreateParams, options?: RequestOptions): APIPromise<DetectionCreateResponse> {
    const { zone_id, ...body } = params;
    return (
      this._client.post(path`/zones/${zone_id}/leaked-credential-checks/detections`, {
        body,
        ...options,
      }) as APIPromise<{ result: DetectionCreateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Update the username and password expressions of an existing detection location,
   * identified by its detection ID. Both expressions are overwritten, so omitting
   * the password expression clears it.
   *
   * @example
   * ```ts
   * const detection =
   *   await client.leakedCredentialChecks.detections.update(
   *     '18a14bafaa8eb1df04ce683ec18c765e',
   *     { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  update(
    detectionID: string,
    params: DetectionUpdateParams,
    options?: RequestOptions,
  ): APIPromise<DetectionUpdateResponse> {
    const { zone_id, ...body } = params;
    return (
      this._client.put(path`/zones/${zone_id}/leaked-credential-checks/detections/${detectionID}`, {
        body,
        ...options,
      }) as APIPromise<{ result: DetectionUpdateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * List the user-defined detection locations configured for Leaked Credential
   * Checks, each with its own identifier. A custom detection location tells the WAF
   * where to find the username and password in requests to your application.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const detectionListResponse of client.leakedCredentialChecks.detections.list(
   *   { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * )) {
   *   // ...
   * }
   * ```
   */
  list(
    params: DetectionListParams,
    options?: RequestOptions,
  ): PagePromise<DetectionListResponsesSinglePage, DetectionListResponse> {
    const { zone_id } = params;
    return this._client.getAPIList(
      path`/zones/${zone_id}/leaked-credential-checks/detections`,
      SinglePage<DetectionListResponse>,
      options,
    );
  }

  /**
   * Delete a user-defined detection location, identified by its detection ID.
   * Incoming requests are then scanned using only the default scan locations and the
   * detection locations that remain.
   *
   * @example
   * ```ts
   * const detection =
   *   await client.leakedCredentialChecks.detections.delete(
   *     '18a14bafaa8eb1df04ce683ec18c765e',
   *     { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  delete(
    detectionID: string,
    params: DetectionDeleteParams,
    options?: RequestOptions,
  ): APIPromise<DetectionDeleteResponse> {
    const { zone_id } = params;
    return (
      this._client.delete(
        path`/zones/${zone_id}/leaked-credential-checks/detections/${detectionID}`,
        options,
      ) as APIPromise<{ result: DetectionDeleteResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Get the username and password expressions of a single user-defined detection
   * location, identified by its detection ID.
   *
   * @example
   * ```ts
   * const detection =
   *   await client.leakedCredentialChecks.detections.get(
   *     '18a14bafaa8eb1df04ce683ec18c765e',
   *     { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  get(
    detectionID: string,
    params: DetectionGetParams,
    options?: RequestOptions,
  ): APIPromise<DetectionGetResponse> {
    const { zone_id } = params;
    return (
      this._client.get(
        path`/zones/${zone_id}/leaked-credential-checks/detections/${detectionID}`,
        options,
      ) as APIPromise<{ result: DetectionGetResponse }>
    )._thenUnwrap((obj) => obj.result);
  }
}
export class Detections extends BaseDetections {}

export type DetectionListResponsesSinglePage = SinglePage<DetectionListResponse>;

/**
 * Defines a custom set of username/password expressions to match Leaked Credential
 * Checks on.
 */
export interface DetectionCreateResponse {
  /**
   * Defines the unique ID for this custom detection.
   */
  id?: string;

  /**
   * Defines ehe ruleset expression to use in matching the password in a request.
   */
  password?: string;

  /**
   * Defines the ruleset expression to use in matching the username in a request.
   */
  username?: string;
}

/**
 * Defines a custom set of username/password expressions to match Leaked Credential
 * Checks on.
 */
export interface DetectionUpdateResponse {
  /**
   * Defines the unique ID for this custom detection.
   */
  id?: string;

  /**
   * Defines ehe ruleset expression to use in matching the password in a request.
   */
  password?: string;

  /**
   * Defines the ruleset expression to use in matching the username in a request.
   */
  username?: string;
}

/**
 * Defines a custom set of username/password expressions to match Leaked Credential
 * Checks on.
 */
export interface DetectionListResponse {
  /**
   * Defines the unique ID for this custom detection.
   */
  id?: string;

  /**
   * Defines ehe ruleset expression to use in matching the password in a request.
   */
  password?: string;

  /**
   * Defines the ruleset expression to use in matching the username in a request.
   */
  username?: string;
}

export type DetectionDeleteResponse = unknown;

/**
 * Defines a custom set of username/password expressions to match Leaked Credential
 * Checks on.
 */
export interface DetectionGetResponse {
  /**
   * Defines the unique ID for this custom detection.
   */
  id?: string;

  /**
   * Defines ehe ruleset expression to use in matching the password in a request.
   */
  password?: string;

  /**
   * Defines the ruleset expression to use in matching the username in a request.
   */
  username?: string;
}

export interface DetectionCreateParams {
  /**
   * Path param: Defines an identifier.
   */
  zone_id: string;

  /**
   * Body param: Defines ehe ruleset expression to use in matching the password in a
   * request.
   */
  password?: string;

  /**
   * Body param: Defines the ruleset expression to use in matching the username in a
   * request.
   */
  username?: string;
}

export interface DetectionUpdateParams {
  /**
   * Path param: Defines an identifier.
   */
  zone_id: string;

  /**
   * Body param: Defines ehe ruleset expression to use in matching the password in a
   * request.
   */
  password?: string;

  /**
   * Body param: Defines the ruleset expression to use in matching the username in a
   * request.
   */
  username?: string;
}

export interface DetectionListParams {
  /**
   * Defines an identifier.
   */
  zone_id: string;
}

export interface DetectionDeleteParams {
  /**
   * Defines an identifier.
   */
  zone_id: string;
}

export interface DetectionGetParams {
  /**
   * Defines an identifier.
   */
  zone_id: string;
}

export declare namespace Detections {
  export {
    type DetectionCreateResponse as DetectionCreateResponse,
    type DetectionUpdateResponse as DetectionUpdateResponse,
    type DetectionListResponse as DetectionListResponse,
    type DetectionDeleteResponse as DetectionDeleteResponse,
    type DetectionGetResponse as DetectionGetResponse,
    type DetectionListResponsesSinglePage as DetectionListResponsesSinglePage,
    type DetectionCreateParams as DetectionCreateParams,
    type DetectionUpdateParams as DetectionUpdateParams,
    type DetectionListParams as DetectionListParams,
    type DetectionDeleteParams as DetectionDeleteParams,
    type DetectionGetParams as DetectionGetParams,
  };
}
