{"-- Create table for legal documents (Terms of Service, Privacy Policy)
CREATE TABLE public.legal_documents (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  document_type TEXT NOT NULL UNIQUE CHECK (document_type IN ('terms_of_service', 'privacy_policy')),
  title TEXT NOT NULL,
  content TEXT NOT NULL,
  version INTEGER NOT NULL DEFAULT 1,
  effective_date TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
  created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
  created_by UUID REFERENCES auth.users(id),
  updated_by UUID REFERENCES auth.users(id)
)","-- Enable RLS
ALTER TABLE public.legal_documents ENABLE ROW LEVEL SECURITY","-- Create policies
-- Anyone can view legal documents
CREATE POLICY \"Anyone can view legal documents\" 
ON public.legal_documents 
FOR SELECT 
USING (true)","-- Only super admins can manage legal documents
CREATE POLICY \"Super admins can manage legal documents\" 
ON public.legal_documents 
FOR ALL 
USING (
  EXISTS (
    SELECT 1 FROM user_roles ur 
    WHERE ur.user_id = auth.uid() 
    AND ur.role = 'admin'
  )
)","-- Create trigger for updated_at
CREATE TRIGGER update_legal_documents_updated_at
BEFORE UPDATE ON public.legal_documents
FOR EACH ROW
EXECUTE FUNCTION public.update_modified_column()","-- Insert default content
INSERT INTO public.legal_documents (document_type, title, content) VALUES 
('terms_of_service', 'شروط الخدمة', 'محتوى شروط الخدمة سيتم تحديثه من قبل الإدارة.'),
('privacy_policy', 'سياسة الخصوصية', 'محتوى سياسة الخصوصية سيتم تحديثه من قبل الإدارة.')"}