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

import { APIResource } from '../../../core/resource';
import * as ConfigAPI from '../config';
import * as ConfigsAPI from './configs';
import { BaseConfigs, ConfigGetParams, ConfigGetResponse, Configs } from './configs';
import { APIPromise } from '../../../core/api-promise';
import { PagePromise, SinglePage } from '../../../core/pagination';
import { RequestOptions } from '../../../internal/request-options';
import { path } from '../../../internal/utils/path';

export class BaseHistory extends APIResource {
  static override readonly _key: readonly ['zaraz', 'history'] = Object.freeze(['zaraz', 'history'] as const);

  /**
   * Restores a historical published Zaraz configuration by ID for a zone.
   *
   * @example
   * ```ts
   * const configuration = await client.zaraz.history.update({
   *   zone_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *   body: 12345,
   * });
   * ```
   */
  update(params: HistoryUpdateParams, options?: RequestOptions): APIPromise<ConfigAPI.Configuration> {
    const { zone_id, body } = params;
    return (
      this._client.put(path`/zones/${zone_id}/settings/zaraz/history`, {
        body: body,
        ...options,
      }) as APIPromise<{ result: ConfigAPI.Configuration }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Lists a history of published Zaraz configuration records for a zone.
   *
   * @example
   * ```ts
   * // Automatically fetches more pages as needed.
   * for await (const historyListResponse of client.zaraz.history.list(
   *   { zone_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * )) {
   *   // ...
   * }
   * ```
   */
  list(
    params: HistoryListParams,
    options?: RequestOptions,
  ): PagePromise<HistoryListResponsesSinglePage, HistoryListResponse> {
    const { zone_id, ...query } = params;
    return this._client.getAPIList(
      path`/zones/${zone_id}/settings/zaraz/history`,
      SinglePage<HistoryListResponse>,
      { query, ...options },
    );
  }
}
export class History extends BaseHistory {
  configs: ConfigsAPI.Configs = new ConfigsAPI.Configs(this._client);
}

export type HistoryListResponsesSinglePage = SinglePage<HistoryListResponse>;

export interface HistoryListResponse {
  /**
   * ID of the configuration.
   */
  id: number;

  /**
   * Date and time the configuration was created.
   */
  createdAt: string;

  /**
   * Configuration description provided by the user who published this configuration.
   */
  description: string;

  /**
   * Date and time the configuration was last updated.
   */
  updatedAt: string;

  /**
   * Alpha-numeric ID of the account user who published the configuration.
   */
  userId: string;
}

export interface HistoryUpdateParams {
  /**
   * Path param: Identifier.
   */
  zone_id: string;

  /**
   * Body param: ID of the Zaraz configuration to restore.
   */
  body: number;
}

export interface HistoryListParams {
  /**
   * Path param: Identifier.
   */
  zone_id: string;

  /**
   * Query param: Maximum amount of results to list. Default value is 10.
   */
  limit?: number;

  /**
   * Query param: Ordinal number to start listing the results with. Default value
   * is 0.
   */
  offset?: number;

  /**
   * Query param: The field to sort by. Default is updated_at.
   */
  sortField?: 'id' | 'user_id' | 'description' | 'created_at' | 'updated_at';

  /**
   * Query param: Sorting order. Default is DESC.
   */
  sortOrder?: 'DESC' | 'ASC';
}

History.Configs = Configs;
History.BaseConfigs = BaseConfigs;

export declare namespace History {
  export {
    type HistoryListResponse as HistoryListResponse,
    type HistoryListResponsesSinglePage as HistoryListResponsesSinglePage,
    type HistoryUpdateParams as HistoryUpdateParams,
    type HistoryListParams as HistoryListParams,
  };

  export {
    Configs as Configs,
    BaseConfigs as BaseConfigs,
    type ConfigGetResponse as ConfigGetResponse,
    type ConfigGetParams as ConfigGetParams,
  };
}
