-- Add ban expiration tracking
ALTER TABLE public.profiles 
ADD COLUMN IF NOT EXISTS ban_expires_at timestamptz;

COMMENT ON COLUMN public.profiles.ban_expires_at IS 
'When the ban expires (null = permanent ban). Automatic unbanning is handled by a scheduled edge function.';

-- Update existing banned users to have a default ban reason
UPDATE public.profiles
SET ban_reason = 'No reason provided (legacy ban)'
WHERE banned = true AND (ban_reason IS NULL OR ban_reason = '');

-- Add constraint to require ban_reason when banned
ALTER TABLE public.profiles 
ADD CONSTRAINT ban_reason_required 
CHECK (
  (banned = true AND ban_reason IS NOT NULL AND ban_reason != '') 
  OR 
  (banned = false)
);

COMMENT ON CONSTRAINT ban_reason_required ON public.profiles IS 
'Ensures that a ban reason must be provided when banning a user';