// 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 { SinglePage } from '../../../pagination';

export class PagerdutyResource extends APIResource {
  /**
   * Creates a new token for integrating with PagerDuty.
   *
   * @example
   * ```ts
   * const pagerduty =
   *   await client.alerting.destinations.pagerduty.create({
   *     account_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *   });
   * ```
   */
  create(
    params?: PagerdutyCreateParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<PagerdutyCreateResponse>;
  create(options?: Core.RequestOptions): Core.APIPromise<PagerdutyCreateResponse>;
  create(
    params: PagerdutyCreateParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.APIPromise<PagerdutyCreateResponse> {
    if (isRequestOptions(params)) {
      return this.create({}, params);
    }
    const { account_id = this._client.accountId } = params;
    return (
      this._client.post(
        `/accounts/${account_id}/alerting/v3/destinations/pagerduty/connect`,
        options,
      ) as Core.APIPromise<{ result: PagerdutyCreateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Deletes all the PagerDuty Services connected to the account.
   *
   * @example
   * ```ts
   * const pagerduty =
   *   await client.alerting.destinations.pagerduty.delete({
   *     account_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *   });
   * ```
   */
  delete(
    params?: PagerdutyDeleteParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<PagerdutyDeleteResponse>;
  delete(options?: Core.RequestOptions): Core.APIPromise<PagerdutyDeleteResponse>;
  delete(
    params: PagerdutyDeleteParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.APIPromise<PagerdutyDeleteResponse> {
    if (isRequestOptions(params)) {
      return this.delete({}, params);
    }
    const { account_id = this._client.accountId } = params;
    return this._client.delete(`/accounts/${account_id}/alerting/v3/destinations/pagerduty`, options);
  }

  /**
   * Get a list of all configured PagerDuty services.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const pagerduty of client.alerting.destinations.pagerduty.get(
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * )) {
   *   // ...
   * }
   * ```
   */
  get(
    params?: PagerdutyGetParams,
    options?: Core.RequestOptions,
  ): Core.PagePromise<PagerdutiesSinglePage, Pagerduty>;
  get(options?: Core.RequestOptions): Core.PagePromise<PagerdutiesSinglePage, Pagerduty>;
  get(
    params: PagerdutyGetParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.PagePromise<PagerdutiesSinglePage, Pagerduty> {
    if (isRequestOptions(params)) {
      return this.get({}, params);
    }
    const { account_id = this._client.accountId } = params;
    return this._client.getAPIList(
      `/accounts/${account_id}/alerting/v3/destinations/pagerduty`,
      PagerdutiesSinglePage,
      options,
    );
  }

  /**
   * Links PagerDuty with the account using the integration token.
   *
   * @example
   * ```ts
   * const response =
   *   await client.alerting.destinations.pagerduty.link(
   *     '8c71e667571b4f61b94d9e4b12158038',
   *     { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  link(
    tokenId: string,
    params?: PagerdutyLinkParams,
    options?: Core.RequestOptions,
  ): Core.APIPromise<PagerdutyLinkResponse>;
  link(tokenId: string, options?: Core.RequestOptions): Core.APIPromise<PagerdutyLinkResponse>;
  link(
    tokenId: string,
    params: PagerdutyLinkParams | Core.RequestOptions = {},
    options?: Core.RequestOptions,
  ): Core.APIPromise<PagerdutyLinkResponse> {
    if (isRequestOptions(params)) {
      return this.link(tokenId, {}, params);
    }
    const { account_id = this._client.accountId } = params;
    return (
      this._client.get(
        `/accounts/${account_id}/alerting/v3/destinations/pagerduty/connect/${tokenId}`,
        options,
      ) as Core.APIPromise<{ result: PagerdutyLinkResponse }>
    )._thenUnwrap((obj) => obj.result);
  }
}

export class PagerdutiesSinglePage extends SinglePage<Pagerduty> {}

export interface Pagerduty {
  /**
   * UUID
   */
  id?: string;

  /**
   * The name of the pagerduty service.
   */
  name?: string;
}

export interface PagerdutyCreateResponse {
  /**
   * token in form of UUID
   */
  id?: string;
}

export interface PagerdutyDeleteResponse {
  errors: Array<PagerdutyDeleteResponse.Error>;

  messages: Array<PagerdutyDeleteResponse.Message>;

  /**
   * Whether the API call was successful
   */
  success: true;
}

export namespace PagerdutyDeleteResponse {
  export interface Error {
    message: string;

    code?: number;
  }

  export interface Message {
    message: string;

    code?: number;
  }
}

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

export interface PagerdutyCreateParams {
  /**
   * The account id
   */
  account_id?: string;
}

export interface PagerdutyDeleteParams {
  /**
   * The account id
   */
  account_id?: string;
}

export interface PagerdutyGetParams {
  /**
   * The account id
   */
  account_id?: string;
}

export interface PagerdutyLinkParams {
  /**
   * The account id
   */
  account_id?: string;
}

PagerdutyResource.PagerdutiesSinglePage = PagerdutiesSinglePage;

export declare namespace PagerdutyResource {
  export {
    type Pagerduty as Pagerduty,
    type PagerdutyCreateResponse as PagerdutyCreateResponse,
    type PagerdutyDeleteResponse as PagerdutyDeleteResponse,
    type PagerdutyLinkResponse as PagerdutyLinkResponse,
    PagerdutiesSinglePage as PagerdutiesSinglePage,
    type PagerdutyCreateParams as PagerdutyCreateParams,
    type PagerdutyDeleteParams as PagerdutyDeleteParams,
    type PagerdutyGetParams as PagerdutyGetParams,
    type PagerdutyLinkParams as PagerdutyLinkParams,
  };
}
