BEGIN;

CREATE TABLE IF NOT EXISTS public.daily_challenges (
  id              uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  challenge_id    text NOT NULL,
  challenge_type  text NOT NULL,
  team_role       text NOT NULL DEFAULT 'blue',
  xp_reward       integer NOT NULL DEFAULT 150,
  active_date     date NOT NULL DEFAULT CURRENT_DATE,
  created_at      timestamptz NOT NULL DEFAULT now(),
  UNIQUE(active_date)
);

ALTER TABLE public.daily_challenges ENABLE ROW LEVEL SECURITY;

DROP POLICY IF EXISTS "Open read daily_challenges" ON public.daily_challenges;
CREATE POLICY "Open read daily_challenges" ON public.daily_challenges
  FOR SELECT USING (true);

DROP POLICY IF EXISTS "Service role insert daily_challenges" ON public.daily_challenges;
CREATE POLICY "Service role insert daily_challenges" ON public.daily_challenges
  FOR INSERT WITH CHECK (true);

DROP POLICY IF EXISTS "Service role update daily_challenges" ON public.daily_challenges;
CREATE POLICY "Service role update daily_challenges" ON public.daily_challenges
  FOR UPDATE USING (true);

COMMIT;
