CREATE TABLE public.events (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  title TEXT NOT NULL,
  description TEXT,
  category TEXT NOT NULL DEFAULT 'Soirée',
  event_date DATE NOT NULL,
  time_label TEXT,
  image_url TEXT,
  is_featured BOOLEAN NOT NULL DEFAULT false,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

GRANT SELECT ON public.events TO anon;
GRANT SELECT, INSERT, UPDATE, DELETE ON public.events TO authenticated;
GRANT ALL ON public.events TO service_role;

ALTER TABLE public.events ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Public can view events" ON public.events FOR SELECT TO anon, authenticated USING (true);
CREATE POLICY "Authenticated can create events" ON public.events FOR INSERT TO authenticated WITH CHECK (true);
CREATE POLICY "Authenticated can update events" ON public.events FOR UPDATE TO authenticated USING (true) WITH CHECK (true);
CREATE POLICY "Authenticated can delete events" ON public.events FOR DELETE TO authenticated USING (true);

CREATE OR REPLACE FUNCTION public.update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = now();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql SET search_path = public;

CREATE TRIGGER update_events_updated_at
  BEFORE UPDATE ON public.events
  FOR EACH ROW
  EXECUTE FUNCTION public.update_updated_at_column();

INSERT INTO public.events (title, description, category, event_date, time_label, image_url, is_featured) VALUES
('Midnight Jazz Session', 'Un quartet acoustique revisitant les classiques du film noir, dans une ambiance club feutrée. Réservation conseillée.', 'Jazz', '2026-08-21', '21:00 — 00:00', '/__l5e/assets-v1/9318e326-a7cf-42cd-85c4-b9ec0067710d/event-jazz.jpg', true),
('Vinyl & Velvet Night', 'Deep house & nu-disco mixés exclusivement sur vinyle par Julian M. Une nuit rouge et sensuelle jusqu''au bout de la nuit.', 'DJ Set', '2026-08-28', '22:00 — 03:00', '/__l5e/assets-v1/0ea47e7d-ffc4-4c5a-89a5-461153fa9cd2/event-dj.jpg', true),
('Masterclass Mixologie', 'Apprenez l''art de l''équilibre parfait et du geste avec notre chef barman. Dégustation de 3 signatures incluses.', 'Dégustation', '2026-09-04', '19:30 — 21:30', '/__l5e/assets-v1/79e6939f-bd46-4fde-b959-ef5776bfb99d/event-mixology.jpg', false);