CREATE TABLE schedule_tasks (
    id              BIGSERIAL PRIMARY KEY,
    tenant_id       BIGINT NOT NULL REFERENCES tenants(id),
    prompt_rule_id  BIGINT NOT NULL REFERENCES prompt_rules(id),
    brand_id        BIGINT REFERENCES brands(id),
    name            VARCHAR(100) NOT NULL,
    cron_expr       VARCHAR(100) NOT NULL,
    start_at        TIMESTAMPTZ,
    end_at          TIMESTAMPTZ,
    next_run_at     TIMESTAMPTZ,
    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_schedule_task_tenant ON schedule_tasks(tenant_id, created_at DESC);
CREATE INDEX idx_schedule_task_next_run ON schedule_tasks(status, next_run_at) WHERE deleted_at IS NULL;
