-- ============================================
-- E2E 测试种子数据
-- 描述: Gym 业务模块测试用初始化数据
-- 注意: 使用 INSERT ... ON CONFLICT DO NOTHING 确保幂等性
--       ID 从 100 开始避免与已有数据冲突
-- ============================================

-- ============================================
-- 1. 测试教练（sys_user + user_role）
--    密码均为 Test@123
--    bcrypt: $2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C
-- ============================================

INSERT INTO sys_user (id, username, password, email, phone, nickname, status, create_by, update_by, created_at, updated_at)
VALUES
(100, 'coach_e2e_01', '$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C', 'coach_e2e_01@test.com', '13900139100', 'E2E测试教练-张', 1, 'system', 'system', NOW(), NOW()),
(101, 'coach_e2e_02', '$2a$12$nZ1EMUpZQljbnEdIKzH72eHlDJKUmHmHppnTTVth/SlHs5VpSAr8C', 'coach_e2e_02@test.com', '13900139101', 'E2E测试教练-李', 1, 'system', 'system', NOW(), NOW())
ON CONFLICT (username) DO UPDATE SET
    password = EXCLUDED.password,
    status    = EXCLUDED.status;

-- 分配教练角色（role_id=5）
INSERT INTO user_role (user_id, role_id, created_by, created_at)
VALUES
(100, 5, 'system', NOW()),
(101, 5, 'system', NOW())
ON CONFLICT (user_id, role_id) DO NOTHING;

-- ============================================
-- 2. 团课类型（group_course_type）
-- ============================================

INSERT INTO group_course_type (id, type_name, base_difficulty, description, category, create_by, update_by, created_at, updated_at)
VALUES
(100, '瑜伽',      3, '通过呼吸调控与体式练习，提升身体柔韧性与心灵平静',             '柔韧与平衡类', 'system', 'system', NOW(), NOW()),
(101, '动感单车',  5, '伴随动感音乐进行室内骑行，高效燃脂释放压力',                   '基础有氧与热身', 'system', 'system', NOW(), NOW()),
(102, '搏击操',    6, '融合拳击、踢腿动作的有氧运动，宣泄压力同时高效塑形',            '高强度与爆发力', 'system', 'system', NOW(), NOW()),
(103, '普拉提',    4, '注重核心肌群控制与脊柱对齐，有效改善体态预防运动损伤',          '柔韧与平衡类', 'system', 'system', NOW(), NOW())
ON CONFLICT (id) DO UPDATE SET
    type_name        = EXCLUDED.type_name,
    base_difficulty  = EXCLUDED.base_difficulty,
    description      = EXCLUDED.description,
    category         = EXCLUDED.category;

-- ============================================
-- 3. 课程标签（course_label）
-- ============================================

INSERT INTO course_label (id, label_name, color, description, create_by, update_by, created_at, updated_at)
VALUES
(100, '燃脂',       '#ff4d4f', '高效燃烧卡路里，助力减脂',          'system', 'system', NOW(), NOW()),
(101, '塑形',       '#722ed1', '注重体型塑造与线条雕刻',            'system', 'system', NOW(), NOW()),
(102, '减压',       '#52c41a', '放松身心，缓解压力',                'system', 'system', NOW(), NOW()),
(103, '核心训练',   '#1890ff', '强化核心肌群，改善身体稳定性',      'system', 'system', NOW(), NOW()),
(104, '初学者友好', '#faad14', '适合健身初学者，动作简单易学',      'system', 'system', NOW(), NOW()),
(105, '高阶挑战',   '#f5222d', '适合高级学员，强度和技巧要求较高',  'system', 'system', NOW(), NOW())
ON CONFLICT (id) DO UPDATE SET
    label_name  = EXCLUDED.label_name,
    color       = EXCLUDED.color,
    description = EXCLUDED.description;

-- ============================================
-- 4. 类型-标签关联（course_type_label）
-- ============================================

-- 瑜伽: 减压 + 初学者友好 + 核心训练
INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 100, 102, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 100 AND label_id = 102);

INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 100, 104, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 100 AND label_id = 104);

INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 100, 103, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 100 AND label_id = 103);

-- 动感单车: 燃脂 + 高阶挑战
INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 101, 100, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 101 AND label_id = 100);

INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 101, 105, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 101 AND label_id = 105);

-- 搏击操: 燃脂 + 塑形 + 高阶挑战
INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 102, 100, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 102 AND label_id = 100);

INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 102, 101, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 102 AND label_id = 101);

INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 102, 105, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 102 AND label_id = 105);

-- 普拉提: 核心训练 + 塑形 + 初学者友好
INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 103, 103, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 103 AND label_id = 103);

INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 103, 101, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 103 AND label_id = 101);

INSERT INTO course_type_label (type_id, label_id, create_by, update_by, created_at, updated_at)
SELECT 103, 104, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM course_type_label WHERE type_id = 103 AND label_id = 104);

-- ============================================
-- 5. 团课课程（group_course）
--    4节课，未来日期，不同时间段和地点
-- ============================================

-- 晨间瑜伽 | A教室 | 明天 08:00-09:00 | 教练100
INSERT INTO group_course (id, course_name, coach_id, course_type, start_time, end_time, max_members, current_members, status, location, cover_image, description, stored_value_amount, create_by, update_by, created_at, updated_at)
SELECT 100, '晨间瑜伽', 100, 100, '2026-07-27 08:00:00'::TIMESTAMP, '2026-07-27 09:00:00'::TIMESTAMP, 20, 5, '0', 'A教室', '/static/course/e2e_yoga.jpg', 'E2E测试-晨间瑜伽课程，温和唤醒身体', 88.00, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM group_course WHERE id = 100);

-- 动感燃脂 | 单车房 | 后天 19:00-20:00 | 教练101
INSERT INTO group_course (id, course_name, coach_id, course_type, start_time, end_time, max_members, current_members, status, location, cover_image, description, stored_value_amount, create_by, update_by, created_at, updated_at)
SELECT 101, '动感燃脂', 101, 101, '2026-07-28 19:00:00'::TIMESTAMP, '2026-07-28 20:00:00'::TIMESTAMP, 25, 10, '0', '单车房', '/static/course/e2e_spin.jpg', 'E2E测试-动感单车燃脂课程', 68.00, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM group_course WHERE id = 101);

-- 有氧搏击 | B教室 | 大后天 10:00-11:00 | 教练100
INSERT INTO group_course (id, course_name, coach_id, course_type, start_time, end_time, max_members, current_members, status, location, cover_image, description, stored_value_amount, create_by, update_by, created_at, updated_at)
SELECT 102, '有氧搏击', 100, 102, '2026-07-29 10:00:00'::TIMESTAMP, '2026-07-29 11:00:00'::TIMESTAMP, 20, 8, '0', 'B教室', '/static/course/e2e_boxing.jpg', 'E2E测试-有氧搏击操课程', 68.00, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM group_course WHERE id = 102);

-- 普拉提入门 | 瑜伽房 | 4天后 14:00-15:00 | 教练101
INSERT INTO group_course (id, course_name, coach_id, course_type, start_time, end_time, max_members, current_members, status, location, cover_image, description, stored_value_amount, create_by, update_by, created_at, updated_at)
SELECT 103, '普拉提入门', 101, 103, '2026-07-30 14:00:00'::TIMESTAMP, '2026-07-30 15:00:00'::TIMESTAMP, 15, 3, '0', '瑜伽房', '/static/course/e2e_pilates.jpg', 'E2E测试-普拉提入门课程，适合初学者', 108.00, 'system', 'system', NOW(), NOW()
WHERE NOT EXISTS (SELECT 1 FROM group_course WHERE id = 103);

-- ============================================
-- 6. 测试会员（member_user）
--    包含 miniapp_openid 用于微信登录模拟
-- ============================================

INSERT INTO member_user (id, member_no, nickname, phone, gender, miniapp_open_id, created_at, updated_at)
VALUES
(100, 'MEM-E2E-001', '测试会员一', '13900139200', 1, 'o_test_member_e2e_001', NOW(), NOW()),
(101, 'MEM-E2E-002', '测试会员二', '13900139201', 2, 'o_test_member_e2e_002', NOW(), NOW())
ON CONFLICT (member_no) DO UPDATE SET
    nickname        = EXCLUDED.nickname,
    phone           = EXCLUDED.phone,
    gender          = EXCLUDED.gender,
    miniapp_open_id = EXCLUDED.miniapp_open_id;

-- ============================================
-- 7. 签到记录（sign_in_record）
--    为测试会员创建几条签到历史
-- ============================================

INSERT INTO sign_in_record (id, member_id, sign_in_time, sign_in_type, sign_in_status, source, created_at, updated_at)
VALUES
(100, 100, '2026-07-26 08:30:00'::TIMESTAMP, 'QR_CODE', 'SUCCESS', 'MINI_PROGRAM', NOW(), NOW()),
(101, 101, '2026-07-26 09:00:00'::TIMESTAMP, 'QR_CODE', 'SUCCESS', 'MINI_PROGRAM', NOW(), NOW()),
(102, 100, '2026-07-26 18:00:00'::TIMESTAMP, 'MANUAL',  'SUCCESS', 'PC_BACKEND',   NOW(), NOW())
ON CONFLICT (id) DO NOTHING;

-- ============================================
-- 8. 重置序列值，避免后续插入冲突
-- ============================================
SELECT setval('sys_user_id_seq',        GREATEST(COALESCE((SELECT MAX(id) FROM sys_user), 1), 101));
SELECT setval('group_course_type_id_seq', GREATEST(COALESCE((SELECT MAX(id) FROM group_course_type), 1), 103));
SELECT setval('course_label_id_seq',    GREATEST(COALESCE((SELECT MAX(id) FROM course_label), 1), 105));
SELECT setval('group_course_id_seq',    GREATEST(COALESCE((SELECT MAX(id) FROM group_course), 1), 103));
SELECT setval('member_user_id_seq',     GREATEST(COALESCE((SELECT MAX(id) FROM member_user), 1), 101));
SELECT setval('sign_in_record_id_seq',  GREATEST(COALESCE((SELECT MAX(id) FROM sign_in_record), 1), 102));
