-- Table from approval_instance.json
CREATE TABLE IF NOT EXISTS `approval_instance` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `workflow_id` VARCHAR(32) NOT NULL COMMENT '关联的工作流定义',
  `module_type` VARCHAR(50) NOT NULL COMMENT 'customer/opportunity/contract/financial',
  `module_record_id` VARCHAR(32) NOT NULL COMMENT '关联的具体业务记录ID',
  `current_step_id` VARCHAR(32) COMMENT '当前待审批的步骤',
  `status` VARCHAR(20) NOT NULL COMMENT 'pending/approved/rejected/cancelled',
  `initiator_id` VARCHAR(32) NOT NULL COMMENT '审批发起人用户ID',
  `title` VARCHAR(200) NOT NULL COMMENT '审批标题',
  `description` VARCHAR(1000) COMMENT '审批详细描述',
  `org_id` VARCHAR(32) NOT NULL COMMENT '多租户组织隔离',
  `created_at` TIMESTAMP NOT NULL COMMENT '创建时间',
  `completed_at` TIMESTAMP COMMENT '完成时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批实例';

CREATE INDEX `idx_instance_workflow` ON `approval_instance` (`workflow_id`);
CREATE INDEX `idx_instance_module` ON `approval_instance` (`module_type`, `module_record_id`);
CREATE INDEX `idx_instance_status` ON `approval_instance` (`status`);
CREATE INDEX `idx_instance_org` ON `approval_instance` (`org_id`);

-- Table from approval_step.json
CREATE TABLE IF NOT EXISTS `approval_step` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `workflow_id` VARCHAR(32) NOT NULL COMMENT '关联的工作流',
  `step_name` VARCHAR(100) NOT NULL COMMENT '审批步骤名称',
  `step_order` INT NOT NULL COMMENT '步骤执行顺序',
  `approver_type` VARCHAR(20) NOT NULL COMMENT 'role/user/department/dynamic',
  `approver_value` VARCHAR(100) COMMENT '角色ID/用户ID/部门ID/动态表达式',
  `approval_type` VARCHAR(20) NOT NULL COMMENT 'single/multiple/sequential/parallel',
  `timeout_hours` INT COMMENT '审批超时时间（小时）',
  `description` VARCHAR(500) COMMENT '步骤描述',
  `org_id` VARCHAR(32) NOT NULL COMMENT '多租户组织隔离',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批步骤';

CREATE INDEX `idx_step_workflow` ON `approval_step` (`workflow_id`);
CREATE INDEX `idx_step_order` ON `approval_step` (`workflow_id`, `step_order`);

-- Table from approval_task.json
CREATE TABLE IF NOT EXISTS `approval_task` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `instance_id` VARCHAR(32) NOT NULL COMMENT '关联的审批实例',
  `step_id` VARCHAR(32) NOT NULL COMMENT '关联的审批步骤',
  `approver_id` VARCHAR(32) NOT NULL COMMENT '具体审批人用户ID',
  `status` VARCHAR(20) NOT NULL COMMENT 'pending/approved/rejected',
  `decision` VARCHAR(1000) COMMENT '审批意见',
  `org_id` VARCHAR(32) NOT NULL COMMENT '多租户组织隔离',
  `assigned_at` TIMESTAMP NOT NULL COMMENT '任务分配时间',
  `completed_at` TIMESTAMP COMMENT '任务完成时间',
  `due_at` TIMESTAMP COMMENT '任务截止时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批任务';

CREATE INDEX `idx_task_instance` ON `approval_task` (`instance_id`);
CREATE INDEX `idx_task_approver` ON `approval_task` (`approver_id`);
CREATE INDEX `idx_task_status` ON `approval_task` (`status`);

-- Table from approval_workflow.json
CREATE TABLE IF NOT EXISTS `approval_workflow` (
  `id` VARCHAR(32) NOT NULL COMMENT '主键UUID',
  `workflow_name` VARCHAR(100) NOT NULL COMMENT '审批工作流名称',
  `module_type` VARCHAR(50) NOT NULL COMMENT '关联的模块类型(customer/opportunity/contract/financial)',
  `trigger_condition` VARCHAR(500) COMMENT 'JSON格式的触发条件表达式',
  `description` VARCHAR(500) COMMENT '工作流描述',
  `org_id` VARCHAR(32) NOT NULL COMMENT '多租户组织隔离',
  `created_at` TIMESTAMP NOT NULL COMMENT '创建时间',
  `updated_at` TIMESTAMP NOT NULL COMMENT '更新时间',
  `is_active` VARCHAR(1) NOT NULL COMMENT 'Y/N',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='审批工作流';

CREATE INDEX `idx_workflow_org` ON `approval_workflow` (`org_id`);
CREATE INDEX `idx_workflow_module` ON `approval_workflow` (`module_type`);
CREATE UNIQUE INDEX `uk_workflow_name_org` ON `approval_workflow` (`workflow_name`, `org_id`);
