// This file was auto-generated by Fern from our API Definition.

import type { BaseClientOptions, BaseRequestOptions } from "../../../../BaseClient.js";
import { type NormalizedClientOptionsWithAuth, normalizeClientOptionsWithAuth } from "../../../../BaseClient.js";
import { mergeHeaders } from "../../../../core/headers.js";
import * as core from "../../../../core/index.js";
import * as environments from "../../../../environments.js";
import { handleNonStatusCodeError } from "../../../../errors/handleNonStatusCodeError.js";
import * as errors from "../../../../errors/index.js";
import type * as Vapi from "../../../index.js";

export declare namespace EvalClient {
    export type Options = BaseClientOptions;

    export interface RequestOptions extends BaseRequestOptions {}
}

export class EvalClient {
    protected readonly _options: NormalizedClientOptionsWithAuth<EvalClient.Options>;

    constructor(options: EvalClient.Options) {
        this._options = normalizeClientOptionsWithAuth(options);
    }

    /**
     * @param {Vapi.EvalControllerGetPaginatedRequest} request
     * @param {EvalClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.eval.evalControllerGetPaginated()
     */
    public evalControllerGetPaginated(
        request: Vapi.EvalControllerGetPaginatedRequest = {},
        requestOptions?: EvalClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.EvalPaginatedResponse> {
        return core.HttpResponsePromise.fromPromise(this.__evalControllerGetPaginated(request, requestOptions));
    }

    private async __evalControllerGetPaginated(
        request: Vapi.EvalControllerGetPaginatedRequest = {},
        requestOptions?: EvalClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.EvalPaginatedResponse>> {
        const {
            id,
            page,
            sortOrder,
            limit,
            createdAtGt,
            createdAtLt,
            createdAtGe,
            createdAtLe,
            updatedAtGt,
            updatedAtLt,
            updatedAtGe,
            updatedAtLe,
        } = request;
        const _queryParams: Record<string, unknown> = {
            id,
            page,
            sortOrder: sortOrder != null ? sortOrder : undefined,
            limit,
            createdAtGt: createdAtGt != null ? createdAtGt : undefined,
            createdAtLt: createdAtLt != null ? createdAtLt : undefined,
            createdAtGe: createdAtGe != null ? createdAtGe : undefined,
            createdAtLe: createdAtLe != null ? createdAtLe : undefined,
            updatedAtGt: updatedAtGt != null ? updatedAtGt : undefined,
            updatedAtLt: updatedAtLt != null ? updatedAtLt : undefined,
            updatedAtGe: updatedAtGe != null ? updatedAtGe : undefined,
            updatedAtLe: updatedAtLe != null ? updatedAtLe : undefined,
        };
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                "eval",
            ),
            method: "GET",
            headers: _headers,
            queryParameters: { ..._queryParams, ...requestOptions?.queryParams },
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.EvalPaginatedResponse, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/eval");
    }

    /**
     * @param {Vapi.CreateEvalDto} request
     * @param {EvalClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.eval.evalControllerCreate({
     *         messages: [{
     *                 role: "assistant"
     *             }],
     *         type: "chat.mockConversation"
     *     })
     */
    public evalControllerCreate(
        request: Vapi.CreateEvalDto,
        requestOptions?: EvalClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.Eval> {
        return core.HttpResponsePromise.fromPromise(this.__evalControllerCreate(request, requestOptions));
    }

    private async __evalControllerCreate(
        request: Vapi.CreateEvalDto,
        requestOptions?: EvalClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.Eval>> {
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                "eval",
            ),
            method: "POST",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: request,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.Eval, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/eval");
    }

    /**
     * @param {Vapi.EvalControllerGetRequest} request
     * @param {EvalClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.eval.evalControllerGet({
     *         id: "id"
     *     })
     */
    public evalControllerGet(
        request: Vapi.EvalControllerGetRequest,
        requestOptions?: EvalClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.Eval> {
        return core.HttpResponsePromise.fromPromise(this.__evalControllerGet(request, requestOptions));
    }

    private async __evalControllerGet(
        request: Vapi.EvalControllerGetRequest,
        requestOptions?: EvalClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.Eval>> {
        const { id } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                `eval/${core.url.encodePathParam(id)}`,
            ),
            method: "GET",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.Eval, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/eval/{id}");
    }

    /**
     * @param {Vapi.EvalControllerRemoveRequest} request
     * @param {EvalClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.eval.evalControllerRemove({
     *         id: "id"
     *     })
     */
    public evalControllerRemove(
        request: Vapi.EvalControllerRemoveRequest,
        requestOptions?: EvalClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.Eval> {
        return core.HttpResponsePromise.fromPromise(this.__evalControllerRemove(request, requestOptions));
    }

    private async __evalControllerRemove(
        request: Vapi.EvalControllerRemoveRequest,
        requestOptions?: EvalClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.Eval>> {
        const { id } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                `eval/${core.url.encodePathParam(id)}`,
            ),
            method: "DELETE",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.Eval, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "DELETE", "/eval/{id}");
    }

    /**
     * @param {Vapi.UpdateEvalDto} request
     * @param {EvalClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.eval.evalControllerUpdate({
     *         id: "id"
     *     })
     */
    public evalControllerUpdate(
        request: Vapi.UpdateEvalDto,
        requestOptions?: EvalClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.Eval> {
        return core.HttpResponsePromise.fromPromise(this.__evalControllerUpdate(request, requestOptions));
    }

    private async __evalControllerUpdate(
        request: Vapi.UpdateEvalDto,
        requestOptions?: EvalClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.Eval>> {
        const { id, ..._body } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                `eval/${core.url.encodePathParam(id)}`,
            ),
            method: "PATCH",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: _body,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.Eval, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "PATCH", "/eval/{id}");
    }

    /**
     * @param {Vapi.EvalControllerGetRunRequest} request
     * @param {EvalClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.eval.evalControllerGetRun({
     *         id: "id"
     *     })
     */
    public evalControllerGetRun(
        request: Vapi.EvalControllerGetRunRequest,
        requestOptions?: EvalClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.EvalRun> {
        return core.HttpResponsePromise.fromPromise(this.__evalControllerGetRun(request, requestOptions));
    }

    private async __evalControllerGetRun(
        request: Vapi.EvalControllerGetRunRequest,
        requestOptions?: EvalClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.EvalRun>> {
        const { id } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                `eval/run/${core.url.encodePathParam(id)}`,
            ),
            method: "GET",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.EvalRun, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/eval/run/{id}");
    }

    /**
     * @param {Vapi.EvalControllerRemoveRunRequest} request
     * @param {EvalClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.eval.evalControllerRemoveRun({
     *         id: "id"
     *     })
     */
    public evalControllerRemoveRun(
        request: Vapi.EvalControllerRemoveRunRequest,
        requestOptions?: EvalClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.EvalRun> {
        return core.HttpResponsePromise.fromPromise(this.__evalControllerRemoveRun(request, requestOptions));
    }

    private async __evalControllerRemoveRun(
        request: Vapi.EvalControllerRemoveRunRequest,
        requestOptions?: EvalClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.EvalRun>> {
        const { id } = request;
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                `eval/run/${core.url.encodePathParam(id)}`,
            ),
            method: "DELETE",
            headers: _headers,
            queryParameters: requestOptions?.queryParams,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.EvalRun, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "DELETE", "/eval/run/{id}");
    }

    /**
     * @param {Vapi.EvalControllerGetRunsPaginatedRequest} request
     * @param {EvalClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.eval.evalControllerGetRunsPaginated()
     */
    public evalControllerGetRunsPaginated(
        request: Vapi.EvalControllerGetRunsPaginatedRequest = {},
        requestOptions?: EvalClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.EvalRunPaginatedResponse> {
        return core.HttpResponsePromise.fromPromise(this.__evalControllerGetRunsPaginated(request, requestOptions));
    }

    private async __evalControllerGetRunsPaginated(
        request: Vapi.EvalControllerGetRunsPaginatedRequest = {},
        requestOptions?: EvalClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.EvalRunPaginatedResponse>> {
        const {
            id,
            page,
            sortOrder,
            limit,
            createdAtGt,
            createdAtLt,
            createdAtGe,
            createdAtLe,
            updatedAtGt,
            updatedAtLt,
            updatedAtGe,
            updatedAtLe,
        } = request;
        const _queryParams: Record<string, unknown> = {
            id,
            page,
            sortOrder: sortOrder != null ? sortOrder : undefined,
            limit,
            createdAtGt: createdAtGt != null ? createdAtGt : undefined,
            createdAtLt: createdAtLt != null ? createdAtLt : undefined,
            createdAtGe: createdAtGe != null ? createdAtGe : undefined,
            createdAtLe: createdAtLe != null ? createdAtLe : undefined,
            updatedAtGt: updatedAtGt != null ? updatedAtGt : undefined,
            updatedAtLt: updatedAtLt != null ? updatedAtLt : undefined,
            updatedAtGe: updatedAtGe != null ? updatedAtGe : undefined,
            updatedAtLe: updatedAtLe != null ? updatedAtLe : undefined,
        };
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                "eval/run",
            ),
            method: "GET",
            headers: _headers,
            queryParameters: { ..._queryParams, ...requestOptions?.queryParams },
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Vapi.EvalRunPaginatedResponse, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "GET", "/eval/run");
    }

    /**
     * @param {Vapi.CreateEvalRunDto} request
     * @param {EvalClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.eval.evalControllerRun({
     *         target: {
     *             type: "assistant"
     *         },
     *         type: "eval"
     *     })
     */
    public evalControllerRun(
        request: Vapi.CreateEvalRunDto,
        requestOptions?: EvalClient.RequestOptions,
    ): core.HttpResponsePromise<Record<string, unknown>> {
        return core.HttpResponsePromise.fromPromise(this.__evalControllerRun(request, requestOptions));
    }

    private async __evalControllerRun(
        request: Vapi.CreateEvalRunDto,
        requestOptions?: EvalClient.RequestOptions,
    ): Promise<core.WithRawResponse<Record<string, unknown>>> {
        const _authRequest: core.AuthRequest = await this._options.authProvider.getAuthRequest();
        const _headers: core.Fetcher.Args["headers"] = mergeHeaders(
            _authRequest.headers,
            this._options?.headers,
            requestOptions?.headers,
        );
        const _response = await (this._options.fetcher ?? core.fetcher)({
            url: core.url.join(
                (await core.Supplier.get(this._options.baseUrl)) ??
                    (await core.Supplier.get(this._options.environment)) ??
                    environments.VapiEnvironment.Default,
                "eval/run",
            ),
            method: "POST",
            headers: _headers,
            contentType: "application/json",
            queryParameters: requestOptions?.queryParams,
            requestType: "json",
            body: request,
            timeoutMs: (requestOptions?.timeoutInSeconds ?? this._options?.timeoutInSeconds ?? 60) * 1000,
            maxRetries: requestOptions?.maxRetries ?? this._options?.maxRetries,
            abortSignal: requestOptions?.abortSignal,
            fetchFn: this._options?.fetch,
            logging: this._options.logging,
        });
        if (_response.ok) {
            return { data: _response.body as Record<string, unknown>, rawResponse: _response.rawResponse };
        }

        if (_response.error.reason === "status-code") {
            throw new errors.VapiError({
                statusCode: _response.error.statusCode,
                body: _response.error.body,
                rawResponse: _response.rawResponse,
            });
        }

        return handleNonStatusCodeError(_response.error, _response.rawResponse, "POST", "/eval/run");
    }
}
