-- ============================================
-- 此文件由 Meoo Cloud 自动生成，请勿修改
-- This file is auto-generated by Meoo Cloud, DO NOT MODIFY
-- ============================================


CREATE TABLE IF NOT EXISTS user_feedback (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 type TEXT NOT NULL CHECK (type IN ('suggestion', 'bug', 'question')),
 content TEXT NOT NULL,
 contact TEXT,
 user_id UUID REFERENCES auth.users(id),
 status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'processing', 'resolved', 'closed')),
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
 updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

CREATE TABLE IF NOT EXISTS crash_reports (
 id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
 error_message TEXT NOT NULL,
 error_stack TEXT,
 component_stack TEXT,
 user_description TEXT,
 user_agent TEXT,
 url TEXT,
 user_id UUID REFERENCES auth.users(id),
 status TEXT NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'investigating', 'fixed', 'closed')),
 created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
 updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);

-- 启用RLS
ALTER TABLE user_feedback ENABLE ROW LEVEL SECURITY;
ALTER TABLE crash_reports ENABLE ROW LEVEL SECURITY;

-- 创建RLS策略
CREATE POLICY users_insert_feedback ON user_feedback
 FOR INSERT WITH CHECK (auth.uid() = user_id OR user_id IS NULL);

CREATE POLICY users_select_own_feedback ON user_feedback
 FOR SELECT USING (auth.uid() = user_id);

CREATE POLICY users_insert_crash ON crash_reports
 FOR INSERT WITH CHECK (auth.uid() = user_id OR user_id IS NULL);

