{"-- Create storage configuration table for system-wide storage settings
CREATE TABLE public.storage_configs (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  name TEXT NOT NULL,
  type TEXT NOT NULL CHECK (type IN ('google_drive', 'ftp', 'aws_s3', 'local')),
  config JSONB NOT NULL DEFAULT '{}',
  is_active BOOLEAN NOT NULL DEFAULT false,
  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)
)","-- Create system settings table for general system configurations
CREATE TABLE public.system_settings (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  setting_key TEXT NOT NULL UNIQUE,
  setting_value JSONB NOT NULL DEFAULT '{}',
  description TEXT,
  created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  updated_by UUID REFERENCES auth.users(id)
)","-- Create storage bucket for model files
INSERT INTO storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
VALUES (
  'model-files',
  'model-files', 
  false,
  104857600, -- 100MB limit
  ARRAY['application/octet-stream', 'application/x-pytorch', 'application/x-onnx', 'application/zip']
)","-- Enable RLS on storage tables
ALTER TABLE public.storage_configs ENABLE ROW LEVEL SECURITY","ALTER TABLE public.system_settings ENABLE ROW LEVEL SECURITY","-- RLS policies for storage_configs (super admin only)
CREATE POLICY \"Super admin can manage storage configs\"
ON public.storage_configs
FOR ALL
TO authenticated
USING (is_current_user_super_admin())","-- RLS policies for system_settings (super admin only)
CREATE POLICY \"Super admin can manage system settings\"
ON public.system_settings
FOR ALL
TO authenticated
USING (is_current_user_super_admin())","-- Storage policies for model files
CREATE POLICY \"Super admin can manage model files\"
ON storage.objects
FOR ALL
TO authenticated
USING (bucket_id = 'model-files' AND is_current_user_super_admin())","CREATE POLICY \"Users can view model files\"
ON storage.objects
FOR SELECT
TO authenticated
USING (bucket_id = 'model-files')","-- Update triggers
CREATE TRIGGER update_storage_configs_updated_at
  BEFORE UPDATE ON public.storage_configs
  FOR EACH ROW
  EXECUTE FUNCTION public.update_modified_column()","CREATE TRIGGER update_system_settings_updated_at
  BEFORE UPDATE ON public.system_settings
  FOR EACH ROW
  EXECUTE FUNCTION public.update_modified_column()","-- Insert default storage configuration
INSERT INTO public.storage_configs (name, type, config, is_active) VALUES
('Local Storage', 'local', '{\"path\": \"/uploads/models\"}', true)","-- Insert default system settings
INSERT INTO public.system_settings (setting_key, setting_value, description) VALUES
('default_storage_provider', '{\"type\": \"local\"}', 'Default storage provider for model files'),
('max_file_size_mb', '{\"value\": 100}', 'Maximum file size in MB for model uploads')"}