-- Async task table schema migration
-- Add new columns for enhanced task tracking and retry functionality

-- Add new columns to async_task table
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS started_at bigint NULL;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS execution_time bigint NULL;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS retry_count integer NOT NULL DEFAULT 0;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS max_retry_count integer NOT NULL DEFAULT 3;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS next_retry_time bigint NULL;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS retry_delay integer NOT NULL DEFAULT 5000;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS priority integer NOT NULL DEFAULT 5;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS queue_name varchar(100) NOT NULL DEFAULT 'default';
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS worker_id varchar(50) NULL;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS failure_reason text NULL;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS stack_trace text NULL;
ALTER TABLE async_task ADD COLUMN IF NOT EXISTS created_by varchar(100) NULL;

-- Add new columns to async_task_result table
ALTER TABLE async_task_result ADD COLUMN IF NOT EXISTS execution_time bigint NOT NULL DEFAULT 0;
ALTER TABLE async_task_result ADD COLUMN IF NOT EXISTS memory_usage bigint NULL;
ALTER TABLE async_task_result ADD COLUMN IF NOT EXISTS cpu_usage double precision NULL;
ALTER TABLE async_task_result ADD COLUMN IF NOT EXISTS retry_count integer NOT NULL DEFAULT 0;
ALTER TABLE async_task_result ADD COLUMN IF NOT EXISTS completed_at bigint NOT NULL DEFAULT 0;

-- Add indexes for improved query performance
CREATE INDEX IF NOT EXISTS idx_async_task_status_priority ON async_task(status, priority DESC);
CREATE INDEX IF NOT EXISTS idx_async_task_next_retry_time ON async_task(next_retry_time) WHERE status = 'FAILED';
CREATE INDEX IF NOT EXISTS idx_async_task_created_by ON async_task(created_by);
CREATE INDEX IF NOT EXISTS idx_async_task_task_type ON async_task(task_type);
CREATE INDEX IF NOT EXISTS idx_async_task_started_at ON async_task(started_at) WHERE started_at IS NOT NULL;

-- Update existing rows to have default values for new columns
UPDATE async_task SET priority = 5 WHERE priority IS NULL;
UPDATE async_task SET queue_name = 'default' WHERE queue_name IS NULL;
UPDATE async_task SET retry_count = 0 WHERE retry_count IS NULL;
UPDATE async_task SET max_retry_count = 3 WHERE max_retry_count IS NULL;
UPDATE async_task SET retry_delay = 5000 WHERE retry_delay IS NULL;

-- Add comments for new columns
COMMENT ON COLUMN async_task.started_at IS 'Timestamp when task execution started (Unix epoch seconds)';
COMMENT ON COLUMN async_task.execution_time IS 'Task execution time in milliseconds';
COMMENT ON COLUMN async_task.retry_count IS 'Number of retry attempts for failed tasks';
COMMENT ON COLUMN async_task.max_retry_count IS 'Maximum number of retry attempts allowed';
COMMENT ON COLUMN async_task.next_retry_time IS 'Next retry timestamp (Unix epoch seconds)';
COMMENT ON COLUMN async_task.retry_delay IS 'Delay between retries in milliseconds';
COMMENT ON COLUMN async_task.priority IS 'Task priority (1-10, higher is more important)';
COMMENT ON COLUMN async_task.queue_name IS 'Name of the queue the task belongs to';
COMMENT ON COLUMN async_task.worker_id IS 'ID of the worker processing the task';
COMMENT ON COLUMN async_task.failure_reason IS 'Reason for task failure';
COMMENT ON COLUMN async_task.stack_trace IS 'Stack trace for debugging failed tasks';
COMMENT ON COLUMN async_task.created_by IS 'User or system that created the task';

COMMENT ON COLUMN async_task_result.execution_time IS 'Total execution time in milliseconds';
COMMENT ON COLUMN async_task_result.memory_usage IS 'Memory usage in bytes';
COMMENT ON COLUMN async_task_result.cpu_usage IS 'CPU usage percentage';
COMMENT ON COLUMN async_task_result.retry_count IS 'Number of retries before success';
COMMENT ON COLUMN async_task_result.completed_at IS 'Timestamp when task completed (Unix epoch seconds)';