create or replace function medreport.medipost_retry_dispatch(
  order_id text
)
returns jsonb
language plpgsql
as $function$
declare
  response_result record;
begin
  select into response_result
    net.http_post(
      url := 'https://test.medreport.ee/api/job/medipost-retry-dispatch',
      headers := jsonb_build_object(
        'Content-Type', 'application/json',
        'x-jobs-api-key', 'fd26ec26-70ed-11f0-9e95-431ac3b15a84'
      ),
      body := jsonb_build_object(
        'medusaOrderId', order_id
      )::text
    ) as request_id;
  
  return jsonb_build_object(
    'success', true
  );
  
exception
  when others then
    return jsonb_build_object(
      'success', false
    );
end;
$function$;

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

comment on function medreport.medipost_retry_dispatch(text) is 
'Manually trigger a medipost retry dispatch for a specific order ID. 
Parameters:
- order_id: The medusa order ID to retry dispatch for
Returns: JSONB with success status and request details';
