{"-- Create a function to handle HTTP requests
CREATE OR REPLACE FUNCTION public.trigger_auto_sync()
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
BEGIN
  -- Skipped: requires pg_net extension not enabled in local dev
  -- In production, this function calls auto-sync-data edge function via pg_net
  NULL;
END;
$$","-- Create a function to manage the auto sync schedule (no-op in local dev)
CREATE OR REPLACE FUNCTION public.manage_auto_sync_schedule(enabled boolean, interval_minutes integer DEFAULT 30)
RETURNS void
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
BEGIN
  -- No-op: pg_cron not available in local dev
  NULL;
END;
$$","-- Create a table to store auto sync settings
CREATE TABLE IF NOT EXISTS public.auto_sync_settings (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    enabled BOOLEAN NOT NULL DEFAULT true,
    interval_minutes INTEGER NOT NULL DEFAULT 30,
    last_sync TIMESTAMP WITH TIME ZONE,
    next_sync TIMESTAMP WITH TIME ZONE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
    updated_at TIMESTAMP WITH TIME ZONE DEFAULT now()
)","-- Insert default settings
INSERT INTO public.auto_sync_settings (enabled, interval_minutes)
VALUES (true, 30)
ON CONFLICT DO NOTHING","-- Enable RLS
ALTER TABLE public.auto_sync_settings ENABLE ROW LEVEL SECURITY","-- Create policies for auto sync settings (only super admins can manage)
CREATE POLICY \"autosync_view\" ON public.auto_sync_settings FOR SELECT
USING (public.is_current_user_super_admin())","CREATE POLICY \"autosync_update\" ON public.auto_sync_settings FOR UPDATE
USING (public.is_current_user_super_admin())","-- Create trigger for updated_at
DROP TRIGGER IF EXISTS update_auto_sync_settings_updated_at ON public.auto_sync_settings","CREATE TRIGGER update_auto_sync_settings_updated_at
    BEFORE UPDATE ON public.auto_sync_settings
    FOR EACH ROW
    EXECUTE FUNCTION public.update_modified_column()"}