import { useCrudOperations } from "@/hooks/cruds/baseCrud";
import {
    CreateProjectSchema,
    ProjectSchema,
    UpdateProjectSchema,
} from "@/lib/client";
import {
    createProjectMutation,
    deleteProjectMutation,
    updateProjectMutation,
} from "@/lib/client/@tanstack/react-query.gen";

type Props = {
    queryKey: any[];
};

export type ProjectsCrud = {
    onCreate: (data: Partial<CreateProjectSchema>) => void;
    onUpdate: (projectId: number, project: UpdateProjectSchema) => void;
    onDelete: (project: ProjectSchema, onSuccess?: () => void) => void;
};

export const useProjectsCrud = ({ queryKey }: Props): ProjectsCrud => {
    return useCrudOperations<
        ProjectSchema,
        UpdateProjectSchema,
        CreateProjectSchema
    >({
        key: "getProjects",
        queryKey,
        mutations: {
            create: createProjectMutation(),
            update: updateProjectMutation(),
            delete: deleteProjectMutation(),
        },
        getCreateEntity: data => ({ name: data.name! }),
        getUpdateEntity: (old, update) => ({
            ...old,
            name: update.name ?? old.name,
        }),
        getDeleteConfirmTitle: () => "Удаление проекта",
    });
};
