-- RVW V4.0 期刊配置中心 MVP
-- 一次性补齐 tenants(P0/P1/P2+language) 与 tenant_rvw_configs(4维Prompt+Template)

-- 1) 枚举：JournalLanguage
DO $$
BEGIN
  IF NOT EXISTS (
    SELECT 1
    FROM pg_type t
    JOIN pg_namespace n ON n.oid = t.typnamespace
    WHERE t.typname = 'JournalLanguage'
      AND n.nspname = 'platform_schema'
  ) THEN
    CREATE TYPE "platform_schema"."JournalLanguage" AS ENUM ('ZH', 'EN', 'OTHER');
  END IF;
END $$;

-- 2) 枚举：TenantType 增加 JOURNAL
DO $$
BEGIN
  ALTER TYPE "platform_schema"."TenantType" ADD VALUE IF NOT EXISTS 'JOURNAL';
EXCEPTION
  WHEN duplicate_object THEN NULL;
END $$;

-- 3) tenants 表新增字段
ALTER TABLE "platform_schema"."tenants"
  ADD COLUMN IF NOT EXISTS "journal_language" "platform_schema"."JournalLanguage",
  ADD COLUMN IF NOT EXISTS "journal_full_name" TEXT,
  ADD COLUMN IF NOT EXISTS "logo_url" TEXT,
  ADD COLUMN IF NOT EXISTS "brand_color" TEXT,
  ADD COLUMN IF NOT EXISTS "login_background_url" TEXT;

ALTER TABLE "platform_schema"."tenants"
  ALTER COLUMN "journal_language" SET DEFAULT 'ZH';

-- 4) tenant_rvw_configs 表升级为 Prompt/Template 统一结构
ALTER TABLE "platform_schema"."tenant_rvw_configs"
  ADD COLUMN IF NOT EXISTS "editorial_base_standard" TEXT NOT NULL DEFAULT 'en',
  ADD COLUMN IF NOT EXISTS "editorial_expert_prompt" TEXT,
  ADD COLUMN IF NOT EXISTS "editorial_handlebars_template" TEXT,
  ADD COLUMN IF NOT EXISTS "data_forensics_expert_prompt" TEXT,
  ADD COLUMN IF NOT EXISTS "data_forensics_handlebars_template" TEXT,
  ADD COLUMN IF NOT EXISTS "clinical_handlebars_template" TEXT;

ALTER TABLE "platform_schema"."tenant_rvw_configs"
  DROP COLUMN IF EXISTS "editorial_rules",
  DROP COLUMN IF EXISTS "data_forensics_level",
  DROP COLUMN IF EXISTS "finer_weights";
