-- ECR-020 QualityFeedback CREATE TABLE IF NOT EXISTS ask_quality_feedback ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), thread_id uuid NOT NULL REFERENCES ask_threads(id), message_id uuid NULL REFERENCES ask_messages(id), source varchar(16) NOT NULL CHECK (source IN ('admin','user')), rating smallint NOT NULL CHECK (rating >= 1 AND rating <= 5), tag varchar(32) NULL CHECK (tag IS NULL OR tag IN ('helpful','off_topic','unsafe','other')), note text NULL, created_by_admin uuid NULL REFERENCES admin_accounts(id), created_by_user uuid NULL REFERENCES users(id), created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX IF NOT EXISTS idx_ask_qf_thread ON ask_quality_feedback(thread_id); CREATE INDEX IF NOT EXISTS idx_ask_qf_created ON ask_quality_feedback(created_at DESC); INSERT INTO admin_role_permissions(role_id, code) SELECT r.id, 'admin.ask.feedback.write' FROM admin_roles r WHERE r.name = 'super_admin' ON CONFLICT DO NOTHING;