CREATE OR REPLACE FUNCTION medreport.get_order_possible_actions(p_medusa_order_id text)
RETURNS jsonb
LANGUAGE plpgsql
AS $$
DECLARE
  order_status text;
  is_queued boolean;
BEGIN
  -- Get the analysis order status
  SELECT status INTO order_status 
  FROM medreport.analysis_orders 
  WHERE medusa_order_id = p_medusa_order_id;
  
  -- Check if status is QUEUED
  is_queued := (order_status = 'QUEUED');
  
  -- Return JSON object with actions and their allowed status
  RETURN jsonb_build_object(
    'retry_dispatch', is_queued,
    'mark_as_not_received_by_synlab', is_queued
  );
END;
$$;

grant execute on function medreport.get_order_possible_actions(text) to service_role;
