-- Add missing columns to profiles table (safe: uses IF NOT EXISTS equivalents)
ALTER TABLE public.profiles
  ADD COLUMN IF NOT EXISTS full_name          TEXT,
  ADD COLUMN IF NOT EXISTS avatar_url         TEXT,
  ADD COLUMN IF NOT EXISTS stripe_price_id    TEXT,
  ADD COLUMN IF NOT EXISTS subscription_status TEXT DEFAULT 'inactive',
  ADD COLUMN IF NOT EXISTS analyses_total     INT NOT NULL DEFAULT 0,
  ADD COLUMN IF NOT EXISTS last_analysis_date DATE;

-- Ensure plan column only allows free/pro
ALTER TABLE public.profiles
  DROP CONSTRAINT IF EXISTS profiles_plan_check;
ALTER TABLE public.profiles
  ADD CONSTRAINT profiles_plan_check CHECK (plan IN ('free', 'pro'));

-- Back-fill full_name from username if present
UPDATE public.profiles SET full_name = username WHERE full_name IS NULL AND username IS NOT NULL;
