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

  const canViewScheduleTasks = computed(() => {
    if (!userAppRole.value) return true;
    if (userAppRole.value.permissions.includes("*")) return true;
    return hasPermission(
      userAppRole.value,
      permissions,
      "schedule-task-mgmt",
      "see-all-schedule-tasks"
    );
  });

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

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

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

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

  return {
    canViewScheduleTasks,
    canCreateScheduleTask,
    canUpdateScheduleTask,
    canDeleteScheduleTask,
    canViewScheduleTaskDetails,
  };
}
