-- =============================================
-- 赛程编排系统 - 完整测试数据初始化
-- =============================================
USE martial_db;

-- 1. 确保赛事存在并配置了时间
UPDATE martial_competition
SET
    competition_start_time = '2025-11-06 08:00:00',
    competition_end_time = '2025-11-08 18:00:00'
WHERE id = 200;

-- 检查赛事是否存在
SELECT
    '1. 检查赛事' AS step,
    CASE
        WHEN COUNT(*) > 0 THEN CONCAT('✓ 赛事ID=200存在, 名称: ', MAX(competition_name))
        ELSE '✗ 赛事ID=200不存在,请先创建赛事'
    END AS result
FROM martial_competition
WHERE id = 200;

-- 2. 创建场地数据(如果不存在)
INSERT IGNORE INTO martial_venue (id, competition_id, venue_name, venue_type, capacity, create_time, is_deleted)
VALUES
    (1, 200, '一号场地', '主场地', 100, NOW(), 0),
    (2, 200, '二号场地', '副场地', 100, NOW(), 0),
    (3, 200, '三号场地', '副场地', 100, NOW(), 0),
    (4, 200, '四号场地', '副场地', 100, NOW(), 0);

SELECT
    '2. 检查场地' AS step,
    CONCAT('✓ 已有 ', COUNT(*), ' 个场地') AS result
FROM martial_venue
WHERE competition_id = 200 AND is_deleted = 0;

-- 3. 创建项目数据(如果不存在)
INSERT IGNORE INTO martial_project (id, project_name, type, category, estimated_duration, create_time)
VALUES
    (1001, '太极拳集体', 3, '成年组', 5, NOW()),
    (1002, '长拳集体', 3, '成年组', 5, NOW()),
    (1003, '剑术集体', 3, '成年组', 5, NOW()),
    (1004, '刀术集体', 3, '成年组', 5, NOW()),
    (1005, '棍术集体', 3, '少年组', 5, NOW());

SELECT
    '3. 检查项目' AS step,
    CONCAT('✓ 已有 ', COUNT(*), ' 个项目') AS result
FROM martial_project
WHERE id BETWEEN 1001 AND 1005;

-- 4. 创建测试参赛者数据(少量测试数据)
DELETE FROM martial_athlete WHERE competition_id = 200;

INSERT INTO martial_athlete (
    competition_id, project_id, organization, team_name,
    player_name, gender, age, phone, category, create_time, is_deleted
)
VALUES
    -- 太极拳集体 - 队伍1: 少林寺武校 (5人)
    (200, 1001, '少林寺武校', '少林寺武校', '张明远', '男', 25, '13800001001', '成年组', NOW(), 0),
    (200, 1001, '少林寺武校', '少林寺武校', '李华强', '男', 26, '13800001002', '成年组', NOW(), 0),
    (200, 1001, '少林寺武校', '少林寺武校', '王建国', '男', 24, '13800001003', '成年组', NOW(), 0),
    (200, 1001, '少林寺武校', '少林寺武校', '赵小明', '男', 23, '13800001004', '成年组', NOW(), 0),
    (200, 1001, '少林寺武校', '少林寺武校', '刘德华', '男', 27, '13800001005', '成年组', NOW(), 0),

    -- 太极拳集体 - 队伍2: 武当派 (5人)
    (200, 1001, '武当派', '武当派', '陈剑锋', '男', 28, '13800001011', '成年组', NOW(), 0),
    (200, 1001, '武当派', '武当派', '周杰伦', '男', 25, '13800001012', '成年组', NOW(), 0),
    (200, 1001, '武当派', '武当派', '吴彦祖', '男', 26, '13800001013', '成年组', NOW(), 0),
    (200, 1001, '武当派', '武当派', '郑伊健', '男', 24, '13800001014', '成年组', NOW(), 0),
    (200, 1001, '武当派', '武当派', '谢霆锋', '男', 27, '13800001015', '成年组', NOW(), 0),

    -- 长拳集体 - 队伍1: 峨眉派 (5人)
    (200, 1002, '峨眉派', '峨眉派', '小龙女', '女', 22, '13800002001', '成年组', NOW(), 0),
    (200, 1002, '峨眉派', '峨眉派', '黄蓉', '女', 23, '13800002002', '成年组', NOW(), 0),
    (200, 1002, '峨眉派', '峨眉派', '赵敏', '女', 24, '13800002003', '成年组', NOW(), 0),
    (200, 1002, '峨眉派', '峨眉派', '周芷若', '女', 22, '13800002004', '成年组', NOW(), 0),
    (200, 1002, '峨眉派', '峨眉派', '任盈盈', '女', 23, '13800002005', '成年组', NOW(), 0),

    -- 长拳集体 - 队伍2: 华山派 (5人)
    (200, 1002, '华山派', '华山派', '令狐冲', '男', 27, '13800002011', '成年组', NOW(), 0),
    (200, 1002, '华山派', '华山派', '风清扬', '男', 28, '13800002012', '成年组', NOW(), 0),
    (200, 1002, '华山派', '华山派', '岳不群', '男', 29, '13800002013', '成年组', NOW(), 0),
    (200, 1002, '华山派', '华山派', '宁中则', '女', 26, '13800002014', '成年组', NOW(), 0),
    (200, 1002, '华山派', '华山派', '岳灵珊', '女', 24, '13800002015', '成年组', NOW(), 0);

SELECT
    '4. 检查参赛者' AS step,
    CONCAT('✓ 已有 ', COUNT(*), ' 个参赛者 (', COUNT(DISTINCT organization), ' 个队伍)') AS result
FROM martial_athlete
WHERE competition_id = 200 AND is_deleted = 0;

-- 5. 清空旧的编排数据(如果有)
DELETE FROM martial_schedule_participant WHERE schedule_group_id IN (
    SELECT id FROM martial_schedule_group WHERE competition_id = 200
);
DELETE FROM martial_schedule_detail WHERE competition_id = 200;
DELETE FROM martial_schedule_group WHERE competition_id = 200;
DELETE FROM martial_schedule_status WHERE competition_id = 200;

SELECT '5. 清理旧数据' AS step, '✓ 已清空旧的编排数据' AS result;

-- 6. 最终验证
SELECT
    '6. 数据完整性检查' AS step,
    CONCAT(
        '✓ 赛事: ', (SELECT COUNT(*) FROM martial_competition WHERE id = 200),
        ', 场地: ', (SELECT COUNT(*) FROM martial_venue WHERE competition_id = 200 AND is_deleted = 0),
        ', 项目: ', (SELECT COUNT(*) FROM martial_project WHERE id BETWEEN 1001 AND 1005),
        ', 参赛者: ', (SELECT COUNT(*) FROM martial_athlete WHERE competition_id = 200 AND is_deleted = 0)
    ) AS result;

-- 7. 检查赛事时间配置
SELECT
    '7. 赛事时间配置' AS step,
    CONCAT(
        '开始: ', IFNULL(competition_start_time, '未配置'),
        ', 结束: ', IFNULL(competition_end_time, '未配置')
    ) AS result
FROM martial_competition
WHERE id = 200;

SELECT
    '========================================' AS '',
    '✓ 测试数据初始化完成!' AS result,
    '========================================' AS '';

SELECT
    '下一步: 测试API' AS action,
    'curl -X POST http://localhost:8123/martial/schedule/auto-arrange -H "Content-Type: application/json" -d "{\"competitionId\": 200}"' AS command;
