import useCleaningPermission from "./useCleaningPermission";
import { useLocalSetup } from "./useLocalSetup";
import usePermission from "./usePermission";

export function useEquipmentManagementPermission() {
  const { hasPermission } = usePermission();
  const { permissions } = useCleaningPermission();
  const { userAppRole } = useLocalSetup();

  const canViewEquipments = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(
      userAppRole.value,
      permissions,
      "supply-mgmt",
      "see-all-supplies"
    );
  });

  const canAddEquipment = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(
      userAppRole.value,
      permissions,
      "supply-mgmt",
      "add-supply"
    );
  });

  const canUpdateEquipment = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(
      userAppRole.value,
      permissions,
      "supply-mgmt",
      "update-supply"
    );
  });

  const canDeleteEquipment = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(
      userAppRole.value,
      permissions,
      "supply-mgmt",
      "delete-supply"
    );
  });

  const canAddStock = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(
      userAppRole.value,
      permissions,
      "supply-mgmt",
      "add-stock"
    );
  });

  const canViewStock = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(
      userAppRole.value,
      permissions,
      "supply-mgmt",
      "view-stock"
    );
  });

  const canRequestItem = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(
      userAppRole.value,
      permissions,
      "supply-mgmt",
      "request-item"
    );
  });

  return {
    canViewEquipments,
    canAddEquipment,
    canUpdateEquipment,
    canDeleteEquipment,
    canAddStock,
    canViewStock,
    canRequestItem,
  };
}
