-- Add subscription tier and soft delete support to user_profiles
-- Migration: 002_add_subscription_and_deactivation.sql

-- Create subscription tier ENUM type
DO $$ BEGIN
  CREATE TYPE subscription_tier AS ENUM ('free', 'pro', 'enterprise');
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

-- Add subscription_tier column (default to 'free' for all users)
ALTER TABLE user_profiles
  ADD COLUMN IF NOT EXISTS subscription_tier subscription_tier NOT NULL DEFAULT 'free';

-- Add soft delete fields
ALTER TABLE user_profiles
  ADD COLUMN IF NOT EXISTS deactivated_at TIMESTAMP WITH TIME ZONE DEFAULT NULL;

ALTER TABLE user_profiles
  ADD COLUMN IF NOT EXISTS deactivated_by VARCHAR(255) DEFAULT NULL;

-- Index for filtering by subscription tier
CREATE INDEX IF NOT EXISTS idx_user_profiles_subscription_tier
  ON user_profiles(subscription_tier);

-- Index for filtering active vs deactivated users
CREATE INDEX IF NOT EXISTS idx_user_profiles_deactivated_at
  ON user_profiles(deactivated_at);

-- Composite index for efficient admin queries (tier + active status)
CREATE INDEX IF NOT EXISTS idx_user_profiles_tier_status
  ON user_profiles(subscription_tier, deactivated_at);
