// 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 InsightClient {
    export type Options = BaseClientOptions;

    export interface RequestOptions extends BaseRequestOptions {}
}

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

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

    /**
     * @param {Vapi.InsightControllerFindAllRequest} request
     * @param {InsightClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.insight.insightControllerFindAll()
     */
    public insightControllerFindAll(
        request: Vapi.InsightControllerFindAllRequest = {},
        requestOptions?: InsightClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.InsightPaginatedResponse> {
        return core.HttpResponsePromise.fromPromise(this.__insightControllerFindAll(request, requestOptions));
    }

    private async __insightControllerFindAll(
        request: Vapi.InsightControllerFindAllRequest = {},
        requestOptions?: InsightClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.InsightPaginatedResponse>> {
        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,
                "reporting/insight",
            ),
            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.InsightPaginatedResponse, 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", "/reporting/insight");
    }

    /**
     * @param {Vapi.InsightControllerCreateRequest} request
     * @param {InsightClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.insight.insightControllerCreate({
     *         type: "bar",
     *         queries: [{
     *                 type: "vapiql-json",
     *                 table: "call",
     *                 column: "id",
     *                 operation: "count"
     *             }]
     *     })
     */
    public insightControllerCreate(
        request: Vapi.InsightControllerCreateRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.InsightControllerCreateResponse> {
        return core.HttpResponsePromise.fromPromise(this.__insightControllerCreate(request, requestOptions));
    }

    private async __insightControllerCreate(
        request: Vapi.InsightControllerCreateRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.InsightControllerCreateResponse>> {
        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,
                "reporting/insight",
            ),
            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.InsightControllerCreateResponse, 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", "/reporting/insight");
    }

    /**
     * @param {Vapi.InsightControllerFindOneRequest} request
     * @param {InsightClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.insight.insightControllerFindOne({
     *         id: "id"
     *     })
     */
    public insightControllerFindOne(
        request: Vapi.InsightControllerFindOneRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.InsightControllerFindOneResponse> {
        return core.HttpResponsePromise.fromPromise(this.__insightControllerFindOne(request, requestOptions));
    }

    private async __insightControllerFindOne(
        request: Vapi.InsightControllerFindOneRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.InsightControllerFindOneResponse>> {
        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,
                `reporting/insight/${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.InsightControllerFindOneResponse,
                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", "/reporting/insight/{id}");
    }

    /**
     * @param {Vapi.InsightControllerRemoveRequest} request
     * @param {InsightClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.insight.insightControllerRemove({
     *         id: "id"
     *     })
     */
    public insightControllerRemove(
        request: Vapi.InsightControllerRemoveRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.InsightControllerRemoveResponse> {
        return core.HttpResponsePromise.fromPromise(this.__insightControllerRemove(request, requestOptions));
    }

    private async __insightControllerRemove(
        request: Vapi.InsightControllerRemoveRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.InsightControllerRemoveResponse>> {
        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,
                `reporting/insight/${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.InsightControllerRemoveResponse, 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", "/reporting/insight/{id}");
    }

    /**
     * @param {Vapi.InsightControllerUpdateRequest} request
     * @param {InsightClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.insight.insightControllerUpdate({
     *         id: "id",
     *         body: {
     *             type: "bar"
     *         }
     *     })
     */
    public insightControllerUpdate(
        request: Vapi.InsightControllerUpdateRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.InsightControllerUpdateResponse> {
        return core.HttpResponsePromise.fromPromise(this.__insightControllerUpdate(request, requestOptions));
    }

    private async __insightControllerUpdate(
        request: Vapi.InsightControllerUpdateRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.InsightControllerUpdateResponse>> {
        const { id, body: _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,
                `reporting/insight/${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.InsightControllerUpdateResponse, 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", "/reporting/insight/{id}");
    }

    /**
     * @param {Vapi.InsightRunDto} request
     * @param {InsightClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.insight.insightControllerRun({
     *         id: "id"
     *     })
     */
    public insightControllerRun(
        request: Vapi.InsightRunDto,
        requestOptions?: InsightClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.InsightRunResponse> {
        return core.HttpResponsePromise.fromPromise(this.__insightControllerRun(request, requestOptions));
    }

    private async __insightControllerRun(
        request: Vapi.InsightRunDto,
        requestOptions?: InsightClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.InsightRunResponse>> {
        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,
                `reporting/insight/${core.url.encodePathParam(id)}/run`,
            ),
            method: "POST",
            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.InsightRunResponse, 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", "/reporting/insight/{id}/run");
    }

    /**
     * @param {Vapi.InsightControllerPreviewRequest} request
     * @param {InsightClient.RequestOptions} requestOptions - Request-specific configuration.
     *
     * @example
     *     await client.insight.insightControllerPreview({
     *         type: "bar",
     *         queries: [{
     *                 type: "vapiql-json",
     *                 table: "call",
     *                 column: "id",
     *                 operation: "count"
     *             }]
     *     })
     */
    public insightControllerPreview(
        request: Vapi.InsightControllerPreviewRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): core.HttpResponsePromise<Vapi.InsightRunResponse> {
        return core.HttpResponsePromise.fromPromise(this.__insightControllerPreview(request, requestOptions));
    }

    private async __insightControllerPreview(
        request: Vapi.InsightControllerPreviewRequest,
        requestOptions?: InsightClient.RequestOptions,
    ): Promise<core.WithRawResponse<Vapi.InsightRunResponse>> {
        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,
                "reporting/insight/preview",
            ),
            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.InsightRunResponse, 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", "/reporting/insight/preview");
    }
}
