-- Create a table to record which notifications have been pushed by the edge function
CREATE TABLE IF NOT EXISTS public.notification_pushes (
  notification_id uuid PRIMARY KEY,
  pushed_at timestamptz NOT NULL DEFAULT now()
);

-- Helper function: try to mark a notification as pushed. Returns true if the
-- insert succeeded (i.e. this notification was not previously pushed), false
-- if it already existed.
CREATE OR REPLACE FUNCTION public.try_mark_notification_pushed(p_notification_id uuid)
RETURNS boolean LANGUAGE plpgsql AS $$
BEGIN
  INSERT INTO public.notification_pushes(notification_id) VALUES (p_notification_id)
  ON CONFLICT DO NOTHING;
  RETURN FOUND; -- true if insert happened, false if ON CONFLICT prevented insert
END;
$$;

-- index for quick lookup by pushed_at if needed
CREATE INDEX IF NOT EXISTS idx_notification_pushes_pushed_at ON public.notification_pushes(pushed_at);
