import { APIResource } from '../../../core/resource';
import { APIPromise } from '../../../core/api-promise';
import { PageCursor, type PageCursorParams, PagePromise } from '../../../core/pagination';
import { RequestOptions } from '../../../internal/request-options';
import { startApiCallSpan } from '../../../internal/tracing';
import { path } from '../../../internal/utils/path';

export class ExternalKeys extends APIResource {
  /**
   * Create an external key config owned by the caller's organization.
   *
   * @throws {UserProfileNotFoundError}
   * @throws {SandboxEgressRestrictedError}
   *
   * @example
   * ```ts
   * const betaExternalKey =
   *   await client.beta.organization.externalKeys.create({
   *     provider_config: {
   *       kms_arn:
   *         'arn:aws:kms:us-east-1:111122223333:key/abcd1234-5678-90ab-cdef-000011112222',
   *       type: 'aws',
   *     },
   *   });
   * ```
   */
  create(body: ExternalKeyCreateParams, options?: RequestOptions): APIPromise<BetaExternalKey> {
    return this._client.post('/v1/organizations/external_keys', {
      body,
      ...options,
      __qualifiers: { beta: 'true' },
      __span: startApiCallSpan(this._client, 'anthropic.organization.external_keys.create', options, {
        route: '/v1/organizations/external_keys',
        createdIDAttribute: 'anthropic.organization.external_key.id',
      }),
    });
  }

  /**
   * Retrieve a single external key config in the caller's organization by ID.
   *
   * @example
   * ```ts
   * const betaExternalKey =
   *   await client.beta.organization.externalKeys.retrieve(
   *     'external_key_id',
   *   );
   * ```
   */
  retrieve(externalKeyID: string, options?: RequestOptions): APIPromise<BetaExternalKey> {
    return this._client.get(path`/v1/organizations/external_keys/${externalKeyID}`, {
      ...options,
      __qualifiers: { beta: 'true' },
      __span: startApiCallSpan(this._client, 'anthropic.organization.external_keys.retrieve', options, {
        route: '/v1/organizations/external_keys/{external_key_id}',
        attributes: { 'anthropic.organization.external_key.id': externalKeyID },
      }),
    });
  }

  /**
   * Partially update an external key config. Omitted fields are left unchanged.
   *
   * `display_name` is always editable. `geo` and `provider_config` cannot be changed
   * once any workspace references this config, and a Google Cloud key's provider and
   * `key_name` cannot be changed at all.
   *
   * @throws {ExternalKeyInWorkspacePolicyError}
   * @throws {SandboxEgressRestrictedError}
   *
   * @example
   * ```ts
   * const betaExternalKey =
   *   await client.beta.organization.externalKeys.update(
   *     'external_key_id',
   *     {},
   *   );
   * ```
   */
  update(
    externalKeyID: string,
    body: ExternalKeyUpdateParams,
    options?: RequestOptions,
  ): APIPromise<BetaExternalKey> {
    return this._client.post(path`/v1/organizations/external_keys/${externalKeyID}`, {
      body,
      ...options,
      __qualifiers: { beta: 'true' },
      __span: startApiCallSpan(this._client, 'anthropic.organization.external_keys.update', options, {
        route: '/v1/organizations/external_keys/{external_key_id}',
        attributes: { 'anthropic.organization.external_key.id': externalKeyID },
      }),
    });
  }

  /**
   * List external key configs in the caller's organization.
   *
   * Results are ordered by creation time (newest first). Use the `next_page` cursor
   * from the response to fetch subsequent pages.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const betaExternalKey of client.beta.organization.externalKeys.list()) {
   *   // ...
   * }
   * ```
   */
  list(
    query: ExternalKeyListParams | null | undefined = {},
    options?: RequestOptions,
  ): PagePromise<BetaExternalKeysPageCursor, BetaExternalKey> {
    return this._client.getAPIList('/v1/organizations/external_keys', PageCursor<BetaExternalKey>, {
      query,
      ...options,
      __qualifiers: { beta: 'true' },
      __span: startApiCallSpan(this._client, 'anthropic.organization.external_keys.list', options, {
        route: '/v1/organizations/external_keys',
        queryParams: ['limit', 'page'],
      }),
    });
  }

  /**
   * Delete an external key config.
   *
   * The request is rejected if any workspace still references this config.
   *
   * @throws {ExternalKeyInWorkspacePolicyError}
   * @throws {SandboxEgressRestrictedError}
   *
   * @example
   * ```ts
   * const externalKey =
   *   await client.beta.organization.externalKeys.delete(
   *     'external_key_id',
   *   );
   * ```
   */
  delete(externalKeyID: string, options?: RequestOptions): APIPromise<ExternalKeyDeleteResponse> {
    return this._client.delete(path`/v1/organizations/external_keys/${externalKeyID}`, {
      ...options,
      __qualifiers: { beta: 'true' },
      __span: startApiCallSpan(this._client, 'anthropic.organization.external_keys.delete', options, {
        route: '/v1/organizations/external_keys/{external_key_id}',
        attributes: { 'anthropic.organization.external_key.id': externalKeyID },
      }),
    });
  }

  /**
   * Validate an external key config against the customer's KMS.
   *
   * Anthropic performs an encrypt/decrypt roundtrip against the configured KMS key
   * and waits up to 30 seconds for the result. The response status is `success` if
   * the roundtrip succeeded, or `failure` with an error message if it failed or
   * timed out.
   *
   * @throws {SandboxEgressRestrictedError}
   *
   * @example
   * ```ts
   * const response =
   *   await client.beta.organization.externalKeys.validate(
   *     'external_key_id',
   *   );
   * ```
   */
  validate(externalKeyID: string, options?: RequestOptions): APIPromise<ExternalKeyValidateResponse> {
    return this._client.post(path`/v1/organizations/external_keys/${externalKeyID}/validate`, {
      ...options,
      __qualifiers: { beta: 'true' },
      __span: startApiCallSpan(this._client, 'anthropic.organization.external_keys.validate', options, {
        route: '/v1/organizations/external_keys/{external_key_id}/validate',
        attributes: { 'anthropic.organization.external_key.id': externalKeyID },
      }),
    });
  }
}

export type BetaExternalKeysPageCursor = PageCursor<BetaExternalKey>;

export interface BetaAWSExternalKeyConfig {
  /**
   * Full ARN of the AWS KMS key. On Claude Platform on AWS, alias ARNs are rejected,
   * and your organization's own key must be a single-Region key.
   */
  kms_arn: string;

  type: 'aws';

  /**
   * AWS region. Derived from `kms_arn` if omitted.
   */
  region?: string | null;

  /**
   * @deprecated IAM role ARN. Deprecated; this field is ignored.
   */
  role_arn?: string | null;
}

export interface BetaAzureExternalKeyConfig {
  /**
   * Name of the key within the vault.
   */
  key_name: string;

  /**
   * Azure AD tenant ID.
   */
  tenant_id: string;

  type: 'azure';

  /**
   * Key Vault data-plane URI — `https://{vault-name}.vault.azure.net` or
   * `https://{hsm-name}.managedhsm.azure.net`.
   */
  vault_uri: string;

  /**
   * Azure AD application (client) ID. Omit to use Anthropic's multitenant app.
   * Provide only if using a single-tenant app registration in the customer's
   * directory.
   */
  client_id?: string | null;
}

/**
 * Azure Key Vault provider configuration.
 */
export interface BetaAzureExternalKeyConfigParam {
  /**
   * Name of the key within the vault.
   */
  key_name: string;

  /**
   * Azure AD tenant ID.
   */
  tenant_id: string;

  type: 'azure';

  /**
   * Key Vault data-plane URI — `https://{vault-name}.vault.azure.net` or
   * `https://{hsm-name}.managedhsm.azure.net`.
   */
  vault_uri: string;

  /**
   * Azure AD application (client) ID. Omit to use Anthropic's multitenant app.
   * Provide only if using a single-tenant app registration in the customer's
   * directory.
   */
  client_id?: string | null;
}

/**
 * CMEK external key config belonging to the caller's organization.
 *
 * Configs are organization-scoped. Workspaces attach to a config; once any
 * workspace references it, the provider fields become effectively immutable
 * (existing encrypted data needs the config for decrypt).
 */
export interface BetaExternalKey {
  /**
   * Identifier of the external key config: a tagged ID prefixed `ekey_`, or, on
   * Claude Platform on AWS, an AWS KMS key's ARN.
   */
  id: string;

  /**
   * Whether any workspace uses this config to encrypt its data — counting live and
   * archived workspaces (an archived workspace's data remains encrypted under the
   * config), excluding deleted ones. Only an attached config is used by the
   * encryption path; an `unattached` config is inert and can be deleted.
   */
  attachment: BetaExternalKeyAttachedAttachment | BetaExternalKeyUnattachedAttachment;

  created_at: string;

  /**
   * Human-friendly display name. Null if none was set.
   */
  display_name: string | null;

  /**
   * Data residency geo. Selects the region this key is used in.
   */
  geo: string;

  /**
   * KMS provider identity and auth coordinates.
   */
  provider_config: BetaAWSExternalKeyConfig | BetaGCPExternalKeyConfig | BetaAzureExternalKeyConfig;

  type: 'external_key';

  updated_at: string;

  /**
   * ID of the user profile the key is bound to, or null if the key is not bound to
   * one. A key bound to a user profile can only be attached to a Workspace that has
   * the same user profile. A key that isn't bound can only be attached to a
   * Workspace that has no user profile.
   */
  user_profile_id: string | null;
}

export interface BetaExternalKeyAttachedAttachment {
  type: 'attached';
}

export interface BetaExternalKeyUnattachedAttachment {
  type: 'unattached';
}

export interface BetaGCPExternalKeyConfig {
  /**
   * Full resource name of the Cloud KMS key.
   */
  key_name: string;

  type: 'gcp';
}

export interface ExternalKeyDeleteResponse {
  /**
   * ID of the deleted External Key.
   */
  id: string;

  type: 'external_key_deleted';
}

/**
 * Result of a validation roundtrip against the customer's KMS.
 *
 * HTTP 200 for both outcomes — the operation completed; `status` says whether the
 * key works.
 */
export interface ExternalKeyValidateResponse {
  /**
   * Error message when status is `failure`. Null otherwise.
   */
  error: string | null;

  /**
   * `success` — encrypt/decrypt roundtrip succeeded. `failure` — the roundtrip
   * failed or timed out; see `error`.
   */
  status: 'failure' | 'success';

  type: 'external_key_validation';
}

export interface ExternalKeyCreateParams {
  /**
   * KMS provider identity and auth coordinates.
   */
  provider_config: BetaAWSExternalKeyConfig | BetaGCPExternalKeyConfig | BetaAzureExternalKeyConfigParam;

  /**
   * Human-friendly display name.
   */
  display_name?: string | null;

  /**
   * Data residency geo. Only `us` is supported.
   */
  geo?: 'us';

  /**
   * ID of a user profile to bind the key to. A user profile represents one of your
   * customers: bind a customer's key to that customer's user profile. A key bound to
   * a user profile can only be attached to a Workspace that has the same user
   * profile, and the binding cannot be changed later. Omit this field only for your
   * organization's own key, never a customer's. A key created without a user profile
   * ID can't be attached to a Workspace that has a user profile, and you can't add
   * one to the key later.
   */
  user_profile_id?: string | null;
}

export interface ExternalKeyUpdateParams {
  /**
   * Human-friendly display name.
   */
  display_name?: string | null;

  /**
   * Data residency geo. Only `us` is supported.
   */
  geo?: 'us' | null;

  /**
   * KMS provider identity and auth coordinates.
   */
  provider_config?:
    | BetaAWSExternalKeyConfig
    | BetaGCPExternalKeyConfig
    | BetaAzureExternalKeyConfigParam
    | null;
}

export interface ExternalKeyListParams extends PageCursorParams {}

export declare namespace ExternalKeys {
  export {
    type BetaAWSExternalKeyConfig as BetaAWSExternalKeyConfig,
    type BetaAzureExternalKeyConfig as BetaAzureExternalKeyConfig,
    type BetaAzureExternalKeyConfigParam as BetaAzureExternalKeyConfigParam,
    type BetaExternalKey as BetaExternalKey,
    type BetaExternalKeyAttachedAttachment as BetaExternalKeyAttachedAttachment,
    type BetaExternalKeyUnattachedAttachment as BetaExternalKeyUnattachedAttachment,
    type BetaGCPExternalKeyConfig as BetaGCPExternalKeyConfig,
    type ExternalKeyDeleteResponse as ExternalKeyDeleteResponse,
    type ExternalKeyValidateResponse as ExternalKeyValidateResponse,
    type BetaExternalKeysPageCursor as BetaExternalKeysPageCursor,
    type ExternalKeyCreateParams as ExternalKeyCreateParams,
    type ExternalKeyUpdateParams as ExternalKeyUpdateParams,
    type ExternalKeyListParams as ExternalKeyListParams,
  };
}
