import { QUERY_KEYS } from "@/utils/query-key";
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import {
  getPermissions,
  getPermission,
  createPermission,
  updatePermission,
  deletePermission,
} from "./_requests";
import {
  CreatePermissionRequest,
  UpdatePermissionRequest,
  PermissionFilters,
} from "./_models";
import toast from "react-hot-toast";

export const usePermissions = (filters?: PermissionFilters) => {
  return useQuery({
    queryKey: [QUERY_KEYS.GET_PERMISSIONS, filters],
    queryFn: () => getPermissions(filters),
  });
};

export const usePermission = (id: string, enabled: boolean = true) => {
  return useQuery({
    queryKey: [QUERY_KEYS.GET_PERMISSION, id],
    queryFn: () => getPermission(id),
    enabled: enabled && !!id,
  });
};

export const useCreatePermission = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationKey: [QUERY_KEYS.CREATE_PERMISSION],
    mutationFn: (permissionData: CreatePermissionRequest) =>
      createPermission(permissionData),
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET_PERMISSIONS] });
      // Also invalidate roles permissions as they might be affected
      queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET_ROLE_PERMISSIONS],
      });
      toast.success("دسترسی با موفقیت ایجاد شد");
    },
    onError: (error: any) => {
      console.error("Create permission error:", error);
      toast.error(error?.message || "خطا در ایجاد دسترسی");
    },
  });
};

export const useUpdatePermission = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationKey: [QUERY_KEYS.UPDATE_PERMISSION],
    mutationFn: ({
      id,
      permissionData,
    }: {
      id: string;
      permissionData: UpdatePermissionRequest;
    }) => updatePermission(id, permissionData),
    onSuccess: (data, variables) => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET_PERMISSIONS] });
      queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET_PERMISSION, variables.id],
      });
      // Also invalidate roles permissions as they might be affected
      queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET_ROLE_PERMISSIONS],
      });
      toast.success("دسترسی با موفقیت به‌روزرسانی شد");
    },
    onError: (error: any) => {
      console.error("Update permission error:", error);
      toast.error(error?.message || "خطا در به‌روزرسانی دسترسی");
    },
  });
};

export const useDeletePermission = () => {
  const queryClient = useQueryClient();

  return useMutation({
    mutationKey: [QUERY_KEYS.DELETE_PERMISSION],
    mutationFn: (id: string) => deletePermission(id),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: [QUERY_KEYS.GET_PERMISSIONS] });
      // Also invalidate roles permissions as they might be affected
      queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET_ROLE_PERMISSIONS],
      });
      toast.success("دسترسی با موفقیت حذف شد");
    },
    onError: (error: any) => {
      console.error("Delete permission error:", error);
      toast.error(error?.message || "خطا در حذف دسترسی");
    },
  });
};
