-- 后台待办通知：业务状态驱动，多个后台账号共享处理状态。
CREATE TABLE IF NOT EXISTS admin_notifications (
  id BIGSERIAL PRIMARY KEY,
  notification_type TEXT NOT NULL,
  priority TEXT NOT NULL DEFAULT 'normal',
  entity_type TEXT NOT NULL,
  entity_id BIGINT NOT NULL,
  dedupe_key TEXT NOT NULL UNIQUE,
  title TEXT NOT NULL,
  body TEXT NOT NULL DEFAULT '',
  action_path TEXT NOT NULL DEFAULT '',
  payload_json JSONB NOT NULL DEFAULT '{}'::jsonb,
  reminder_count INTEGER NOT NULL DEFAULT 0,
  last_reminded_at TIMESTAMPTZ,
  status TEXT NOT NULL DEFAULT 'pending',
  resolved_at TIMESTAMPTZ,
  resolution_reason TEXT NOT NULL DEFAULT '',
  created_at TIMESTAMPTZ NOT NULL,
  updated_at TIMESTAMPTZ NOT NULL
);

CREATE INDEX IF NOT EXISTS idx_admin_notifications_pending
  ON admin_notifications (status, priority, updated_at DESC);

CREATE INDEX IF NOT EXISTS idx_admin_notifications_entity
  ON admin_notifications (entity_type, entity_id);

COMMENT ON TABLE admin_notifications IS '后台共享业务待办通知，不使用个人已读状态';
COMMENT ON COLUMN admin_notifications.dedupe_key IS '同一业务待办的唯一标识';
