-- Report review / publish flow schema migration.
-- Navicat friendly: no DELIMITER, no stored procedure.
-- Usage:
--   Select the target database first, then run this whole file.

SELECT 'START schema_upgrade_report_review_flow' AS migration_step;

SET @reports_exists := (
  SELECT COUNT(*)
    FROM information_schema.TABLES
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'reports'
);
SELECT IF(@reports_exists = 0, 'ERROR reports table does not exist', 'OK reports table exists') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'reports'
     AND COLUMN_NAME = 'reject_reason'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE reports ADD COLUMN reject_reason VARCHAR(500) NOT NULL DEFAULT '''' AFTER invalid_reason',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN reports.reject_reason', 'SKIP_COLUMN reports.reject_reason') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'reports'
     AND COLUMN_NAME = 'rejected_by'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE reports ADD COLUMN rejected_by BIGINT UNSIGNED NULL DEFAULT NULL AFTER reject_reason',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN reports.rejected_by', 'SKIP_COLUMN reports.rejected_by') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'reports'
     AND COLUMN_NAME = 'rejected_by_name'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE reports ADD COLUMN rejected_by_name VARCHAR(64) NOT NULL DEFAULT '''' AFTER rejected_by',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN reports.rejected_by_name', 'SKIP_COLUMN reports.rejected_by_name') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'reports'
     AND COLUMN_NAME = 'rejected_at'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE reports ADD COLUMN rejected_at DATETIME NULL DEFAULT NULL AFTER rejected_by_name',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN reports.rejected_at', 'SKIP_COLUMN reports.rejected_at') AS migration_step;

UPDATE reports
   SET reject_reason = invalid_reason
 WHERE report_status = 'rejected'
   AND reject_reason = ''
   AND invalid_reason <> '';
SELECT CONCAT('SYNC_REJECT_REASON affected_rows=', ROW_COUNT()) AS migration_step;

CREATE TABLE IF NOT EXISTS report_logs (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  report_id BIGINT UNSIGNED NOT NULL,
  action VARCHAR(64) NOT NULL,
  operator_id BIGINT UNSIGNED NULL DEFAULT NULL,
  operator_name VARCHAR(64) NOT NULL DEFAULT '',
  before_data LONGTEXT NULL,
  after_data LONGTEXT NULL,
  remark VARCHAR(255) NOT NULL DEFAULT '',
  created_at DATETIME NULL DEFAULT NULL,
  PRIMARY KEY (id),
  KEY idx_report_logs_report_id (report_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='报告操作日志';
SELECT 'ENSURE_TABLE report_logs' AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'report_logs'
     AND COLUMN_NAME = 'report_id'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE report_logs ADD COLUMN report_id BIGINT UNSIGNED NOT NULL AFTER id',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.report_id', 'SKIP_COLUMN report_logs.report_id') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'report_logs'
     AND COLUMN_NAME = 'action'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE report_logs ADD COLUMN action VARCHAR(64) NOT NULL DEFAULT '''' AFTER report_id',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.action', 'SKIP_COLUMN report_logs.action') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'report_logs'
     AND COLUMN_NAME = 'operator_id'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE report_logs ADD COLUMN operator_id BIGINT UNSIGNED NULL DEFAULT NULL AFTER action',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.operator_id', 'SKIP_COLUMN report_logs.operator_id') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'report_logs'
     AND COLUMN_NAME = 'operator_name'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE report_logs ADD COLUMN operator_name VARCHAR(64) NOT NULL DEFAULT '''' AFTER operator_id',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.operator_name', 'SKIP_COLUMN report_logs.operator_name') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'report_logs'
     AND COLUMN_NAME = 'before_data'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE report_logs ADD COLUMN before_data LONGTEXT NULL AFTER operator_name',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.before_data', 'SKIP_COLUMN report_logs.before_data') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'report_logs'
     AND COLUMN_NAME = 'after_data'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE report_logs ADD COLUMN after_data LONGTEXT NULL AFTER before_data',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.after_data', 'SKIP_COLUMN report_logs.after_data') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'report_logs'
     AND COLUMN_NAME = 'remark'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE report_logs ADD COLUMN remark VARCHAR(255) NOT NULL DEFAULT '''' AFTER after_data',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.remark', 'SKIP_COLUMN report_logs.remark') AS migration_step;

SET @column_exists := (
  SELECT COUNT(*)
    FROM information_schema.COLUMNS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'report_logs'
     AND COLUMN_NAME = 'created_at'
);
SET @sql := IF(
  @column_exists = 0,
  'ALTER TABLE report_logs ADD COLUMN created_at DATETIME NULL DEFAULT NULL AFTER remark',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@column_exists = 0, 'ADD_COLUMN report_logs.created_at', 'SKIP_COLUMN report_logs.created_at') AS migration_step;

SET @index_exists := (
  SELECT COUNT(*)
    FROM information_schema.STATISTICS
   WHERE TABLE_SCHEMA = DATABASE()
     AND TABLE_NAME = 'report_logs'
     AND INDEX_NAME = 'idx_report_logs_report_id'
);
SET @sql := IF(
  @index_exists = 0,
  'ALTER TABLE report_logs ADD KEY idx_report_logs_report_id (report_id)',
  'DO 0'
);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT IF(@index_exists = 0, 'ADD_INDEX report_logs.idx_report_logs_report_id', 'SKIP_INDEX report_logs.idx_report_logs_report_id') AS migration_step;

SELECT 'SCHEMA_UPGRADE_REPORT_REVIEW_FLOW_OK' AS migration_step;
