-- Hourly reconcile of FileBrowser folders against companies/projects/tasks/subcontractors.
-- Vercel Hobby crons only run once per day, so the schedule lives in Postgres instead.
--
-- One-time manual step before this works (run once, do NOT commit the secret):
--   alter database postgres set app.folder_reconcile_secret = '<same value as FOLDER_RECONCILE_SECRET>';

create extension if not exists pg_cron with schema extensions;
create extension if not exists pg_net with schema extensions;

select cron.unschedule('folder-reconcile-hourly')
where exists (select 1 from cron.job where jobname = 'folder-reconcile-hourly');

select cron.schedule(
  'folder-reconcile-hourly',
  '7 * * * *',
  $$
  select net.http_post(
    url := 'https://portal.fourgebranding.com/api/folder-reconcile',
    headers := jsonb_build_object(
      'Content-Type', 'application/json',
      'x-reconcile-secret', current_setting('app.folder_reconcile_secret', true)
    ),
    body := '{}'::jsonb,
    timeout_milliseconds := 120000
  );
  $$
);
