import { mutationOptions } from '@tanstack/react-query';
import { getQueryClient } from '@/lib/query-client';
import { invalidatePermissionData } from '@/lib/query-invalidation';
import {
  activatePermissionTemplate,
  clonePermissionTemplate,
  createPermissionTemplate,
  createUserPermissionOverride,
  deactivatePermissionTemplate,
  deletePermissionTemplate,
  deleteUserPermissionOverride,
  saveUserPermissionAssignment,
  updatePermissionTemplate,
  updateUserPermissionOverride
} from './service';
import { permissionManagementKeys } from './queries';
import type {
  PermissionTemplateMutationPayload,
  UserPermissionAssignmentMutationPayload,
  UserPermissionOverrideMutationPayload
} from './types';

export const createPermissionTemplateMutation = mutationOptions({
  mutationFn: (data: PermissionTemplateMutationPayload) => createPermissionTemplate(data),
  onSuccess: () => {
    return invalidatePermissionData();
  }
});

export const updatePermissionTemplateMutation = mutationOptions({
  mutationFn: ({ id, values }: { id: number; values: PermissionTemplateMutationPayload }) =>
    updatePermissionTemplate(id, values),
  onSuccess: () => {
    return invalidatePermissionData();
  }
});

export const deletePermissionTemplateMutation = mutationOptions({
  mutationFn: ({ id, reason }: { id: number; reason?: string }) =>
    deletePermissionTemplate(id, reason),
  onSuccess: () => {
    return invalidatePermissionData();
  }
});

export const clonePermissionTemplateMutation = mutationOptions({
  mutationFn: ({ id, reason }: { id: number; reason?: string }) => clonePermissionTemplate(id, reason),
  onSuccess: () => {
    return invalidatePermissionData();
  }
});

export const activatePermissionTemplateMutation = mutationOptions({
  mutationFn: ({ id, reason }: { id: number; reason?: string }) =>
    activatePermissionTemplate(id, reason),
  onSuccess: () => {
    return invalidatePermissionData();
  }
});

export const deactivatePermissionTemplateMutation = mutationOptions({
  mutationFn: ({ id, reason }: { id: number; reason?: string }) =>
    deactivatePermissionTemplate(id, reason),
  onSuccess: () => {
    return invalidatePermissionData();
  }
});

export const saveUserPermissionAssignmentMutation = mutationOptions({
  mutationFn: ({
    userId,
    values
  }: {
    userId: string;
    values: UserPermissionAssignmentMutationPayload;
  }) => saveUserPermissionAssignment(userId, values),
  onSuccess: (_, variables) => {
    const queryClient = getQueryClient();
    return Promise.all([
      invalidatePermissionData(queryClient),
      queryClient.invalidateQueries({
        queryKey: permissionManagementKeys.userDetail(variables.userId)
      })
    ]);
  }
});

export const createUserPermissionOverrideMutation = mutationOptions({
  mutationFn: ({
    userId,
    values
  }: {
    userId: string;
    values: UserPermissionOverrideMutationPayload;
  }) => createUserPermissionOverride(userId, values),
  onSuccess: (_, variables) => {
    const queryClient = getQueryClient();
    return Promise.all([
      invalidatePermissionData(queryClient),
      queryClient.invalidateQueries({
        queryKey: permissionManagementKeys.userDetail(variables.userId)
      })
    ]);
  }
});

export const updateUserPermissionOverrideMutation = mutationOptions({
  mutationFn: ({
    userId,
    overrideId,
    values
  }: {
    userId: string;
    overrideId: number;
    values: UserPermissionOverrideMutationPayload;
  }) => updateUserPermissionOverride(userId, overrideId, values),
  onSuccess: (_, variables) => {
    const queryClient = getQueryClient();
    return Promise.all([
      invalidatePermissionData(queryClient),
      queryClient.invalidateQueries({
        queryKey: permissionManagementKeys.userDetail(variables.userId)
      })
    ]);
  }
});

export const deleteUserPermissionOverrideMutation = mutationOptions({
  mutationFn: ({ userId, overrideId }: { userId: string; overrideId: number }) =>
    deleteUserPermissionOverride(userId, overrideId),
  onSuccess: (_, variables) => {
    const queryClient = getQueryClient();
    return Promise.all([
      invalidatePermissionData(queryClient),
      queryClient.invalidateQueries({
        queryKey: permissionManagementKeys.userDetail(variables.userId)
      })
    ]);
  }
});
