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

import { APIResource } from '../../core/resource';
import {
  PagePromise,
  SinglePage,
  V4PagePagination,
  type V4PagePaginationParams,
} from '../../core/pagination';
import { RequestOptions } from '../../internal/request-options';
import { path } from '../../internal/utils/path';

export class BaseMitigations extends APIResource {
  static override readonly _key: readonly ['abuseReports', 'mitigations'] = Object.freeze([
    'abuseReports',
    'mitigations',
  ] as const);

  /**
   * List mitigations done to remediate the abuse report.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const mitigationListResponse of client.abuseReports.mitigations.list(
   *   'report_id',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * )) {
   *   // ...
   * }
   * ```
   */
  list(
    reportID: string,
    params: MitigationListParams,
    options?: RequestOptions,
  ): PagePromise<MitigationListResponsesV4PagePagination, MitigationListResponse> {
    const { account_id, ...query } = params;
    return this._client.getAPIList(
      path`/accounts/${account_id}/abuse-reports/${reportID}/mitigations`,
      V4PagePagination<MitigationListResponse>,
      { query, ...options },
    );
  }

  /**
   * Request a review of mitigations applied because of an abuse report, or submit a
   * report-level appeal.
   *
   * - To request a review of specific mitigations, send `appeals` with the
   *   mitigation IDs and reasons. Repeating a request for a mitigation with an
   *   unresolved appeal is idempotent and returns that mitigation in the in-review
   *   state.
   * - To submit a report-level appeal, send `type` and, for a `counter_notice`, the
   *   counter-notice details in `data`. Report-level appeals are currently available
   *   only for DMCA (copyright) reports.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const mitigationReviewResponse of client.abuseReports.mitigations.review(
   *   'report_id',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * )) {
   *   // ...
   * }
   * ```
   */
  review(
    reportID: string,
    params: MitigationReviewParams,
    options?: RequestOptions,
  ): PagePromise<MitigationReviewResponsesSinglePage, MitigationReviewResponse> {
    const { account_id, ...body } = params;
    return this._client.getAPIList(
      path`/accounts/${account_id}/abuse-reports/${reportID}/mitigations/appeal`,
      SinglePage<MitigationReviewResponse>,
      { body, method: 'post', ...options },
    );
  }
}
export class Mitigations extends BaseMitigations {}

export type MitigationListResponsesV4PagePagination = V4PagePagination<MitigationListResponse>;

export type MitigationReviewResponsesSinglePage = SinglePage<MitigationReviewResponse>;

export interface MitigationListResponse {
  mitigations: Array<MitigationListResponse.Mitigation>;
}

export namespace MitigationListResponse {
  export interface Mitigation {
    /**
     * ID of remediation.
     */
    id: string;

    /**
     * Date when the mitigation will become active. Time in RFC 3339 format
     * (https://www.rfc-editor.org/rfc/rfc3339.html)
     */
    effective_date: string;

    entity_id: string;

    /**
     * The type of entity targeted by a mitigation.
     */
    entity_type: 'url_pattern' | 'account' | 'zone' | 'custom_expression';

    /**
     * The status of a mitigation
     */
    status: 'pending' | 'active' | 'in_review' | 'cancelled' | 'removed';

    /**
     * The type of mitigation applied to a reported entity.
     */
    type: string;
  }
}

export interface MitigationReviewResponse {
  /**
   * ID of remediation.
   */
  id: string;

  /**
   * Date when the mitigation will become active. Time in RFC 3339 format
   * (https://www.rfc-editor.org/rfc/rfc3339.html)
   */
  effective_date: string;

  entity_id: string;

  /**
   * The type of entity targeted by a mitigation.
   */
  entity_type: 'url_pattern' | 'account' | 'zone' | 'custom_expression';

  /**
   * The status of a mitigation
   */
  status: 'pending' | 'active' | 'in_review' | 'cancelled' | 'removed';

  /**
   * The type of mitigation applied to a reported entity.
   */
  type: string;
}

export interface MitigationListParams extends V4PagePaginationParams {
  /**
   * Path param: Cloudflare Account ID
   */
  account_id: string;

  /**
   * Query param: Returns mitigation that were dispatched after the given date
   */
  effective_after?: string;

  /**
   * Query param: Returns mitigations that were dispatched before the given date
   */
  effective_before?: string;

  /**
   * Query param: Filter by the type of entity the mitigation impacts.
   */
  entity_type?: 'url_pattern' | 'account' | 'zone' | 'custom_expression';

  /**
   * Query param: A property to sort by, followed by the order
   */
  sort?:
    | 'type,asc'
    | 'type,desc'
    | 'effective_date,asc'
    | 'effective_date,desc'
    | 'status,asc'
    | 'status,desc'
    | 'entity_type,asc'
    | 'entity_type,desc';

  /**
   * Query param: Filter by the status of the mitigation.
   */
  status?: 'pending' | 'active' | 'in_review' | 'cancelled' | 'removed';

  /**
   * Query param: Filter by the type of mitigation. This filter parameter can be
   * specified multiple times to include multiple types of mitigations in the result
   * set.
   */
  type?: string;
}

export interface MitigationReviewParams {
  /**
   * Path param: Cloudflare Account ID
   */
  account_id: string;

  /**
   * Body param: List of mitigations to appeal.
   */
  appeals?: Array<MitigationReviewParams.Appeal>;

  /**
   * Body param: Counter-notice details supporting an appeal.
   */
  data?: MitigationReviewParams.Data;

  /**
   * Body param: The type of appeal being submitted.
   */
  type?: 'counter_notice' | 'content_removed';
}

export namespace MitigationReviewParams {
  export interface Appeal {
    /**
     * ID of the mitigation to appeal.
     */
    id: string;

    /**
     * Reason why the customer is appealing.
     */
    reason: 'removed' | 'misclassified';
  }

  /**
   * Counter-notice details supporting an appeal.
   */
  export interface Data {
    city: string;

    country: string;

    email: string;

    full_name: string;

    jurisdiction_consent: boolean;

    perjury_attestation: boolean;

    phone_number: string;

    signature: string;

    state: string;

    street_address: string;

    urls: Array<string>;

    zip_code: string;

    company?: string;

    counter_notice_response?: string;
  }
}

export declare namespace Mitigations {
  export {
    type MitigationListResponse as MitigationListResponse,
    type MitigationReviewResponse as MitigationReviewResponse,
    type MitigationListResponsesV4PagePagination as MitigationListResponsesV4PagePagination,
    type MitigationReviewResponsesSinglePage as MitigationReviewResponsesSinglePage,
    type MitigationListParams as MitigationListParams,
    type MitigationReviewParams as MitigationReviewParams,
  };
}
