create table if not exists public.tenant_content_notifications ( id uuid primary key default gen_random_uuid(), tenant_id uuid not null references public.tenants(id) on delete cascade, notification_type text not null check (notification_type in ( 'public_question_bank_synced', 'public_question_bank_conflict' )), status text not null default 'unread' check (status in ('unread', 'read', 'dismissed', 'resolved')), severity text not null default 'info' check (severity in ('info', 'success', 'warning', 'error')), adoption_id uuid references public.tenant_question_bank_adoptions(id) on delete cascade, source_question_bank_id uuid references public.question_banks(id) on delete cascade, title text not null, message text not null, action_label text, action_path text, dedupe_key text, metadata jsonb not null default '{}'::jsonb, created_by uuid references public.platform_users(id) on delete set null, read_by uuid references public.platform_users(id) on delete set null, read_at timestamptz, resolved_at timestamptz, created_at timestamptz not null default now(), updated_at timestamptz not null default now() ); create unique index if not exists idx_tenant_content_notifications_dedupe on public.tenant_content_notifications(tenant_id, notification_type, dedupe_key) where dedupe_key is not null; create index if not exists idx_tenant_content_notifications_tenant_status on public.tenant_content_notifications(tenant_id, status, created_at desc); create index if not exists idx_tenant_content_notifications_adoption on public.tenant_content_notifications(tenant_id, adoption_id, notification_type, status); alter table public.tenant_content_notifications enable row level security; drop policy if exists tenant_content_notifications_isolation on public.tenant_content_notifications; create policy tenant_content_notifications_isolation on public.tenant_content_notifications for all using (tenant_id = app.current_tenant_id() or app.is_platform_admin()) with check (tenant_id = app.current_tenant_id() or app.is_platform_admin()); drop trigger if exists set_updated_at on public.tenant_content_notifications; create trigger set_updated_at before update on public.tenant_content_notifications for each row execute function app.touch_updated_at();