CREATE TABLE jobs (
    job_id          UUID PRIMARY KEY,
    job_type        TEXT NOT NULL,
    target_asset_id UUID,
    batch_id        UUID,
    status          TEXT NOT NULL DEFAULT 'queued',
    priority        INTEGER NOT NULL DEFAULT 0,
    payload         JSONB NOT NULL DEFAULT '{}',
    result_data     JSONB,
    retry_count     INTEGER NOT NULL DEFAULT 0,
    max_retries     INTEGER NOT NULL DEFAULT 3,
    created_at      TIMESTAMPTZ NOT NULL DEFAULT NOW(),
    started_at      TIMESTAMPTZ,
    completed_at    TIMESTAMPTZ,
    error_message   TEXT
);

CREATE INDEX idx_jobs_status_priority ON jobs(status, priority DESC);
CREATE INDEX idx_jobs_batch ON jobs(batch_id);

CREATE TABLE job_batches (
    batch_id        UUID PRIMARY KEY,
    batch_type      TEXT NOT NULL,
    total_jobs      INTEGER NOT NULL DEFAULT 0,
    completed_count INTEGER NOT NULL DEFAULT 0,
    failed_count    INTEGER NOT NULL DEFAULT 0,
    status          TEXT NOT NULL DEFAULT 'in_progress'
);

CREATE TABLE plugins (
    plugin_id     UUID PRIMARY KEY,
    name          TEXT NOT NULL,
    plugin_type   TEXT NOT NULL,
    is_enabled    BOOLEAN NOT NULL DEFAULT true,
    configuration JSONB NOT NULL DEFAULT '{}'
);

CREATE TABLE processing_pipelines (
    pipeline_id   UUID PRIMARY KEY,
    trigger_event TEXT NOT NULL,
    steps         JSONB NOT NULL DEFAULT '[]'
);
