{"-- Create pinned_dashboards table to store user-specific pinned dashboard layouts
CREATE TABLE IF NOT EXISTS public.pinned_dashboards (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID NOT NULL,
  instance_id UUID NOT NULL,
  name TEXT,
  path TEXT NOT NULL DEFAULT '/lovelace/default_view?kiosk',
  x INTEGER NOT NULL DEFAULT 0,
  y INTEGER NOT NULL DEFAULT 0,
  width INTEGER NOT NULL DEFAULT 600,
  height INTEGER NOT NULL DEFAULT 400,
  order_index INTEGER NOT NULL DEFAULT 0,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
)","-- Enable Row Level Security
ALTER TABLE public.pinned_dashboards ENABLE ROW LEVEL SECURITY","-- RLS Policies: users can manage only their own rows
CREATE POLICY \"Users can view their pinned dashboards\"
  ON public.pinned_dashboards
  FOR SELECT
  USING (auth.uid() = user_id)","CREATE POLICY \"Users can insert their pinned dashboards\"
  ON public.pinned_dashboards
  FOR INSERT
  WITH CHECK (auth.uid() = user_id)","CREATE POLICY \"Users can update their pinned dashboards\"
  ON public.pinned_dashboards
  FOR UPDATE
  USING (auth.uid() = user_id)","CREATE POLICY \"Users can delete their pinned dashboards\"
  ON public.pinned_dashboards
  FOR DELETE
  USING (auth.uid() = user_id)","-- Updated-at trigger
DO $$ BEGIN
  IF NOT EXISTS (
    SELECT 1 FROM pg_trigger WHERE tgname = 'update_pinned_dashboards_updated_at'
  ) THEN
    CREATE TRIGGER update_pinned_dashboards_updated_at
    BEFORE UPDATE ON public.pinned_dashboards
    FOR EACH ROW
    EXECUTE FUNCTION public.update_modified_column();
  END IF;
END $$","-- Helpful index
CREATE INDEX IF NOT EXISTS idx_pinned_dashboards_user_id ON public.pinned_dashboards(user_id)"}