CREATE TABLE generation_tasks (
    id                   BIGSERIAL PRIMARY KEY,
    tenant_id            BIGINT NOT NULL REFERENCES tenants(id),
    article_id           BIGINT REFERENCES articles(id),
    task_batch_id        VARCHAR(100),
    quota_reservation_id BIGINT REFERENCES quota_reservations(id),
    task_type            VARCHAR(50) NOT NULL,
    request_hash         VARCHAR(64),
    input_params_json    JSONB,
    status               VARCHAR(20) NOT NULL DEFAULT 'queued',
    error_message        TEXT,
    started_at           TIMESTAMPTZ,
    completed_at         TIMESTAMPTZ,
    created_at           TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    updated_at           TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE INDEX idx_generation_task_status_created ON generation_tasks(status, created_at);
CREATE INDEX idx_generation_task_tenant ON generation_tasks(tenant_id, created_at DESC);
