-- Migration 104: Add source tracking to worker_tasks
-- Purpose: Track WHERE tasks are created from (schedule vs API endpoint)
--
-- All automated task creation should be visible in task_schedules.
-- This column helps identify "phantom" tasks created outside the schedule system.

-- Add source column to worker_tasks
ALTER TABLE worker_tasks
ADD COLUMN IF NOT EXISTS source VARCHAR(100);

-- Add source_id column (references schedule_id if from a schedule)
ALTER TABLE worker_tasks
ADD COLUMN IF NOT EXISTS source_schedule_id INTEGER REFERENCES task_schedules(id);

-- Add request metadata (IP, user agent) for debugging
ALTER TABLE worker_tasks
ADD COLUMN IF NOT EXISTS source_metadata JSONB;

-- Create index for querying by source
CREATE INDEX IF NOT EXISTS idx_worker_tasks_source ON worker_tasks(source);

-- Comment explaining source values
COMMENT ON COLUMN worker_tasks.source IS 'Task creation source: schedule, api_run_now, api_crawl_state, api_batch_staggered, api_batch_az_stores, task_chain, manual';
COMMENT ON COLUMN worker_tasks.source_schedule_id IS 'ID of the schedule that created this task (if source=schedule or source=api_run_now)';
COMMENT ON COLUMN worker_tasks.source_metadata IS 'Request metadata: {ip, user_agent, endpoint, timestamp}';
