-- v68: Discord Live Credit Drops tracking table + site popup notification state
CREATE TABLE IF NOT EXISTS public.discord_credit_drops (
  drop_id text PRIMARY KEY,
  channel_id text NOT NULL,
  message_id text,
  amount integer NOT NULL,
  max_claims integer NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now()
);

ALTER TABLE public.discord_credit_drops ENABLE ROW LEVEL SECURITY;

CREATE TABLE IF NOT EXISTS public.discord_credit_drop_claims (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  drop_id text NOT NULL,
  discord_user_id text NOT NULL,
  user_id uuid NOT NULL REFERENCES public.profiles(id) ON DELETE CASCADE,
  username text NOT NULL,
  amount integer NOT NULL,
  seen_on_site boolean NOT NULL DEFAULT false,
  created_at timestamptz NOT NULL DEFAULT now(),
  UNIQUE (drop_id, user_id)
);

ALTER TABLE public.discord_credit_drop_claims
  DROP CONSTRAINT IF EXISTS discord_credit_drop_claims_drop_id_discord_user_id_key;

ALTER TABLE public.discord_credit_drop_claims
  ADD COLUMN IF NOT EXISTS seen_on_site boolean NOT NULL DEFAULT false;

CREATE INDEX IF NOT EXISTS idx_discord_credit_drop_claims_unseen
  ON public.discord_credit_drop_claims (user_id, seen_on_site)
  WHERE seen_on_site = false;

ALTER TABLE public.discord_credit_drop_claims ENABLE ROW LEVEL SECURITY;
