{"-- Create organization_settings table (referenced by later migration)
CREATE TABLE IF NOT EXISTS public.organization_settings (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  organization_id UUID NOT NULL REFERENCES public.organizations(id) ON DELETE CASCADE,
  setting_key TEXT NOT NULL,
  setting_value JSONB NOT NULL,
  description TEXT,
  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  UNIQUE(organization_id, setting_key)
)","-- Enable RLS
ALTER TABLE public.organization_settings ENABLE ROW LEVEL SECURITY","-- Drop existing policies to avoid conflicts
DO $$
DECLARE r record;
BEGIN
  FOR r IN SELECT policyname FROM pg_policies WHERE tablename='organization_settings' AND schemaname='public' LOOP
    EXECUTE format('DROP POLICY %I ON public.organization_settings', r.policyname);
  END LOOP;
END $$","-- RLS: users can view their org's settings, super_admin can manage all
CREATE POLICY \"org_settings_view\" ON public.organization_settings FOR SELECT
USING (public.is_current_user_super_admin() OR organization_id IN
  (SELECT organization_id FROM public.user_roles WHERE user_id = auth.uid()))","CREATE POLICY \"org_settings_manage\" ON public.organization_settings FOR ALL
USING (public.is_current_user_super_admin() OR organization_id IN
  (SELECT organization_id FROM public.user_roles WHERE user_id = auth.uid() AND role IN ('admin', 'manager')))
WITH CHECK (public.is_current_user_super_admin() OR organization_id IN
  (SELECT organization_id FROM public.user_roles WHERE user_id = auth.uid() AND role IN ('admin', 'manager')))","-- Trigger
DROP TRIGGER IF EXISTS update_organization_settings_updated_at ON public.organization_settings","CREATE TRIGGER update_organization_settings_updated_at
BEFORE UPDATE ON public.organization_settings
FOR EACH ROW EXECUTE FUNCTION public.update_modified_column()"}