-- V11: 团课预约取消业务场景测试数据
-- 测试场景：
-- 1. 用户张三使用时长卡预约了团课，需要取消预约
-- 2. 用户李四使用次数卡预约了团课，需要取消预约
-- 3. 用户王五使用储值卡预约了团课，需要取消预约

-- ============================================
-- 1. 创建测试会员用户 (张三、李四、王五)
-- ============================================

INSERT INTO member_user (member_no, nickname, phone, gender, is_deleted, created_at, updated_at) VALUES
    ('MEM_TEST_ZHANG', '张三_时长卡用户', '13800002001', 1, false, '2026-06-02 10:00:00', '2026-06-02 10:00:00'),
    ('MEM_TEST_LI', '李四_次数卡用户', '13800002002', 1, false, '2026-06-02 10:00:00', '2026-06-02 10:00:00'),
    ('MEM_TEST_WANG', '王五_储值卡用户', '13800002003', 2, false, '2026-06-02 10:00:00', '2026-06-02 10:00:00');

-- ============================================
-- 2. 确保会员卡类型已存在（如果不存在则创建）
-- ============================================

-- 时长卡类型（30天）
INSERT INTO member_card (member_card_name, member_card_type, member_card_price, member_card_validity_days, member_card_total_times, member_card_amount, member_card_status, extra_config, created_at, updated_at) 
SELECT '30天时长卡', 'TIME_CARD', 299.00, 30, NULL, NULL, 1, '{}', '2026-01-01 00:00:00', '2026-01-01 00:00:00'
WHERE NOT EXISTS (SELECT 1 FROM member_card WHERE member_card_name = '30天时长卡');

-- 次数卡类型（10次）
INSERT INTO member_card (member_card_name, member_card_type, member_card_price, member_card_validity_days, member_card_total_times, member_card_amount, member_card_status, extra_config, created_at, updated_at) 
SELECT '10次卡', 'COUNT_CARD', 499.00, NULL, 10, NULL, 1, '{}', '2026-01-01 00:00:00', '2026-01-01 00:00:00'
WHERE NOT EXISTS (SELECT 1 FROM member_card WHERE member_card_name = '10次卡');

-- 储值卡类型（500元）
INSERT INTO member_card (member_card_name, member_card_type, member_card_price, member_card_validity_days, member_card_total_times, member_card_amount, member_card_status, extra_config, created_at, updated_at) 
SELECT '储值卡500', 'STORED_VALUE_CARD', 500.00, NULL, NULL, 500.00, 1, '{}', '2026-01-01 00:00:00', '2026-01-01 00:00:00'
WHERE NOT EXISTS (SELECT 1 FROM member_card WHERE member_card_name = '储值卡500');

-- ============================================
-- 3. 为测试用户分配会员卡
-- ============================================

-- 张三：时长卡（有效期内）
INSERT INTO member_card_record (member_id, member_card_id, status, remaining_times, remaining_amount, expire_time, purchase_time, version, card_composition, created_at, updated_at) VALUES
    ((SELECT id FROM member_user WHERE member_no = 'MEM_TEST_ZHANG'), 
     (SELECT member_card_id FROM member_card WHERE member_card_name = '30天时长卡'), 
     'ACTIVE', 0, 0.00, '2026-07-02 23:59:59', '2026-06-02 10:00:00', 0, '{}', '2026-06-02 10:00:00', '2026-06-02 10:00:00');

-- 李四：次数卡（剩余5次）
INSERT INTO member_card_record (member_id, member_card_id, status, remaining_times, remaining_amount, expire_time, purchase_time, version, card_composition, created_at, updated_at) VALUES
    ((SELECT id FROM member_user WHERE member_no = 'MEM_TEST_LI'), 
     (SELECT member_card_id FROM member_card WHERE member_card_name = '10次卡'), 
     'ACTIVE', 5, 0.00, '2026-12-31 23:59:59', '2026-05-01 10:00:00', 0, '{}', '2026-05-01 10:00:00', '2026-06-02 10:00:00');

-- 王五：储值卡（余额200元）
INSERT INTO member_card_record (member_id, member_card_id, status, remaining_times, remaining_amount, expire_time, purchase_time, version, card_composition, created_at, updated_at) VALUES
    ((SELECT id FROM member_user WHERE member_no = 'MEM_TEST_WANG'), 
     (SELECT member_card_id FROM member_card WHERE member_card_name = '储值卡500'), 
     'ACTIVE', 0, 200.00, '2027-06-02 23:59:59', '2026-05-01 10:00:00', 0, '{}', '2026-05-01 10:00:00', '2026-06-02 10:00:00');

-- ============================================
-- 4. 创建用于取消预约测试的团课
-- ============================================

-- 团课X: 时长卡可用课程（使用次数卡支付，时长卡会员可预约）
INSERT INTO group_course (course_name, coach_id, course_type, start_time, end_time, max_members, current_members, status, location, description, point_card_amount, stored_value_amount, create_by, created_at, updated_at) VALUES
    ('晚间瑜伽_取消测试', 101, 1, '2026-06-15 19:00:00', '2026-06-15 20:00:00', 20, 0, '0', '瑜伽教室', '适合所有级别的瑜伽课程，用于测试取消预约功能', 1, 30.00, 'admin', '2026-06-02 10:00:00', '2026-06-02 10:00:00');

-- ============================================
-- 5. 创建已预约的记录（状态为0-已预约）
-- ============================================

-- 张三使用时长卡预约团课（状态：已预约）
INSERT INTO group_course_booking (course_id, member_id, member_card_id, booking_time, status, course_name, course_start_time, course_end_time, location, created_at, updated_at) VALUES
    ((SELECT id FROM group_course WHERE course_name = '晚间瑜伽_取消测试'),
     (SELECT id FROM member_user WHERE member_no = 'MEM_TEST_ZHANG'),
     (SELECT id FROM member_card_record WHERE member_id = (SELECT id FROM member_user WHERE member_no = 'MEM_TEST_ZHANG')),
     '2026-06-02 11:00:00', '0', '晚间瑜伽_取消测试', '2026-06-15 19:00:00', '2026-06-15 20:00:00', '瑜伽教室',
     '2026-06-02 11:00:00', '2026-06-02 11:00:00');

-- 李四使用次数卡预约团课（状态：已预约）
INSERT INTO group_course_booking (course_id, member_id, member_card_id, booking_time, status, course_name, course_start_time, course_end_time, location, created_at, updated_at) VALUES
    ((SELECT id FROM group_course WHERE course_name = '晚间瑜伽_取消测试'),
     (SELECT id FROM member_user WHERE member_no = 'MEM_TEST_LI'),
     (SELECT id FROM member_card_record WHERE member_id = (SELECT id FROM member_user WHERE member_no = 'MEM_TEST_LI')),
     '2026-06-02 11:30:00', '0', '晚间瑜伽_取消测试', '2026-06-15 19:00:00', '2026-06-15 20:00:00', '瑜伽教室',
     '2026-06-02 11:30:00', '2026-06-02 11:30:00');

-- 王五使用储值卡预约团课（状态：已预约）
INSERT INTO group_course_booking (course_id, member_id, member_card_id, booking_time, status, course_name, course_start_time, course_end_time, location, created_at, updated_at) VALUES
    ((SELECT id FROM group_course WHERE course_name = '晚间瑜伽_取消测试'),
     (SELECT id FROM member_user WHERE member_no = 'MEM_TEST_WANG'),
     (SELECT id FROM member_card_record WHERE member_id = (SELECT id FROM member_user WHERE member_no = 'MEM_TEST_WANG')),
     '2026-06-02 12:00:00', '0', '晚间瑜伽_取消测试', '2026-06-15 19:00:00', '2026-06-15 20:00:00', '瑜伽教室',
     '2026-06-02 12:00:00', '2026-06-02 12:00:00');

-- ============================================
-- 6. 更新团课当前预约人数
-- ============================================

UPDATE group_course 
SET current_members = current_members + 3 
WHERE course_name = '晚间瑜伽_取消测试';

-- ============================================
-- 测试数据说明
-- ============================================
-- 用户信息：
-- 张三 (MEM_TEST_ZHANG): 13800002001 - 持有有效期内的时长卡
-- 李四 (MEM_TEST_LI): 13800002002 - 持有次数卡（剩余5次）
-- 王五 (MEM_TEST_WANG): 13800002003 - 持有储值卡（余额200元）

-- 测试场景：
-- 1. 张三使用时长卡预约了团课 -> 取消预约 -> 应恢复时长卡状态，释放课程名额
-- 2. 李四使用次数卡预约了团课 -> 取消预约 -> 应返还扣除的次数，释放课程名额
-- 3. 王五使用储值卡预约了团课 -> 取消预约 -> 应返还扣除的金额，释放课程名额

-- 预约记录状态说明：
-- status = '0' 表示已预约
-- status = '1' 表示已取消（取消后应更新为此状态）
