SET NAMES utf8mb4;

DROP PROCEDURE IF EXISTS `sp_add_column_if_missing`;

DELIMITER $$
CREATE PROCEDURE `sp_add_column_if_missing`(
    IN in_table_name VARCHAR(128),
    IN in_column_name VARCHAR(128),
    IN in_definition TEXT
)
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM information_schema.COLUMNS
        WHERE TABLE_SCHEMA = DATABASE()
          AND TABLE_NAME = in_table_name
          AND COLUMN_NAME = in_column_name
    ) THEN
        SET @ddl = CONCAT('ALTER TABLE `', in_table_name, '` ADD COLUMN ', in_definition);
        PREPARE stmt FROM @ddl;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END IF;
END $$
DELIMITER ;

CALL `sp_add_column_if_missing`(
    'tb_approval_flow_step',
    'assignee_type',
    '`assignee_type` varchar(16) DEFAULT NULL COMMENT ''审批对象类型'' AFTER `step_name`'
);
CALL `sp_add_column_if_missing`(
    'tb_approval_flow_step',
    'assignee_target_id',
    '`assignee_target_id` bigint DEFAULT NULL COMMENT ''审批对象ID'' AFTER `assignee_type`'
);
CALL `sp_add_column_if_missing`(
    'tb_approval_flow_step',
    'assignee_target_code',
    '`assignee_target_code` varchar(128) DEFAULT NULL COMMENT ''审批对象编码'' AFTER `assignee_target_id`'
);
CALL `sp_add_column_if_missing`(
    'tb_approval_flow_step',
    'assignee_target_name',
    '`assignee_target_name` varchar(128) DEFAULT NULL COMMENT ''审批对象名称'' AFTER `assignee_target_code`'
);

ALTER TABLE `tb_approval_task`
  MODIFY COLUMN `assignee_role_code` varchar(64) DEFAULT NULL COMMENT '指派角色编码';

CALL `sp_add_column_if_missing`(
    'tb_approval_task',
    'assignee_type',
    '`assignee_type` varchar(16) DEFAULT NULL COMMENT ''审批对象类型'' AFTER `assignee_role_code`'
);
CALL `sp_add_column_if_missing`(
    'tb_approval_task',
    'assignee_target_id',
    '`assignee_target_id` bigint DEFAULT NULL COMMENT ''审批对象ID'' AFTER `assignee_type`'
);
CALL `sp_add_column_if_missing`(
    'tb_approval_task',
    'assignee_target_code',
    '`assignee_target_code` varchar(128) DEFAULT NULL COMMENT ''审批对象编码'' AFTER `assignee_target_id`'
);
CALL `sp_add_column_if_missing`(
    'tb_approval_task',
    'assignee_target_name',
    '`assignee_target_name` varchar(128) DEFAULT NULL COMMENT ''审批对象名称'' AFTER `assignee_target_code`'
);

DROP PROCEDURE IF EXISTS `sp_patch_approval_assignee`;
DELIMITER $$
CREATE PROCEDURE `sp_patch_approval_assignee`()
BEGIN
  DECLARE v_super_role_id BIGINT;
  DECLARE v_super_role_code VARCHAR(128);
  DECLARE v_super_role_name VARCHAR(128);
  DECLARE v_index_count INT DEFAULT 0;

  SELECT `id`, `role_key`, `role_name`
  INTO v_super_role_id, v_super_role_code, v_super_role_name
  FROM `tb_sys_role`
  WHERE `role_key` = 'super_admin'
  LIMIT 1;

  IF v_super_role_id IS NULL THEN
    SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'super_admin role is required for approval assignee backfill';
  END IF;

  UPDATE `tb_approval_flow_step`
  SET `assignee_type` = 'ROLE',
      `assignee_target_id` = v_super_role_id,
      `assignee_target_code` = v_super_role_code,
      `assignee_target_name` = v_super_role_name
  WHERE `assignee_type` IS NULL
     OR `assignee_target_id` IS NULL;

  UPDATE `tb_approval_task`
  SET `assignee_role_code` = IFNULL(`assignee_role_code`, v_super_role_code),
      `assignee_type` = 'ROLE',
      `assignee_target_id` = v_super_role_id,
      `assignee_target_code` = v_super_role_code,
      `assignee_target_name` = v_super_role_name
  WHERE `assignee_type` IS NULL
     OR `assignee_target_id` IS NULL;

  SELECT COUNT(1)
  INTO v_index_count
  FROM `information_schema`.`statistics`
  WHERE `table_schema` = DATABASE()
    AND `table_name` = 'tb_approval_task'
    AND `index_name` = 'idx_approval_task_assignee';

  IF v_index_count = 0 THEN
    ALTER TABLE `tb_approval_task`
      ADD INDEX `idx_approval_task_assignee` (`status`, `assignee_type`, `assignee_target_id`);
  END IF;
END$$
DELIMITER ;

CALL `sp_patch_approval_assignee`();
DROP PROCEDURE IF EXISTS `sp_patch_approval_assignee`;
DROP PROCEDURE IF EXISTS `sp_add_column_if_missing`;
