// This file is auto-generated by @hey-api/openapi-ts

import { type DefaultError, type MutationOptions, queryOptions } from '@tanstack/solid-query';

import { client } from '../client.gen';
import { type Options, serenAgentCapabilities, serenAgentDeleteManagedDeployment, serenAgentDeploy, serenAgentGetManagedDeployment, serenAgentListDeployments, serenAgentListManagedDeploymentRevisions, serenAgentPatchManagedDeploymentFiles, serenAgentPreviewManagedDeploymentRollback, serenAgentPreviewManagedDeploymentUpdate, serenAgentPrivateModels, serenAgentRollbackManagedDeployment, serenAgentStartManagedDeployment, serenAgentStopManagedDeployment, serenAgentTestRun, serenAgentUpdateManagedDeployment } from '../sdk.gen';
import type { SerenAgentCapabilitiesData, SerenAgentCapabilitiesResponse, SerenAgentDeleteManagedDeploymentData, SerenAgentDeleteManagedDeploymentResponse, SerenAgentDeployData, SerenAgentDeployResponse, SerenAgentGetManagedDeploymentData, SerenAgentGetManagedDeploymentResponse, SerenAgentListDeploymentsData, SerenAgentListDeploymentsResponse, SerenAgentListManagedDeploymentRevisionsData, SerenAgentListManagedDeploymentRevisionsResponse, SerenAgentPatchManagedDeploymentFilesData, SerenAgentPatchManagedDeploymentFilesResponse, SerenAgentPreviewManagedDeploymentRollbackData, SerenAgentPreviewManagedDeploymentRollbackResponse, SerenAgentPreviewManagedDeploymentUpdateData, SerenAgentPreviewManagedDeploymentUpdateResponse, SerenAgentPrivateModelsData, SerenAgentPrivateModelsResponse, SerenAgentRollbackManagedDeploymentData, SerenAgentRollbackManagedDeploymentResponse, SerenAgentStartManagedDeploymentData, SerenAgentStartManagedDeploymentResponse, SerenAgentStopManagedDeploymentData, SerenAgentStopManagedDeploymentResponse, SerenAgentTestRunData, SerenAgentTestRunResponse, SerenAgentUpdateManagedDeploymentData, SerenAgentUpdateManagedDeploymentResponse } from '../types.gen';

export type QueryKey<TOptions extends Options> = [
    Pick<TOptions, 'baseUrl' | 'body' | 'headers' | 'path' | 'query'> & {
        _id: string;
        _infinite?: boolean;
        tags?: ReadonlyArray<string>;
    }
];

const createQueryKey = <TOptions extends Options>(id: string, options?: TOptions, infinite?: boolean, tags?: ReadonlyArray<string>): [
    QueryKey<TOptions>[0]
] => {
    const params: QueryKey<TOptions>[0] = { _id: id, baseUrl: options?.baseUrl || (options?.client ?? client).getConfig().baseUrl } as QueryKey<TOptions>[0];
    if (infinite) {
        params._infinite = infinite;
    }
    if (tags) {
        params.tags = tags;
    }
    if (options?.body) {
        params.body = options.body;
    }
    if (options?.headers) {
        params.headers = options.headers;
    }
    if (options?.path) {
        params.path = options.path;
    }
    if (options?.query) {
        params.query = options.query;
    }
    return [params];
};

export const serenAgentCapabilitiesQueryKey = (options?: Options<SerenAgentCapabilitiesData>) => createQueryKey('serenAgentCapabilities', options);

/**
 * Inspect available orchestration features and runtime limits.
 */
export const serenAgentCapabilitiesOptions = (options?: Options<SerenAgentCapabilitiesData>) => queryOptions<SerenAgentCapabilitiesResponse, DefaultError, SerenAgentCapabilitiesResponse, ReturnType<typeof serenAgentCapabilitiesQueryKey>>({
    queryFn: async ({ queryKey, signal }) => {
        const { data } = await serenAgentCapabilities({
            ...options,
            ...queryKey[0],
            signal,
            throwOnError: true
        });
        return data;
    },
    queryKey: serenAgentCapabilitiesQueryKey(options)
});

/**
 * Create or update an orchestrated deployment (forwarded to seren-cloud).
 */
export const serenAgentDeployMutation = (options?: Partial<Options<SerenAgentDeployData>>): MutationOptions<SerenAgentDeployResponse, DefaultError, Options<SerenAgentDeployData>> => {
    const mutationOptions: MutationOptions<SerenAgentDeployResponse, DefaultError, Options<SerenAgentDeployData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentDeploy({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

export const serenAgentListDeploymentsQueryKey = (options?: Options<SerenAgentListDeploymentsData>) => createQueryKey('serenAgentListDeployments', options);

/**
 * List orchestrated deployments and their runtime metadata.
 */
export const serenAgentListDeploymentsOptions = (options?: Options<SerenAgentListDeploymentsData>) => queryOptions<SerenAgentListDeploymentsResponse, DefaultError, SerenAgentListDeploymentsResponse, ReturnType<typeof serenAgentListDeploymentsQueryKey>>({
    queryFn: async ({ queryKey, signal }) => {
        const { data } = await serenAgentListDeployments({
            ...options,
            ...queryKey[0],
            signal,
            throwOnError: true
        });
        return data;
    },
    queryKey: serenAgentListDeploymentsQueryKey(options)
});

/**
 * Delete a managed seren-agent deployment and clean up runtime resources.
 */
export const serenAgentDeleteManagedDeploymentMutation = (options?: Partial<Options<SerenAgentDeleteManagedDeploymentData>>): MutationOptions<SerenAgentDeleteManagedDeploymentResponse, DefaultError, Options<SerenAgentDeleteManagedDeploymentData>> => {
    const mutationOptions: MutationOptions<SerenAgentDeleteManagedDeploymentResponse, DefaultError, Options<SerenAgentDeleteManagedDeploymentData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentDeleteManagedDeployment({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

export const serenAgentGetManagedDeploymentQueryKey = (options: Options<SerenAgentGetManagedDeploymentData>) => createQueryKey('serenAgentGetManagedDeployment', options);

/**
 * Get the resolved managed spec for a seren-agent deployment.
 */
export const serenAgentGetManagedDeploymentOptions = (options: Options<SerenAgentGetManagedDeploymentData>) => queryOptions<SerenAgentGetManagedDeploymentResponse, DefaultError, SerenAgentGetManagedDeploymentResponse, ReturnType<typeof serenAgentGetManagedDeploymentQueryKey>>({
    queryFn: async ({ queryKey, signal }) => {
        const { data } = await serenAgentGetManagedDeployment({
            ...options,
            ...queryKey[0],
            signal,
            throwOnError: true
        });
        return data;
    },
    queryKey: serenAgentGetManagedDeploymentQueryKey(options)
});

/**
 * Update the editable managed spec for a seren-agent deployment.
 */
export const serenAgentUpdateManagedDeploymentMutation = (options?: Partial<Options<SerenAgentUpdateManagedDeploymentData>>): MutationOptions<SerenAgentUpdateManagedDeploymentResponse, DefaultError, Options<SerenAgentUpdateManagedDeploymentData>> => {
    const mutationOptions: MutationOptions<SerenAgentUpdateManagedDeploymentResponse, DefaultError, Options<SerenAgentUpdateManagedDeploymentData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentUpdateManagedDeployment({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

/**
 * Update files for a managed deployment.
 */
export const serenAgentPatchManagedDeploymentFilesMutation = (options?: Partial<Options<SerenAgentPatchManagedDeploymentFilesData>>): MutationOptions<SerenAgentPatchManagedDeploymentFilesResponse, DefaultError, Options<SerenAgentPatchManagedDeploymentFilesData>> => {
    const mutationOptions: MutationOptions<SerenAgentPatchManagedDeploymentFilesResponse, DefaultError, Options<SerenAgentPatchManagedDeploymentFilesData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentPatchManagedDeploymentFiles({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

/**
 * Preview a managed update before applying it.
 */
export const serenAgentPreviewManagedDeploymentUpdateMutation = (options?: Partial<Options<SerenAgentPreviewManagedDeploymentUpdateData>>): MutationOptions<SerenAgentPreviewManagedDeploymentUpdateResponse, DefaultError, Options<SerenAgentPreviewManagedDeploymentUpdateData>> => {
    const mutationOptions: MutationOptions<SerenAgentPreviewManagedDeploymentUpdateResponse, DefaultError, Options<SerenAgentPreviewManagedDeploymentUpdateData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentPreviewManagedDeploymentUpdate({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

export const serenAgentListManagedDeploymentRevisionsQueryKey = (options: Options<SerenAgentListManagedDeploymentRevisionsData>) => createQueryKey('serenAgentListManagedDeploymentRevisions', options);

/**
 * List immutable revision snapshots for a managed deployment.
 */
export const serenAgentListManagedDeploymentRevisionsOptions = (options: Options<SerenAgentListManagedDeploymentRevisionsData>) => queryOptions<SerenAgentListManagedDeploymentRevisionsResponse, DefaultError, SerenAgentListManagedDeploymentRevisionsResponse, ReturnType<typeof serenAgentListManagedDeploymentRevisionsQueryKey>>({
    queryFn: async ({ queryKey, signal }) => {
        const { data } = await serenAgentListManagedDeploymentRevisions({
            ...options,
            ...queryKey[0],
            signal,
            throwOnError: true
        });
        return data;
    },
    queryKey: serenAgentListManagedDeploymentRevisionsQueryKey(options)
});

/**
 * Roll a managed deployment back to a prior revision.
 */
export const serenAgentRollbackManagedDeploymentMutation = (options?: Partial<Options<SerenAgentRollbackManagedDeploymentData>>): MutationOptions<SerenAgentRollbackManagedDeploymentResponse, DefaultError, Options<SerenAgentRollbackManagedDeploymentData>> => {
    const mutationOptions: MutationOptions<SerenAgentRollbackManagedDeploymentResponse, DefaultError, Options<SerenAgentRollbackManagedDeploymentData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentRollbackManagedDeployment({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

/**
 * Preview rolling a managed deployment back to a prior revision.
 */
export const serenAgentPreviewManagedDeploymentRollbackMutation = (options?: Partial<Options<SerenAgentPreviewManagedDeploymentRollbackData>>): MutationOptions<SerenAgentPreviewManagedDeploymentRollbackResponse, DefaultError, Options<SerenAgentPreviewManagedDeploymentRollbackData>> => {
    const mutationOptions: MutationOptions<SerenAgentPreviewManagedDeploymentRollbackResponse, DefaultError, Options<SerenAgentPreviewManagedDeploymentRollbackData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentPreviewManagedDeploymentRollback({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

/**
 * Start a managed seren-agent deployment.
 */
export const serenAgentStartManagedDeploymentMutation = (options?: Partial<Options<SerenAgentStartManagedDeploymentData>>): MutationOptions<SerenAgentStartManagedDeploymentResponse, DefaultError, Options<SerenAgentStartManagedDeploymentData>> => {
    const mutationOptions: MutationOptions<SerenAgentStartManagedDeploymentResponse, DefaultError, Options<SerenAgentStartManagedDeploymentData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentStartManagedDeployment({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

/**
 * Stop a managed seren-agent deployment.
 */
export const serenAgentStopManagedDeploymentMutation = (options?: Partial<Options<SerenAgentStopManagedDeploymentData>>): MutationOptions<SerenAgentStopManagedDeploymentResponse, DefaultError, Options<SerenAgentStopManagedDeploymentData>> => {
    const mutationOptions: MutationOptions<SerenAgentStopManagedDeploymentResponse, DefaultError, Options<SerenAgentStopManagedDeploymentData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentStopManagedDeployment({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};

export const serenAgentPrivateModelsQueryKey = (options?: Options<SerenAgentPrivateModelsData>) => createQueryKey('serenAgentPrivateModels', options);

/**
 * List the private model catalog available through seren-private-models.
 */
export const serenAgentPrivateModelsOptions = (options?: Options<SerenAgentPrivateModelsData>) => queryOptions<SerenAgentPrivateModelsResponse, DefaultError, SerenAgentPrivateModelsResponse, ReturnType<typeof serenAgentPrivateModelsQueryKey>>({
    queryFn: async ({ queryKey, signal }) => {
        const { data } = await serenAgentPrivateModels({
            ...options,
            ...queryKey[0],
            signal,
            throwOnError: true
        });
        return data;
    },
    queryKey: serenAgentPrivateModelsQueryKey(options)
});

/**
 * Run an unsaved managed seren-agent draft once before deploying.
 */
export const serenAgentTestRunMutation = (options?: Partial<Options<SerenAgentTestRunData>>): MutationOptions<SerenAgentTestRunResponse, DefaultError, Options<SerenAgentTestRunData>> => {
    const mutationOptions: MutationOptions<SerenAgentTestRunResponse, DefaultError, Options<SerenAgentTestRunData>> = {
        mutationFn: async (fnOptions) => {
            const { data } = await serenAgentTestRun({
                ...options,
                ...fnOptions,
                throwOnError: true
            });
            return data;
        }
    };
    return mutationOptions;
};
