{"-- 1) Add zoom to pinned_dashboards
ALTER TABLE public.pinned_dashboards
ADD COLUMN IF NOT EXISTS zoom NUMERIC NOT NULL DEFAULT 1.0","-- 2) Create labels table for canvas annotations
CREATE TABLE IF NOT EXISTS public.pinned_labels (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID NOT NULL,
  text TEXT NOT NULL DEFAULT 'Label',
  color TEXT NOT NULL DEFAULT '#111827', -- neutral-900
  font_size INTEGER NOT NULL DEFAULT 16,
  x INTEGER NOT NULL DEFAULT 40,
  y INTEGER NOT NULL DEFAULT 40,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
)","-- Enable RLS (idempotent)
ALTER TABLE public.pinned_labels ENABLE ROW LEVEL SECURITY","-- Policies: only owner can access their labels (idempotent via DO blocks)
DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE schemaname = 'public' AND tablename = 'pinned_labels' AND policyname = 'Labels: users can view their own'
  ) THEN
    CREATE POLICY \"Labels: users can view their own\"
    ON public.pinned_labels FOR SELECT
    USING (auth.uid() = user_id);
  END IF;
END $$","DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE schemaname = 'public' AND tablename = 'pinned_labels' AND policyname = 'Labels: users can insert their own'
  ) THEN
    CREATE POLICY \"Labels: users can insert their own\"
    ON public.pinned_labels FOR INSERT
    WITH CHECK (auth.uid() = user_id);
  END IF;
END $$","DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE schemaname = 'public' AND tablename = 'pinned_labels' AND policyname = 'Labels: users can update their own'
  ) THEN
    CREATE POLICY \"Labels: users can update their own\"
    ON public.pinned_labels FOR UPDATE
    USING (auth.uid() = user_id);
  END IF;
END $$","DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_policies 
    WHERE schemaname = 'public' AND tablename = 'pinned_labels' AND policyname = 'Labels: users can delete their own'
  ) THEN
    CREATE POLICY \"Labels: users can delete their own\"
    ON public.pinned_labels FOR DELETE
    USING (auth.uid() = user_id);
  END IF;
END $$","-- Trigger to keep updated_at fresh
CREATE OR REPLACE FUNCTION public.update_modified_column()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = now();
  RETURN NEW;
END; $$ LANGUAGE plpgsql","DROP TRIGGER IF EXISTS trg_pinned_labels_updated_at ON public.pinned_labels","CREATE TRIGGER trg_pinned_labels_updated_at
BEFORE UPDATE ON public.pinned_labels
FOR EACH ROW EXECUTE FUNCTION public.update_modified_column()","-- Helpful index for user_id lookup
CREATE INDEX IF NOT EXISTS idx_pinned_labels_user_id ON public.pinned_labels(user_id)"}