{"-- Create event types table
CREATE TABLE public.event_types (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  name_en TEXT NOT NULL,
  name_ar TEXT NOT NULL,
  description_en TEXT,
  description_ar TEXT,
  icon TEXT DEFAULT 'alert-circle',
  color TEXT DEFAULT '#3b82f6',
  enabled 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
)","-- Create camera event assignments table (many-to-many relationship)
CREATE TABLE public.camera_event_assignments (
  id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
  camera_id UUID NOT NULL,
  event_type_id UUID NOT NULL,
  enabled BOOLEAN NOT NULL DEFAULT true,
  confidence_threshold NUMERIC DEFAULT 0.7,
  created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
  UNIQUE(camera_id, event_type_id)
)","-- Enable RLS
ALTER TABLE public.event_types ENABLE ROW LEVEL SECURITY","ALTER TABLE public.camera_event_assignments ENABLE ROW LEVEL SECURITY","-- RLS policies for event_types
CREATE POLICY \"Super admin can manage all event types\"
ON public.event_types
FOR ALL
TO authenticated
USING (is_current_user_super_admin())","CREATE POLICY \"Users can view event types\"
ON public.event_types
FOR SELECT
TO authenticated
USING (enabled = true)","-- RLS policies for camera_event_assignments
CREATE POLICY \"Users can view camera event assignments in their organization\"
ON public.camera_event_assignments
FOR SELECT
TO authenticated
USING (
  EXISTS (
    SELECT 1 FROM cameras c
    JOIN branches b ON c.branch_id = b.id
    JOIN user_roles ur ON ur.organization_id = b.organization_id
    WHERE c.id = camera_event_assignments.camera_id 
    AND ur.user_id = auth.uid()
  )
)","CREATE POLICY \"Admins and managers can manage camera event assignments\"
ON public.camera_event_assignments
FOR ALL
TO authenticated
USING (
  EXISTS (
    SELECT 1 FROM cameras c
    JOIN branches b ON c.branch_id = b.id
    JOIN user_roles ur ON ur.organization_id = b.organization_id
    WHERE c.id = camera_event_assignments.camera_id 
    AND ur.user_id = auth.uid()
    AND ur.role IN ('admin', 'manager')
  )
)","-- Update triggers
CREATE TRIGGER update_event_types_updated_at
  BEFORE UPDATE ON public.event_types
  FOR EACH ROW
  EXECUTE FUNCTION public.update_modified_column()","-- Insert default event types
INSERT INTO public.event_types (name_en, name_ar, description_en, description_ar, icon, color) VALUES
('person', 'شخص', 'Person detection', 'اكتشاف الأشخاص', 'user', '#10b981'),
('car', 'سيارة', 'Vehicle detection', 'اكتشاف المركبات', 'car', '#3b82f6'),
('fire', 'حريق', 'Fire detection', 'اكتشاف الحرائق', 'flame', '#ef4444'),
('weapon', 'سلاح', 'Weapon detection', 'اكتشاف الأسلحة', 'shield-alert', '#dc2626'),
('theft', 'سرقة', 'Theft detection', 'اكتشاف السرقة', 'alert-triangle', '#f59e0b'),
('intrusion', 'تسلل', 'Intrusion detection', 'اكتشاف التسلل', 'shield-x', '#8b5cf6'),
('crowd', 'حشد', 'Crowd detection', 'اكتشاف التجمعات', 'users', '#06b6d4'),
('violence', 'عنف', 'Violence detection', 'اكتشاف العنف', 'zap', '#e11d48')"}