-- Phase: Dashboard Builder (GridStack) layouts & templates

CREATE TABLE IF NOT EXISTS dashboard_layout (
  id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  name text NOT NULL,
  scope text NOT NULL CHECK (scope IN ('global','org','user')),
  org_id uuid REFERENCES org(id) ON DELETE CASCADE,
  created_by_user_id bigint REFERENCES app_user(id) ON DELETE SET NULL,
  is_default boolean NOT NULL DEFAULT false,
  layout_json jsonb NOT NULL,
  created_at timestamptz NOT NULL DEFAULT now(),
  updated_at timestamptz
);

CREATE INDEX IF NOT EXISTS idx_dashboard_layout_scope ON dashboard_layout(scope);
CREATE INDEX IF NOT EXISTS idx_dashboard_layout_org ON dashboard_layout(org_id);
