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

  /**
   * Creates a dataset that selects gateway logs matching the specified filters for
   * use in evaluations. Evaluations and datasets are deprecated and unavailable to
   * new accounts.
   *
   * @example
   * ```ts
   * const dataset = await client.aiGateway.datasets.create(
   *   'my-gateway',
   *   {
   *     account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
   *     enable: true,
   *     filters: [
   *       {
   *         key: 'created_at',
   *         operator: 'eq',
   *         value: ['string'],
   *       },
   *     ],
   *     name: 'name',
   *   },
   * );
   * ```
   */
  create(
    gatewayID: string,
    params: DatasetCreateParams,
    options?: RequestOptions,
  ): APIPromise<DatasetCreateResponse> {
    const { account_id, ...body } = params;
    return (
      this._client.post(path`/accounts/${account_id}/ai-gateway/gateways/${gatewayID}/datasets`, {
        body,
        ...options,
      }) as APIPromise<{ result: DatasetCreateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Replaces the name, log filters, and enabled state of a dataset. Evaluations and
   * datasets are deprecated and unavailable to new accounts.
   *
   * @example
   * ```ts
   * const dataset = await client.aiGateway.datasets.update(
   *   'id',
   *   {
   *     account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
   *     gateway_id: 'my-gateway',
   *     enable: true,
   *     filters: [
   *       {
   *         key: 'created_at',
   *         operator: 'eq',
   *         value: ['string'],
   *       },
   *     ],
   *     name: 'name',
   *   },
   * );
   * ```
   */
  update(
    id: string,
    params: DatasetUpdateParams,
    options?: RequestOptions,
  ): APIPromise<DatasetUpdateResponse> {
    const { account_id, gateway_id, ...body } = params;
    return (
      this._client.put(path`/accounts/${account_id}/ai-gateway/gateways/${gateway_id}/datasets/${id}`, {
        body,
        ...options,
      }) as APIPromise<{ result: DatasetUpdateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Lists the datasets defined for an AI Gateway. Evaluations and datasets are
   * deprecated and unavailable to new accounts.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const datasetListResponse of client.aiGateway.datasets.list(
   *   'my-gateway',
   *   { account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0' },
   * )) {
   *   // ...
   * }
   * ```
   */
  list(
    gatewayID: string,
    params: DatasetListParams,
    options?: RequestOptions,
  ): PagePromise<DatasetListResponsesV4PagePaginationArray, DatasetListResponse> {
    const { account_id, ...query } = params;
    return this._client.getAPIList(
      path`/accounts/${account_id}/ai-gateway/gateways/${gatewayID}/datasets`,
      V4PagePaginationArray<DatasetListResponse>,
      { query, ...options },
    );
  }

  /**
   * Deletes a dataset. Evaluations and datasets are deprecated and unavailable to
   * new accounts.
   *
   * @example
   * ```ts
   * const dataset = await client.aiGateway.datasets.delete(
   *   'id',
   *   {
   *     account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
   *     gateway_id: 'my-gateway',
   *   },
   * );
   * ```
   */
  delete(
    id: string,
    params: DatasetDeleteParams,
    options?: RequestOptions,
  ): APIPromise<DatasetDeleteResponse> {
    const { account_id, gateway_id } = params;
    return (
      this._client.delete(
        path`/accounts/${account_id}/ai-gateway/gateways/${gateway_id}/datasets/${id}`,
        options,
      ) as APIPromise<{ result: DatasetDeleteResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Retrieves a dataset and its log filters. Evaluations and datasets are deprecated
   * and unavailable to new accounts.
   *
   * @example
   * ```ts
   * const dataset = await client.aiGateway.datasets.get('id', {
   *   account_id: '3ebbcb006d4d46d7bb6a8c7f14676cb0',
   *   gateway_id: 'my-gateway',
   * });
   * ```
   */
  get(id: string, params: DatasetGetParams, options?: RequestOptions): APIPromise<DatasetGetResponse> {
    const { account_id, gateway_id } = params;
    return (
      this._client.get(
        path`/accounts/${account_id}/ai-gateway/gateways/${gateway_id}/datasets/${id}`,
        options,
      ) as APIPromise<{ result: DatasetGetResponse }>
    )._thenUnwrap((obj) => obj.result);
  }
}
export class Datasets extends BaseDatasets {}

export type DatasetListResponsesV4PagePaginationArray = V4PagePaginationArray<DatasetListResponse>;

export interface DatasetCreateResponse {
  id: string;

  created_at: string;

  enable: boolean;

  filters: Array<DatasetCreateResponse.Filter>;

  /**
   * Unique identifier of the AI Gateway within the account.
   */
  gateway_id: string;

  modified_at: string;

  name: string;
}

export namespace DatasetCreateResponse {
  export interface Filter {
    key:
      | 'created_at'
      | 'request_content_type'
      | 'response_content_type'
      | 'success'
      | 'cached'
      | 'provider'
      | 'model'
      | 'cost'
      | 'tokens'
      | 'tokens_in'
      | 'tokens_out'
      | 'duration'
      | 'feedback';

    operator: 'eq' | 'contains' | 'lt' | 'gt';

    value: Array<string | number | boolean>;
  }
}

export interface DatasetUpdateResponse {
  id: string;

  created_at: string;

  enable: boolean;

  filters: Array<DatasetUpdateResponse.Filter>;

  /**
   * Unique identifier of the AI Gateway within the account.
   */
  gateway_id: string;

  modified_at: string;

  name: string;
}

export namespace DatasetUpdateResponse {
  export interface Filter {
    key:
      | 'created_at'
      | 'request_content_type'
      | 'response_content_type'
      | 'success'
      | 'cached'
      | 'provider'
      | 'model'
      | 'cost'
      | 'tokens'
      | 'tokens_in'
      | 'tokens_out'
      | 'duration'
      | 'feedback';

    operator: 'eq' | 'contains' | 'lt' | 'gt';

    value: Array<string | number | boolean>;
  }
}

export interface DatasetListResponse {
  id: string;

  created_at: string;

  enable: boolean;

  filters: Array<DatasetListResponse.Filter>;

  /**
   * Unique identifier of the AI Gateway within the account.
   */
  gateway_id: string;

  modified_at: string;

  name: string;
}

export namespace DatasetListResponse {
  export interface Filter {
    key:
      | 'created_at'
      | 'request_content_type'
      | 'response_content_type'
      | 'success'
      | 'cached'
      | 'provider'
      | 'model'
      | 'cost'
      | 'tokens'
      | 'tokens_in'
      | 'tokens_out'
      | 'duration'
      | 'feedback';

    operator: 'eq' | 'contains' | 'lt' | 'gt';

    value: Array<string | number | boolean>;
  }
}

export interface DatasetDeleteResponse {
  id: string;

  created_at: string;

  enable: boolean;

  filters: Array<DatasetDeleteResponse.Filter>;

  /**
   * Unique identifier of the AI Gateway within the account.
   */
  gateway_id: string;

  modified_at: string;

  name: string;
}

export namespace DatasetDeleteResponse {
  export interface Filter {
    key:
      | 'created_at'
      | 'request_content_type'
      | 'response_content_type'
      | 'success'
      | 'cached'
      | 'provider'
      | 'model'
      | 'cost'
      | 'tokens'
      | 'tokens_in'
      | 'tokens_out'
      | 'duration'
      | 'feedback';

    operator: 'eq' | 'contains' | 'lt' | 'gt';

    value: Array<string | number | boolean>;
  }
}

export interface DatasetGetResponse {
  id: string;

  created_at: string;

  enable: boolean;

  filters: Array<DatasetGetResponse.Filter>;

  /**
   * Unique identifier of the AI Gateway within the account.
   */
  gateway_id: string;

  modified_at: string;

  name: string;
}

export namespace DatasetGetResponse {
  export interface Filter {
    key:
      | 'created_at'
      | 'request_content_type'
      | 'response_content_type'
      | 'success'
      | 'cached'
      | 'provider'
      | 'model'
      | 'cost'
      | 'tokens'
      | 'tokens_in'
      | 'tokens_out'
      | 'duration'
      | 'feedback';

    operator: 'eq' | 'contains' | 'lt' | 'gt';

    value: Array<string | number | boolean>;
  }
}

export interface DatasetCreateParams {
  /**
   * Path param
   */
  account_id: string;

  /**
   * Body param
   */
  enable: boolean;

  /**
   * Body param
   */
  filters: Array<DatasetCreateParams.Filter>;

  /**
   * Body param
   */
  name: string;
}

export namespace DatasetCreateParams {
  export interface Filter {
    key:
      | 'created_at'
      | 'request_content_type'
      | 'response_content_type'
      | 'success'
      | 'cached'
      | 'provider'
      | 'model'
      | 'cost'
      | 'tokens'
      | 'tokens_in'
      | 'tokens_out'
      | 'duration'
      | 'feedback';

    operator: 'eq' | 'contains' | 'lt' | 'gt';

    value: Array<string | number | boolean>;
  }
}

export interface DatasetUpdateParams {
  /**
   * Path param
   */
  account_id: string;

  /**
   * Path param: Unique identifier of the AI Gateway within the account.
   */
  gateway_id: string;

  /**
   * Body param
   */
  enable: boolean;

  /**
   * Body param
   */
  filters: Array<DatasetUpdateParams.Filter>;

  /**
   * Body param
   */
  name: string;
}

export namespace DatasetUpdateParams {
  export interface Filter {
    key:
      | 'created_at'
      | 'request_content_type'
      | 'response_content_type'
      | 'success'
      | 'cached'
      | 'provider'
      | 'model'
      | 'cost'
      | 'tokens'
      | 'tokens_in'
      | 'tokens_out'
      | 'duration'
      | 'feedback';

    operator: 'eq' | 'contains' | 'lt' | 'gt';

    value: Array<string | number | boolean>;
  }
}

export interface DatasetListParams extends V4PagePaginationArrayParams {
  /**
   * Path param
   */
  account_id: string;

  /**
   * Query param
   */
  enable?: boolean;

  /**
   * Query param
   */
  name?: string;

  /**
   * Query param: Search by id, name, filters
   */
  search?: string;
}

export interface DatasetDeleteParams {
  account_id: string;

  /**
   * Unique identifier of the AI Gateway within the account.
   */
  gateway_id: string;
}

export interface DatasetGetParams {
  account_id: string;

  /**
   * Unique identifier of the AI Gateway within the account.
   */
  gateway_id: string;
}

export declare namespace Datasets {
  export {
    type DatasetCreateResponse as DatasetCreateResponse,
    type DatasetUpdateResponse as DatasetUpdateResponse,
    type DatasetListResponse as DatasetListResponse,
    type DatasetDeleteResponse as DatasetDeleteResponse,
    type DatasetGetResponse as DatasetGetResponse,
    type DatasetListResponsesV4PagePaginationArray as DatasetListResponsesV4PagePaginationArray,
    type DatasetCreateParams as DatasetCreateParams,
    type DatasetUpdateParams as DatasetUpdateParams,
    type DatasetListParams as DatasetListParams,
    type DatasetDeleteParams as DatasetDeleteParams,
    type DatasetGetParams as DatasetGetParams,
  };
}
