/*create database beacon DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
create user 'beacon'@'%' identified by '5VR2Fz7gAmKF0bv7';
grant all privileges on beacon.* TO 'beacon'@'%';
FLUSH PRIVILEGES;*/

-- 复合案例定义表
CREATE TABLE `composite_cases`
(
    `id`          BIGINT        NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键',
    `created_at`  DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `created_by`  VARCHAR(64)   NOT NULL DEFAULT '' COMMENT '创建者',
    `updated_at`  DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `updated_by`  VARCHAR(64)   NOT NULL DEFAULT '' COMMENT '更新者',
    `version`     SMALLINT      NOT NULL DEFAULT '0' COMMENT '乐观锁版本号',
    `is_del`      TINYINT       NOT NULL DEFAULT '0' COMMENT '是否删除：0正常 1删除',
    `ext_json`    VARCHAR(2048) NOT NULL DEFAULT '' COMMENT '扩展字段',

    `name`        VARCHAR(255)  NOT NULL COMMENT '案例名称，例如：用户注册与登录流程',
    `description` TEXT          NULL COMMENT '案例描述',
    `status`      VARCHAR(50)            DEFAULT 'active',

    INDEX idx_name (`name`),
    INDEX idx_create_at (`created_at`)
) ENGINE = INNODB
  DEFAULT CHARSET = utf8mb4 COMMENT = '复合案例定义表';

-- 复合案例步骤表
CREATE TABLE `composite_case_steps`
(
    `id`                      BIGINT        NOT NULL AUTO_INCREMENT PRIMARY KEY COMMENT '主键',
    `created_at`              DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
    `created_by`              VARCHAR(64)   NOT NULL DEFAULT '' COMMENT '创建者',
    `updated_at`              DATETIME      NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    `updated_by`              VARCHAR(64)   NOT NULL DEFAULT '' COMMENT '更新者',
    `version`                 SMALLINT      NOT NULL DEFAULT '0' COMMENT '乐观锁版本号',
    `is_del`                  TINYINT       NOT NULL DEFAULT '0' COMMENT '是否删除：0正常 1删除',
    `ext_json`                VARCHAR(2048) NOT NULL DEFAULT '' COMMENT '扩展字段',

    `composite_case_id`       BIGINT        NOT NULL COMMENT '所属复合案例ID',
    `step_name`               VARCHAR(128)  NOT NULL COMMENT '步骤名称',
    `step_order`              INT           NOT NULL COMMENT '步骤执行顺序',
    `step_type`               VARCHAR(64)   NOT NULL COMMENT '步骤类型，例如：api, ui, sql, python',
    `activity_name`           VARCHAR(128)  NOT NULL COMMENT 'Temporal Activity函数名，例如：RunApiTest, RunUiTest',
    `parameters_json`         JSON          NULL COMMENT '步骤特定参数，例如：{"endpoint": "/users", "method": "GET"}',
    `is_required`             BOOLEAN       DEFAULT TRUE COMMENT '是否必需步骤',
    `step_description`        TEXT          NULL COMMENT '步骤描述',
    `success_next_step_order` INT           NULL COMMENT '成功时跳转到的步骤顺序',
    `failure_next_step_order` INT           NULL COMMENT '失败时跳转到的步骤顺序',
    `run_condition`           JSON          NULL COMMENT '执行条件，例如：{"previous_step_id": "step_xyz", "status": "success"}',

    FOREIGN KEY (`composite_case_id`) REFERENCES `composite_cases` (`id`) ON DELETE CASCADE,
    INDEX idx_composite_case_id (`composite_case_id`),
    INDEX idx_step_order (`composite_case_id`, `step_order`),
    INDEX idx_step_type (`step_type`),
    UNIQUE KEY uk_case_step_order (`composite_case_id`, `step_order`)
) ENGINE = INNODB
  DEFAULT CHARSET = utf8mb4 COMMENT = '复合案例步骤表';

ALTER TABLE composite_cases
    ADD COLUMN deleted_at DATETIME NULL AFTER updated_at;
