CREATE TABLE prompt_rule_groups (
    id          BIGSERIAL PRIMARY KEY,
    tenant_id   BIGINT NOT NULL REFERENCES tenants(id),
    name        VARCHAR(100) NOT NULL,
    sort_order  INT NOT NULL DEFAULT 0,
    created_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at  TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    deleted_at  TIMESTAMPTZ
);
CREATE INDEX idx_prompt_rule_group_tenant ON prompt_rule_groups(tenant_id, sort_order);

CREATE TABLE prompt_rules (
    id              BIGSERIAL PRIMARY KEY,
    tenant_id       BIGINT NOT NULL REFERENCES tenants(id),
    group_id        BIGINT REFERENCES prompt_rule_groups(id),
    name            VARCHAR(100) NOT NULL,
    prompt_content  TEXT NOT NULL,
    scene           VARCHAR(50),
    default_tone    VARCHAR(30),
    default_word_count INT,
    status          VARCHAR(20) NOT NULL DEFAULT 'enabled',
    created_at      TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at      TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    deleted_at      TIMESTAMPTZ
);
CREATE INDEX idx_prompt_rule_tenant ON prompt_rules(tenant_id, created_at DESC);
CREATE INDEX idx_prompt_rule_tenant_status ON prompt_rules(tenant_id, status) WHERE deleted_at IS NULL;
CREATE INDEX idx_prompt_rule_group ON prompt_rules(group_id) WHERE deleted_at IS NULL;
