-- easyai:migration:no-transaction
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_tasks_retention
  ON gateway_tasks(finished_at, id)
  WHERE status IN ('succeeded', 'failed', 'cancelled');

-- easyai:migration:statement
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_task_attempts_retention
  ON gateway_task_attempts((COALESCE(finished_at, started_at)), id)
  WHERE status <> 'running';

-- easyai:migration:statement
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_task_events_retention
  ON gateway_task_events(created_at, id);

-- easyai:migration:statement
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_task_param_logs_retention
  ON gateway_task_param_preprocessing_logs(created_at, id);

-- easyai:migration:statement
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_tasks_minimal_compaction
  ON gateway_tasks(updated_at, id)
  WHERE COALESCE(normalized_request, '{}'::jsonb) <> '{}'::jsonb
     OR compatibility_public_id IS NOT NULL
     OR compatibility_submit_http_status IS NOT NULL
     OR COALESCE(compatibility_submit_headers, '{}'::jsonb) <> '{}'::jsonb
     OR COALESCE(compatibility_submit_body, '{}'::jsonb) <> '{}'::jsonb
     OR result ?| ARRAY[
       'raw', 'raw_data', 'rawData', 'provider_response', 'providerResponse',
       'submit', 'file_retrieve', 'fileRetrieve', 'upstream_task_id', 'remote_task_id'
     ]
     OR error IS NOT NULL
     OR octet_length(COALESCE(error_message, '')) > 2048;

-- easyai:migration:statement
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_tasks_checkpoint_compaction
  ON gateway_tasks(updated_at, id)
  WHERE COALESCE(remote_task_payload, '{}'::jsonb) <> '{}'::jsonb;

-- easyai:migration:statement
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_task_attempts_minimal_compaction
  ON gateway_task_attempts(started_at, id)
  WHERE request_snapshot <> '{}'::jsonb
     OR COALESCE(response_snapshot, '{}'::jsonb) <> '{}'::jsonb
     OR COALESCE(usage, '{}'::jsonb) <> '{}'::jsonb
     OR COALESCE(metrics, '{}'::jsonb) <> '{}'::jsonb
     OR COALESCE(pricing_snapshot, '{}'::jsonb) <> '{}'::jsonb
     OR request_fingerprint IS NOT NULL;

-- easyai:migration:statement
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_task_events_minimal_compaction
  ON gateway_task_events(created_at, id)
  WHERE payload <> '{}'::jsonb
     OR phase IS NOT NULL
     OR COALESCE(progress, 0) <> 0
     OR message IS NOT NULL;

-- easyai:migration:statement
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_task_param_logs_minimal_compaction
  ON gateway_task_param_preprocessing_logs(created_at, id)
  WHERE actual_input <> '{}'::jsonb
     OR converted_output <> '{}'::jsonb
     OR model_snapshot <> '{}'::jsonb
     OR octet_length(changes::text) > 1024;

-- easyai:migration:statement
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_gateway_cloned_voices_minimal_compaction
  ON gateway_cloned_voices(updated_at, id)
  WHERE metadata ?| ARRAY['raw', 'raw_data', 'rawData', 'provider_response', 'providerResponse', 'request'];
