-- Sustituye notification_sent (boolean) por tabla de log de notificaciones por sesión

ALTER TABLE public.session_participants DROP COLUMN notification_sent;

CREATE TABLE public.session_notifications_log (
  id            serial PRIMARY KEY,
  session_id    integer     NOT NULL REFERENCES public.sessions(session_id) ON DELETE CASCADE,
  participant_email text    NOT NULL,
  event_type    text        NOT NULL,
  sent_at       timestamptz NOT NULL DEFAULT now()
);

CREATE INDEX ON public.session_notifications_log (session_id);

CREATE OR REPLACE FUNCTION public.fn_log_session_notification(
  p_session_id integer,
  p_email      text,
  p_event_type text
)
RETURNS void
LANGUAGE sql AS $$
  INSERT INTO public.session_notifications_log (session_id, participant_email, event_type)
  VALUES (p_session_id, p_email, p_event_type);
$$;
