-- RVW V4.0：新增期刊租户级审稿配置表
-- 每个期刊租户独立配置稿约规则、方法学/临床专家提示词、Handlebars 展示模板
-- Migration: 20260314_add_tenant_rvw_configs

CREATE TABLE "platform_schema"."tenant_rvw_configs" (
    "id"                              TEXT NOT NULL,
    "tenant_id"                       TEXT NOT NULL,
    "editorial_rules"                 JSONB,
    "methodology_expert_prompt"       TEXT,
    "methodology_handlebars_template" TEXT,
    "data_forensics_level"            TEXT NOT NULL DEFAULT 'L2',
    "finer_weights"                   JSONB,
    "clinical_expert_prompt"          TEXT,
    "created_at"                      TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "updated_at"                      TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,

    CONSTRAINT "tenant_rvw_configs_pkey" PRIMARY KEY ("id")
);

-- Unique constraint: 每个租户只有一份配置
CREATE UNIQUE INDEX "tenant_rvw_configs_tenant_id_key"
    ON "platform_schema"."tenant_rvw_configs"("tenant_id");

-- Foreign key to tenants（同 schema 内，可用标准外键）
ALTER TABLE "platform_schema"."tenant_rvw_configs"
    ADD CONSTRAINT "tenant_rvw_configs_tenant_id_fkey"
    FOREIGN KEY ("tenant_id") REFERENCES "platform_schema"."tenants"("id")
    ON DELETE CASCADE ON UPDATE CASCADE;
