-- ============================================================
-- Migration 007: Notification System Tables
-- All tables in public schema (platform-wide, cross-tenant)
-- ============================================================

-- 1. Notifications — created & managed by platform admins
CREATE TABLE IF NOT EXISTS public.notifications (
  id                  UUID         PRIMARY KEY DEFAULT gen_random_uuid(),
  title               VARCHAR(200) NOT NULL,
  content             TEXT         NOT NULL,
  type                VARCHAR(30)  NOT NULL DEFAULT 'ANNOUNCEMENT',
  -- SYSTEM | MAINTENANCE | FEATURE | ANNOUNCEMENT | BILLING | SECURITY
  priority            VARCHAR(20)  NOT NULL DEFAULT 'NORMAL',
  -- LOW | NORMAL | HIGH | URGENT
  target_type         VARCHAR(20)  NOT NULL DEFAULT 'ALL',
  -- ALL | SPECIFIC_TENANTS
  image_url           VARCHAR(500),
  link_url            VARCHAR(500),
  requires_force_read BOOLEAN      NOT NULL DEFAULT false,
  is_enabled          BOOLEAN      NOT NULL DEFAULT true,
  published_at        TIMESTAMPTZ,          -- NULL = draft (not yet published)
  expires_at          TIMESTAMPTZ,          -- NULL = never expires
  created_by          VARCHAR(100),
  created_at          TIMESTAMPTZ  NOT NULL DEFAULT NOW(),
  updated_at          TIMESTAMPTZ  NOT NULL DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_notifications_type       ON public.notifications (type);
CREATE INDEX IF NOT EXISTS idx_notifications_enabled    ON public.notifications (is_enabled);
CREATE INDEX IF NOT EXISTS idx_notifications_published  ON public.notifications (published_at);
CREATE INDEX IF NOT EXISTS idx_notifications_target     ON public.notifications (target_type);

-- 2. Notification reads — tracks per-user read status
CREATE TABLE IF NOT EXISTS public.notification_reads (
  id              UUID        PRIMARY KEY DEFAULT gen_random_uuid(),
  notification_id UUID        NOT NULL REFERENCES public.notifications(id) ON DELETE CASCADE,
  user_id         VARCHAR(100) NOT NULL,
  tenant_id       VARCHAR(100) NOT NULL,
  read_at         TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  UNIQUE (notification_id, user_id)
);

CREATE INDEX IF NOT EXISTS idx_notification_reads_user   ON public.notification_reads (user_id);
CREATE INDEX IF NOT EXISTS idx_notification_reads_notif  ON public.notification_reads (notification_id);
CREATE INDEX IF NOT EXISTS idx_notification_reads_tenant ON public.notification_reads (tenant_id);

-- 3. Notification tenant targets — for SPECIFIC_TENANTS targeting
CREATE TABLE IF NOT EXISTS public.notification_tenant_targets (
  id              UUID        PRIMARY KEY DEFAULT gen_random_uuid(),
  notification_id UUID        NOT NULL REFERENCES public.notifications(id) ON DELETE CASCADE,
  tenant_id       VARCHAR(100) NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_notif_tenant_targets_notif  ON public.notification_tenant_targets (notification_id);
CREATE INDEX IF NOT EXISTS idx_notif_tenant_targets_tenant ON public.notification_tenant_targets (tenant_id);
