export function useUnitPermission() {
  const { hasPermission } = usePermission();
  const { permissions } = useCleaningPermission();

  const { userAppRole } = useLocalSetup();

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

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

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

  const canViewUnitDetails = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(userAppRole.value, permissions, "unit-mgmt", "see-unit-details");
  });

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

  const canImportUnit = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(userAppRole.value, permissions, "unit-mgmt", "import-units");
  });

  return {
    canViewUnits,
    canCreateUnit,
    canUpdateUnit,
    canViewUnitDetails,
    canDeleteUnit,
    canImportUnit,
  };
}
