// 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 { RequestOptions } from '../../../internal/request-options';
import { path } from '../../../internal/utils/path';

export class BaseTail extends APIResource {
  static override readonly _key: readonly ['workers', 'scripts', 'tail'] = Object.freeze([
    'workers',
    'scripts',
    'tail',
  ] as const);

  /**
   * Starts a tail that receives logs and exceptions from a Worker.
   *
   * @example
   * ```ts
   * const tail = await client.workers.scripts.tail.create(
   *   'this-is_my_script-01',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  create(
    scriptName: string,
    params: TailCreateParams,
    options?: RequestOptions,
  ): APIPromise<TailCreateResponse> {
    const { account_id } = params;
    return (
      this._client.post(
        path`/accounts/${account_id}/workers/scripts/${scriptName}/tails`,
        options,
      ) as APIPromise<{ result: TailCreateResponse }>
    )._thenUnwrap((obj) => obj.result);
  }

  /**
   * Deletes a tail from a Worker.
   *
   * @example
   * ```ts
   * const tail = await client.workers.scripts.tail.delete(
   *   '023e105f4ecef8ad9ca31a8372d0c353',
   *   {
   *     account_id: '023e105f4ecef8ad9ca31a8372d0c353',
   *     script_name: 'this-is_my_script-01',
   *   },
   * );
   * ```
   */
  delete(id: string, params: TailDeleteParams, options?: RequestOptions): APIPromise<TailDeleteResponse> {
    const { account_id, script_name } = params;
    return this._client.delete(
      path`/accounts/${account_id}/workers/scripts/${script_name}/tails/${id}`,
      options,
    );
  }

  /**
   * Get a list of tails currently deployed on a Worker.
   *
   * @example
   * ```ts
   * const tail = await client.workers.scripts.tail.get(
   *   'this-is_my_script-01',
   *   { account_id: '023e105f4ecef8ad9ca31a8372d0c353' },
   * );
   * ```
   */
  get(scriptName: string, params: TailGetParams, options?: RequestOptions): APIPromise<TailGetResponse> {
    const { account_id } = params;
    return (
      this._client.get(
        path`/accounts/${account_id}/workers/scripts/${scriptName}/tails`,
        options,
      ) as APIPromise<{ result: TailGetResponse }>
    )._thenUnwrap((obj) => obj.result);
  }
}
export class Tail extends BaseTail {}

/**
 * A reference to a script that will consume logs from the attached Worker.
 */
export interface ConsumerScript {
  /**
   * Name of Worker that is to be the consumer.
   */
  service: string;

  /**
   * Optional environment if the Worker utilizes one.
   */
  environment?: string;

  /**
   * Optional dispatch namespace the script belongs to.
   */
  namespace?: string;
}

/**
 * A reference to a script that will consume logs from the attached Worker.
 */
export interface ConsumerScriptParam {
  /**
   * Name of Worker that is to be the consumer.
   */
  service: string;

  /**
   * Optional environment if the Worker utilizes one.
   */
  environment?: string;

  /**
   * Optional dispatch namespace the script belongs to.
   */
  namespace?: string;
}

export interface TailCreateResponse {
  /**
   * Identifier.
   */
  id: string;

  expires_at: string;

  url: string;
}

export interface TailDeleteResponse {
  errors: Array<TailDeleteResponse.Error>;

  messages: Array<TailDeleteResponse.Message>;

  /**
   * Whether the API call was successful.
   */
  success: true;
}

export namespace TailDeleteResponse {
  export interface Error {
    code: number;

    message: string;

    documentation_url?: string;

    source?: Error.Source;
  }

  export namespace Error {
    export interface Source {
      pointer?: string;
    }
  }

  export interface Message {
    code: number;

    message: string;

    documentation_url?: string;

    source?: Message.Source;
  }

  export namespace Message {
    export interface Source {
      pointer?: string;
    }
  }
}

export interface TailGetResponse {
  /**
   * Identifier.
   */
  id: string;

  expires_at: string;

  url: string;
}

export interface TailCreateParams {
  /**
   * Identifier.
   */
  account_id: string;
}

export interface TailDeleteParams {
  /**
   * Identifier.
   */
  account_id: string;

  /**
   * Name of the script.
   */
  script_name: string;
}

export interface TailGetParams {
  /**
   * Identifier.
   */
  account_id: string;
}

export declare namespace Tail {
  export {
    type ConsumerScript as ConsumerScript,
    type TailCreateResponse as TailCreateResponse,
    type TailDeleteResponse as TailDeleteResponse,
    type TailGetResponse as TailGetResponse,
    type TailCreateParams as TailCreateParams,
    type TailDeleteParams as TailDeleteParams,
    type TailGetParams as TailGetParams,
  };
}
