-- P1: Align processor_results with Worker expectations
-- This table tracks processor execution per job.

ALTER TABLE dev.processor_results ADD COLUMN IF NOT EXISTS job_id INTEGER REFERENCES dev.monitor_jobs(id);
ALTER TABLE dev.processor_results ADD COLUMN IF NOT EXISTS processor VARCHAR(64);
ALTER TABLE dev.processor_results ADD COLUMN IF NOT EXISTS output_path TEXT;
ALTER TABLE dev.processor_results ADD COLUMN IF NOT EXISTS progress_total INTEGER DEFAULT 0;
ALTER TABLE dev.processor_results ADD COLUMN IF NOT EXISTS progress_current INTEGER DEFAULT 0;
ALTER TABLE dev.processor_results ADD COLUMN IF NOT EXISTS last_checkpoint TIMESTAMP WITH TIME ZONE;
ALTER TABLE dev.processor_results ADD COLUMN IF NOT EXISTS updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP;
ALTER TABLE dev.processor_results ADD COLUMN IF NOT EXISTS duration_secs DOUBLE PRECISION;

-- Map old processor_type to processor if empty
UPDATE dev.processor_results SET processor = processor_type WHERE processor IS NULL AND processor_type IS NOT NULL;

-- Add unique constraint for upsert logic
ALTER TABLE dev.processor_results ADD CONSTRAINT uq_processor_results_job_processor UNIQUE (job_id, processor);

CREATE INDEX IF NOT EXISTS idx_processor_results_job ON dev.processor_results(job_id);
CREATE INDEX IF NOT EXISTS idx_processor_results_status ON dev.processor_results(status);
