CREATE TABLE article_templates (
    id                          BIGSERIAL PRIMARY KEY,
    scope                       VARCHAR(20) NOT NULL,
    tenant_id                   BIGINT REFERENCES tenants(id),
    origin_type                 VARCHAR(20) NOT NULL DEFAULT 'platform',
    share_code_id               BIGINT,
    template_key                VARCHAR(100) NOT NULL,
    template_name               VARCHAR(200) NOT NULL,
    schema_json                 JSONB NOT NULL DEFAULT '{}',
    prompt_template             TEXT,
    prompt_visibility           VARCHAR(20) NOT NULL DEFAULT 'visible',
    protected_prompt_asset_key  TEXT,
    card_config_json            JSONB NOT NULL DEFAULT '{}',
    status                      VARCHAR(20) NOT NULL DEFAULT 'active',
    version_no                  INT NOT NULL DEFAULT 1,
    created_at                  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at                  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    deleted_at                  TIMESTAMPTZ
);
CREATE UNIQUE INDEX uk_template_scope_key_version_active ON article_templates(scope, tenant_id, template_key, version_no) WHERE deleted_at IS NULL;
