{"-- Create camera sources table for super admin management
CREATE TABLE public.camera_sources (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  name TEXT NOT NULL,
  name_ar TEXT NOT NULL,
  protocol TEXT NOT NULL, -- rtsp, http, youtube, file, etc.
  requires_auth BOOLEAN NOT NULL DEFAULT false,
  default_port INTEGER,
  url_pattern TEXT, -- pattern for URL construction
  description TEXT,
  description_ar TEXT,
  icon TEXT DEFAULT 'camera',
  active BOOLEAN NOT NULL DEFAULT true,
  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)
)","-- Enable RLS
ALTER TABLE public.camera_sources ENABLE ROW LEVEL SECURITY","-- Super admin can manage all camera sources
CREATE POLICY \"Super admin can manage camera sources\" 
ON public.camera_sources 
FOR ALL 
USING (is_current_user_super_admin())","-- All authenticated users can view camera sources
CREATE POLICY \"Users can view camera sources\" 
ON public.camera_sources 
FOR SELECT 
USING (auth.role() = 'authenticated' AND active = true)","-- Create trigger for updated_at
CREATE TRIGGER update_camera_sources_updated_at
BEFORE UPDATE ON public.camera_sources
FOR EACH ROW
EXECUTE FUNCTION public.update_modified_column()","-- Insert default camera sources
INSERT INTO public.camera_sources (name, name_ar, protocol, requires_auth, default_port, url_pattern, description, description_ar) VALUES
('RTSP Stream', 'بث RTSP', 'rtsp', true, 554, 'rtsp://[username]:[password]@[host]:[port]/[path]', 'Real Time Streaming Protocol for IP cameras', 'بروتوكول البث المباشر لكاميرات IP'),
('HTTP Stream', 'بث HTTP', 'http', false, 80, 'http://[host]:[port]/[path]', 'HTTP video stream', 'بث الفيديو عبر HTTP'),
('HTTPS Stream', 'بث HTTPS', 'https', false, 443, 'https://[host]:[port]/[path]', 'Secure HTTP video stream', 'بث الفيديو الآمن عبر HTTPS'),
('YouTube Live', 'يوتيوب مباشر', 'youtube', false, NULL, 'https://youtube.com/watch?v=[id]', 'YouTube live stream', 'البث المباشر من يوتيوب'),
('File Upload', 'رفع ملف', 'file', false, NULL, NULL, 'Upload video file', 'رفع ملف فيديو'),
('Webcam', 'كاميرا ويب', 'webcam', false, NULL, NULL, 'Computer webcam', 'كاميرا الكمبيوتر'),
('HLS Stream', 'بث HLS', 'hls', false, NULL, 'http://[host]:[port]/[path].m3u8', 'HTTP Live Streaming', 'البث المباشر عبر HTTP')","-- Create network discovery results table for real scanning
CREATE TABLE public.network_scan_results (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  ip_address INET NOT NULL,
  port INTEGER NOT NULL,
  manufacturer TEXT,
  model TEXT,
  device_type TEXT,
  protocol TEXT,
  mac_address TEXT,
  hostname TEXT,
  discovered_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  scan_session_id UUID NOT NULL,
  user_id UUID REFERENCES auth.users(id)
)","-- Enable RLS
ALTER TABLE public.network_scan_results ENABLE ROW LEVEL SECURITY","-- Users can only see their own scan results
CREATE POLICY \"Users can view their own scan results\" 
ON public.network_scan_results 
FOR SELECT 
USING (user_id = auth.uid())","-- Users can insert their own scan results
CREATE POLICY \"Users can insert their own scan results\" 
ON public.network_scan_results 
FOR INSERT 
WITH CHECK (user_id = auth.uid())"}