-- AI Monitor: AI-suggested auto-fix feature
--
-- Adds columns to ai_monitor_issues so each finding can carry an
-- AI-proposed wp-cli command that the customer can apply with one click.
-- The status enum tracks the lifecycle: none -> available -> running ->
-- (applied | failed). Output captures stdout/stderr from execWpCli.
--
-- Each ADD COLUMN is wrapped in an information_schema guard so this
-- migration is idempotent and safe to re-run.

USE whp;

-- auto_fix_command --------------------------------------------------------
SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'ai_monitor_issues'
     AND COLUMN_NAME = 'auto_fix_command') = 0,
  'ALTER TABLE ai_monitor_issues ADD COLUMN auto_fix_command VARCHAR(500) DEFAULT NULL',
  'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- auto_fix_description ----------------------------------------------------
SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'ai_monitor_issues'
     AND COLUMN_NAME = 'auto_fix_description') = 0,
  'ALTER TABLE ai_monitor_issues ADD COLUMN auto_fix_description VARCHAR(500) DEFAULT NULL',
  'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- auto_fix_status ---------------------------------------------------------
SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'ai_monitor_issues'
     AND COLUMN_NAME = 'auto_fix_status') = 0,
  "ALTER TABLE ai_monitor_issues ADD COLUMN auto_fix_status ENUM('none','available','running','applied','failed') NOT NULL DEFAULT 'none'",
  'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- auto_fix_applied_at -----------------------------------------------------
SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'ai_monitor_issues'
     AND COLUMN_NAME = 'auto_fix_applied_at') = 0,
  'ALTER TABLE ai_monitor_issues ADD COLUMN auto_fix_applied_at DATETIME DEFAULT NULL',
  'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- auto_fix_applied_by -----------------------------------------------------
SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'ai_monitor_issues'
     AND COLUMN_NAME = 'auto_fix_applied_by') = 0,
  'ALTER TABLE ai_monitor_issues ADD COLUMN auto_fix_applied_by VARCHAR(64) DEFAULT NULL',
  'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;

-- auto_fix_output ---------------------------------------------------------
SET @sql = IF(
  (SELECT COUNT(*) FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'ai_monitor_issues'
     AND COLUMN_NAME = 'auto_fix_output') = 0,
  'ALTER TABLE ai_monitor_issues ADD COLUMN auto_fix_output TEXT DEFAULT NULL',
  'SELECT 1'
);
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
