-- ============================================================
-- Map Shapes: polygons and rectangles for map layers
-- ============================================================

CREATE TABLE map_shapes (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  layer_id UUID NOT NULL REFERENCES map_layers(id) ON DELETE CASCADE,
  shape_type TEXT NOT NULL DEFAULT 'polygon' CHECK (shape_type IN ('polygon', 'rectangle')),
  label TEXT NOT NULL DEFAULT '',
  color TEXT NOT NULL DEFAULT '#6366f1',
  fill_opacity DOUBLE PRECISION NOT NULL DEFAULT 0.15,
  stroke_width DOUBLE PRECISION NOT NULL DEFAULT 2,
  vertices JSONB NOT NULL DEFAULT '[]',
  rotation DOUBLE PRECISION NOT NULL DEFAULT 0,
  sort_order INT NOT NULL DEFAULT 0,
  created_by UUID REFERENCES auth.users(id) ON DELETE SET NULL,
  created_at TIMESTAMPTZ DEFAULT now(),
  updated_at TIMESTAMPTZ DEFAULT now()
);

CREATE INDEX idx_map_shapes_layer ON map_shapes(layer_id);

-- RLS
ALTER TABLE map_shapes ENABLE ROW LEVEL SECURITY;

-- Select: org members can view
CREATE POLICY "Org members can view map shapes" ON map_shapes FOR SELECT
  USING (EXISTS (
    SELECT 1 FROM map_layers ml
    JOIN event_departments ed ON ml.department_id = ed.id
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ml.id = map_shapes.layer_id AND om.user_id = auth.uid()
  ));

-- Insert
CREATE POLICY "Dept members and editors can insert map shapes" ON map_shapes FOR INSERT
  WITH CHECK (EXISTS (
    SELECT 1 FROM map_layers ml
    JOIN event_departments ed ON ml.department_id = ed.id
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ml.id = map_shapes.layer_id
      AND om.user_id = auth.uid()
      AND (
        om.role IN ('owner', 'admin', 'editor')
        OR EXISTS (
          SELECT 1 FROM event_member_departments emd
          JOIN event_members em ON emd.event_member_id = em.id
          WHERE emd.department_id = ed.id AND em.user_id = auth.uid()
        )
      )
  ));

-- Update
CREATE POLICY "Dept members and editors can update map shapes" ON map_shapes FOR UPDATE
  USING (EXISTS (
    SELECT 1 FROM map_layers ml
    JOIN event_departments ed ON ml.department_id = ed.id
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ml.id = map_shapes.layer_id
      AND om.user_id = auth.uid()
      AND (
        om.role IN ('owner', 'admin', 'editor')
        OR EXISTS (
          SELECT 1 FROM event_member_departments emd
          JOIN event_members em ON emd.event_member_id = em.id
          WHERE emd.department_id = ed.id AND em.user_id = auth.uid()
        )
      )
  ))
  WITH CHECK (EXISTS (
    SELECT 1 FROM map_layers ml
    JOIN event_departments ed ON ml.department_id = ed.id
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ml.id = map_shapes.layer_id
      AND om.user_id = auth.uid()
      AND (
        om.role IN ('owner', 'admin', 'editor')
        OR EXISTS (
          SELECT 1 FROM event_member_departments emd
          JOIN event_members em ON emd.event_member_id = em.id
          WHERE emd.department_id = ed.id AND em.user_id = auth.uid()
        )
      )
  ));

-- Delete
CREATE POLICY "Dept members and editors can delete map shapes" ON map_shapes FOR DELETE
  USING (EXISTS (
    SELECT 1 FROM map_layers ml
    JOIN event_departments ed ON ml.department_id = ed.id
    JOIN events e ON ed.event_id = e.id
    JOIN org_members om ON e.org_id = om.org_id
    WHERE ml.id = map_shapes.layer_id
      AND om.user_id = auth.uid()
      AND (
        om.role IN ('owner', 'admin', 'editor')
        OR EXISTS (
          SELECT 1 FROM event_member_departments emd
          JOIN event_members em ON emd.event_member_id = em.id
          WHERE emd.department_id = ed.id AND em.user_id = auth.uid()
        )
      )
  ));

ALTER PUBLICATION supabase_realtime ADD TABLE map_shapes;
