-- ============================================================================
-- Echo (回声) - Onboarding 功能 Schema 修复迁移脚本
-- 版本: 202601120003
-- 创建日期: 2026-01-12
-- 修复内容:
--   1. users 表添加 has_completed_onboarding 字段
--   2. onboarding_answers 表添加 updated_at 字段
--   3. onboarding_answers 表添加 updated_at 触发器
--   4. 确保 onboarding_answers RLS 策略存在
-- ============================================================================

-- ============================================================================
-- 修复 1: users 表添加 has_completed_onboarding 字段
-- ============================================================================

ALTER TABLE users ADD COLUMN IF NOT EXISTS has_completed_onboarding BOOLEAN DEFAULT FALSE;

-- ============================================================================
-- 修复 2: onboarding_answers 表添加 updated_at 字段
-- ============================================================================

ALTER TABLE onboarding_answers ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW();

-- ============================================================================
-- 修复 3: onboarding_answers 表添加 updated_at 触发器
-- ============================================================================

-- 检查触发器是否已存在
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM information_schema.triggers
        WHERE trigger_name = 'update_onboarding_answers_updated_at'
        AND table_name = 'onboarding_answers'
    ) THEN
        CREATE TRIGGER update_onboarding_answers_updated_at
            BEFORE UPDATE ON onboarding_answers
            FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
    END IF;
END $$;

-- ============================================================================
-- 修复 4: 确保 onboarding_answers RLS 策略存在
-- ============================================================================

-- 启用 RLS（如果尚未启用）
ALTER TABLE onboarding_answers ENABLE ROW LEVEL SECURITY;

-- 检查并创建 SELECT 策略
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_policies
        WHERE tablename = 'onboarding_answers'
        AND policyname = 'Users can view own onboarding answers'
    ) THEN
        CREATE POLICY "Users can view own onboarding answers" ON onboarding_answers
            FOR SELECT USING (auth.uid() = user_id);
    END IF;
END $$;

-- 检查并创建 INSERT 策略
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_policies
        WHERE tablename = 'onboarding_answers'
        AND policyname = 'Users can create onboarding answers'
    ) THEN
        CREATE POLICY "Users can create onboarding answers" ON onboarding_answers
            FOR INSERT WITH CHECK (auth.uid() = user_id);
    END IF;
END $$;

-- 检查并创建 UPDATE 策略
DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_policies
        WHERE tablename = 'onboarding_answers'
        AND policyname = 'Users can update own onboarding answers'
    ) THEN
        CREATE POLICY "Users can update own onboarding answers" ON onboarding_answers
            FOR UPDATE USING (auth.uid() = user_id);
    END IF;
END $$;

-- ============================================================================
-- 迁移完成
-- ============================================================================
