CREATE TABLE IF NOT EXISTS `asr_bas_station_path_profile` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
  `profile_code` VARCHAR(64) NOT NULL COMMENT '模板编码',
  `profile_name` VARCHAR(128) NOT NULL COMMENT '模板名称',
  `priority` INT NOT NULL DEFAULT 100 COMMENT '优先级，越小越优先',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态 1启用 0禁用',
  `config_json` LONGTEXT NULL COMMENT '模板参数JSON',
  `memo` VARCHAR(255) NULL COMMENT '备注',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_asr_bas_station_path_profile_code` (`profile_code`),
  KEY `idx_asr_bas_station_path_profile_status_priority` (`status`, `priority`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='输送站点路径评分模板';

CREATE TABLE IF NOT EXISTS `asr_bas_station_path_rule` (
  `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键',
  `rule_code` VARCHAR(64) NOT NULL COMMENT '规则编码',
  `rule_name` VARCHAR(128) NOT NULL COMMENT '规则名称',
  `priority` INT NOT NULL DEFAULT 100 COMMENT '优先级，越小越优先',
  `status` TINYINT NOT NULL DEFAULT 1 COMMENT '状态 1启用 0禁用',
  `scene_type` VARCHAR(32) NULL COMMENT '场景类型',
  `start_station_id` INT NULL COMMENT '起点站点ID，空表示通配',
  `end_station_id` INT NULL COMMENT '终点站点ID，空表示通配',
  `profile_code` VARCHAR(64) NULL COMMENT '绑定模板编码',
  `hard_json` LONGTEXT NULL COMMENT '硬约束JSON',
  `waypoint_json` LONGTEXT NULL COMMENT '关键途经点JSON',
  `soft_json` LONGTEXT NULL COMMENT '软偏好JSON',
  `fallback_json` LONGTEXT NULL COMMENT '降级策略JSON',
  `memo` VARCHAR(255) NULL COMMENT '备注',
  `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_asr_bas_station_path_rule_code` (`rule_code`),
  KEY `idx_asr_bas_station_path_rule_match` (`status`, `start_station_id`, `end_station_id`, `priority`),
  KEY `idx_asr_bas_station_path_rule_profile` (`profile_code`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='输送站点路径人工规则';

INSERT INTO `asr_bas_station_path_profile`
(`profile_code`, `profile_name`, `priority`, `status`, `config_json`, `memo`)
SELECT
  'default',
  '默认两阶段评分模板',
  100,
  1,
  '{"calcMaxDepth":120,"calcMaxPaths":500,"calcMaxCost":300,"s1TopK":5,"s1LenWeight":1.0,"s1TurnWeight":3.0,"s1LiftWeight":8.0,"s1SoftDeviationWeight":4.0,"s1MaxLenRatio":1.15,"s1MaxTurnDiff":1,"s2BusyWeight":2.0,"s2RunBlockWeight":10.0,"s2LoopLoadWeight":12.0}',
  '默认模板，未命中规则时兜底'
FROM dual
WHERE NOT EXISTS (
  SELECT 1 FROM `asr_bas_station_path_profile` WHERE `profile_code` = 'default'
);

INSERT INTO `sys_config`(`name`, `code`, `value`, `type`, `status`, `select_type`)
SELECT '站点路径评分模式', 'stationPathScoreMode', 'legacy', 1, 1, 'system'
FROM dual
WHERE NOT EXISTS (
  SELECT 1 FROM `sys_config` WHERE `code` = 'stationPathScoreMode'
);

INSERT INTO `sys_config`(`name`, `code`, `value`, `type`, `status`, `select_type`)
SELECT '站点路径默认模板编码', 'stationPathDefaultProfileCode', 'default', 1, 1, 'system'
FROM dual
WHERE NOT EXISTS (
  SELECT 1 FROM `sys_config` WHERE `code` = 'stationPathDefaultProfileCode'
);
