// 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,
  V4PagePaginationArray,
  type V4PagePaginationArrayParams,
} from '../../../core/pagination';
import { RequestOptions } from '../../../internal/request-options';
import { path } from '../../../internal/utils/path';

export class BaseSendingDomainRestrictions extends APIResource {
  static override readonly _key: readonly ['emailSecurity', 'settings', 'sendingDomainRestrictions'] =
    Object.freeze(['emailSecurity', 'settings', 'sendingDomainRestrictions'] as const);

  /**
   * Creates a new sending domain restriction to enforce TLS requirements for a
   * domain. Emails without TLS from this domain will be dropped unless the subdomain
   * is in the exclude list.
   *
   * @example
   * ```ts
   * const sendingDomainRestriction =
   *   await client.emailSecurity.settings.sendingDomainRestrictions.create(
   *     {
   *       account_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *       domain: 'example.com',
   *       exclude: ['subdomain.example.com'],
   *     },
   *   );
   * ```
   */
  create(
    params: SendingDomainRestrictionCreateParams,
    options?: RequestOptions,
  ): APIPromise<SendingDomainRestrictionCreateResponse> {
    const { account_id, ...body } = params;
    return (
      this._client.post(path`/accounts/${account_id}/email-security/settings/sending_domain_restrictions`, {
        body,
        ...options,
      }) as APIPromise<{ result: SendingDomainRestrictionCreateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Returns a paginated list of sending domain restrictions. These restrictions
   * enforce TLS requirements for emails from specific domains. Mail without TLS from
   * restricted domains will be dropped unless the subdomain is in the exclude list.
   * Supports sorting and searching.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const sendingDomainRestrictionListResponse of client.emailSecurity.settings.sendingDomainRestrictions.list(
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * )) {
   *   // ...
   * }
   * ```
   */
  list(
    params: SendingDomainRestrictionListParams,
    options?: RequestOptions,
  ): PagePromise<
    SendingDomainRestrictionListResponsesV4PagePaginationArray,
    SendingDomainRestrictionListResponse
  > {
    const { account_id, ...query } = params;
    return this._client.getAPIList(
      path`/accounts/${account_id}/email-security/settings/sending_domain_restrictions`,
      V4PagePaginationArray<SendingDomainRestrictionListResponse>,
      { query, ...options },
    );
  }

  /**
   * Removes a sending domain restriction. After deletion, TLS will no longer be
   * enforced for emails from this domain.
   *
   * @example
   * ```ts
   * const sendingDomainRestriction =
   *   await client.emailSecurity.settings.sendingDomainRestrictions.delete(
   *     'f174e90a-fafe-4643-bbbc-4a0ed4fc8415',
   *     { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  delete(
    sendingDomainRestrictionID: string,
    params: SendingDomainRestrictionDeleteParams,
    options?: RequestOptions,
  ): APIPromise<SendingDomainRestrictionDeleteResponse> {
    const { account_id } = params;
    return (
      this._client.delete(
        path`/accounts/${account_id}/email-security/settings/sending_domain_restrictions/${sendingDomainRestrictionID}`,
        options,
      ) as APIPromise<{ result: SendingDomainRestrictionDeleteResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Updates an existing sending domain restriction. Only provided fields will be
   * modified. Changes affect which domains require TLS and which subdomains are
   * excluded.
   *
   * @example
   * ```ts
   * const response =
   *   await client.emailSecurity.settings.sendingDomainRestrictions.edit(
   *     'f174e90a-fafe-4643-bbbc-4a0ed4fc8415',
   *     { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  edit(
    sendingDomainRestrictionID: string,
    params: SendingDomainRestrictionEditParams,
    options?: RequestOptions,
  ): APIPromise<SendingDomainRestrictionEditResponse> {
    const { account_id, ...body } = params;
    return (
      this._client.patch(
        path`/accounts/${account_id}/email-security/settings/sending_domain_restrictions/${sendingDomainRestrictionID}`,
        { body, ...options },
      ) as APIPromise<{ result: SendingDomainRestrictionEditResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Retrieves details for a specific sending domain restriction including the domain
   * requiring TLS and any excluded subdomains exempt from the TLS requirement.
   *
   * @example
   * ```ts
   * const sendingDomainRestriction =
   *   await client.emailSecurity.settings.sendingDomainRestrictions.get(
   *     'f174e90a-fafe-4643-bbbc-4a0ed4fc8415',
   *     { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   *   );
   * ```
   */
  get(
    sendingDomainRestrictionID: string,
    params: SendingDomainRestrictionGetParams,
    options?: RequestOptions,
  ): APIPromise<SendingDomainRestrictionGetResponse> {
    const { account_id } = params;
    return (
      this._client.get(
        path`/accounts/${account_id}/email-security/settings/sending_domain_restrictions/${sendingDomainRestrictionID}`,
        options,
      ) as APIPromise<{ result: SendingDomainRestrictionGetResponse }>
    )._thenUnwrap((obj) => obj.result);
  }
}
export class SendingDomainRestrictions extends BaseSendingDomainRestrictions {}

export type SendingDomainRestrictionListResponsesV4PagePaginationArray =
  V4PagePaginationArray<SendingDomainRestrictionListResponse>;

/**
 * A sending domain restriction that enforces TLS (Transport Layer Security)
 * requirements for emails from specific domains. If TLS is required, the system
 * drops mail without TLS from the specified domain.
 */
export interface SendingDomainRestrictionCreateResponse {
  /**
   * Sending domain restriction identifier.
   */
  id?: string;

  comments?: string | null;

  created_at?: string;

  /**
   * Domain that requires TLS enforcement.
   */
  domain?: string;

  /**
   * Subdomains to exempt from TLS requirements.
   */
  exclude?: Array<string>;

  /**
   * @deprecated Use `modified_at` instead.
   */
  last_modified?: string;

  modified_at?: string;
}

/**
 * A sending domain restriction that enforces TLS (Transport Layer Security)
 * requirements for emails from specific domains. If TLS is required, the system
 * drops mail without TLS from the specified domain.
 */
export interface SendingDomainRestrictionListResponse {
  /**
   * Sending domain restriction identifier.
   */
  id?: string;

  comments?: string | null;

  created_at?: string;

  /**
   * Domain that requires TLS enforcement.
   */
  domain?: string;

  /**
   * Subdomains to exempt from TLS requirements.
   */
  exclude?: Array<string>;

  /**
   * @deprecated Use `modified_at` instead.
   */
  last_modified?: string;

  modified_at?: string;
}

export interface SendingDomainRestrictionDeleteResponse {
  /**
   * Sending domain restriction identifier.
   */
  id: string;
}

/**
 * A sending domain restriction that enforces TLS (Transport Layer Security)
 * requirements for emails from specific domains. If TLS is required, the system
 * drops mail without TLS from the specified domain.
 */
export interface SendingDomainRestrictionEditResponse {
  /**
   * Sending domain restriction identifier.
   */
  id?: string;

  comments?: string | null;

  created_at?: string;

  /**
   * Domain that requires TLS enforcement.
   */
  domain?: string;

  /**
   * Subdomains to exempt from TLS requirements.
   */
  exclude?: Array<string>;

  /**
   * @deprecated Use `modified_at` instead.
   */
  last_modified?: string;

  modified_at?: string;
}

/**
 * A sending domain restriction that enforces TLS (Transport Layer Security)
 * requirements for emails from specific domains. If TLS is required, the system
 * drops mail without TLS from the specified domain.
 */
export interface SendingDomainRestrictionGetResponse {
  /**
   * Sending domain restriction identifier.
   */
  id?: string;

  comments?: string | null;

  created_at?: string;

  /**
   * Domain that requires TLS enforcement.
   */
  domain?: string;

  /**
   * Subdomains to exempt from TLS requirements.
   */
  exclude?: Array<string>;

  /**
   * @deprecated Use `modified_at` instead.
   */
  last_modified?: string;

  modified_at?: string;
}

export interface SendingDomainRestrictionCreateParams {
  /**
   * Path param: Account identifier tag.
   */
  account_id: string;

  /**
   * Body param: Domain that requires TLS enforcement.
   */
  domain: string;

  /**
   * Body param: Subdomains to exempt from TLS requirements.
   */
  exclude: Array<string>;

  /**
   * Body param
   */
  comments?: string | null;
}

export interface SendingDomainRestrictionListParams extends V4PagePaginationArrayParams {
  /**
   * Path param: Account identifier tag.
   */
  account_id: string;

  /**
   * Query param: The sorting direction.
   */
  direction?: 'asc' | 'desc';

  /**
   * Query param: Field to sort by.
   */
  order?: 'domain' | 'created_at';

  /**
   * Query param: Search term for filtering records. Behavior may change.
   */
  search?: string;
}

export interface SendingDomainRestrictionDeleteParams {
  /**
   * Account identifier tag.
   */
  account_id: string;
}

export interface SendingDomainRestrictionEditParams {
  /**
   * Path param: Account identifier tag.
   */
  account_id: string;

  /**
   * Body param
   */
  comments?: string | null;

  /**
   * Body param: Domain that requires TLS enforcement.
   */
  domain?: string;

  /**
   * Body param: Subdomains to exempt from TLS requirements.
   */
  exclude?: Array<string>;
}

export interface SendingDomainRestrictionGetParams {
  /**
   * Account identifier tag.
   */
  account_id: string;
}

export declare namespace SendingDomainRestrictions {
  export {
    type SendingDomainRestrictionCreateResponse as SendingDomainRestrictionCreateResponse,
    type SendingDomainRestrictionListResponse as SendingDomainRestrictionListResponse,
    type SendingDomainRestrictionDeleteResponse as SendingDomainRestrictionDeleteResponse,
    type SendingDomainRestrictionEditResponse as SendingDomainRestrictionEditResponse,
    type SendingDomainRestrictionGetResponse as SendingDomainRestrictionGetResponse,
    type SendingDomainRestrictionListResponsesV4PagePaginationArray as SendingDomainRestrictionListResponsesV4PagePaginationArray,
    type SendingDomainRestrictionCreateParams as SendingDomainRestrictionCreateParams,
    type SendingDomainRestrictionListParams as SendingDomainRestrictionListParams,
    type SendingDomainRestrictionDeleteParams as SendingDomainRestrictionDeleteParams,
    type SendingDomainRestrictionEditParams as SendingDomainRestrictionEditParams,
    type SendingDomainRestrictionGetParams as SendingDomainRestrictionGetParams,
  };
}
