-- ============================================
-- 为所有学员用户注入陪练会话数据
-- ============================================

USE `kaopeilian`;

-- 先创建陪练场景（如果不存在）
INSERT IGNORE INTO practice_scenes (id, name, description, type, difficulty, status, background, ai_role, objectives, keywords, duration, usage_count, rating, created_by, is_deleted)
VALUES
(1, '电话销售陪练', '模拟电话销售场景，提升电话沟通技巧', 'phone', 'intermediate', 'active', '客户对轻医美项目感兴趣，需要通过电话进行专业介绍', 'AI扮演潜在客户', '["掌握电话开场技巧", "专业介绍项目", "处理客户疑问"]', '["电话销售", "沟通技巧", "项目介绍"]', 15, 0, 4.5, 1, 0),
(2, '面对面咨询陪练', '模拟面对面咨询场景，提升面诊沟通能力', 'face', 'intermediate', 'active', '客户到店咨询轻医美项目，需要专业面诊和方案推荐', 'AI扮演到店客户', '["建立客户信任", "专业面诊", "方案推荐"]', '["面对面", "咨询", "方案设计"]', 20, 0, 4.7, 1, 0),
(3, '客户投诉处理陪练', '模拟客户投诉场景，提升问题处理能力', 'complaint', 'senior', 'active', '客户对服务或效果不满意，需要妥善处理投诉', 'AI扮演投诉客户', '["倾听客户诉求", "安抚客户情绪", "提供解决方案"]', '["投诉处理", "情绪管理", "客户关系"]', 15, 0, 4.3, 1, 0),
(4, '售后服务陪练', '模拟售后服务场景，提升客户满意度', 'after-sales', 'junior', 'active', '项目完成后，进行售后跟进和关怀', 'AI扮演已消费客户', '["售后关怀", "效果跟进", "二次营销"]', '["售后服务", "客户维护", "复购"]', 10, 0, 4.6, 1, 0),
(5, '产品介绍陪练', '模拟产品介绍场景，提升产品讲解能力', 'product-intro', 'beginner', 'active', '向客户详细介绍轻医美项目和产品', 'AI扮演咨询客户', '["产品特点讲解", "适用人群分析", "价值传递"]', '["产品介绍", "专业知识", "销售技巧"]', 15, 0, 4.8, 1, 0);

-- 为每个学员用户创建陪练记录
-- 使用存储过程批量生成

DELIMITER //

DROP PROCEDURE IF EXISTS generate_practice_sessions//

CREATE PROCEDURE generate_practice_sessions(IN target_user_id INT, IN session_count INT)
BEGIN
    DECLARE i INT DEFAULT 0;
    DECLARE session_date DATETIME;
    DECLARE duration INT;
    DECLARE scene INT;
    DECLARE scene_name_val VARCHAR(200);
    DECLARE scene_type_val VARCHAR(50);
    DECLARE score INT;
    DECLARE turns INT;
    
    WHILE i < session_count DO
        -- 在过去60天内随机生成日期
        SET session_date = DATE_SUB(NOW(), INTERVAL FLOOR(RAND() * 60) DAY) + INTERVAL FLOOR(RAND() * 43200) SECOND;
        
        -- 随机时长：10-30分钟
        SET duration = 600 + FLOOR(RAND() * 1200);
        
        -- 随机选择场景
        SET scene = 1 + FLOOR(RAND() * 5);
        
        -- 根据场景设置名称和类型
        CASE scene
            WHEN 1 THEN 
                SET scene_name_val = '电话销售陪练';
                SET scene_type_val = 'phone';
            WHEN 2 THEN 
                SET scene_name_val = '面对面咨询陪练';
                SET scene_type_val = 'face';
            WHEN 3 THEN 
                SET scene_name_val = '客户投诉处理陪练';
                SET scene_type_val = 'complaint';
            WHEN 4 THEN 
                SET scene_name_val = '售后服务陪练';
                SET scene_type_val = 'after-sales';
            WHEN 5 THEN 
                SET scene_name_val = '产品介绍陪练';
                SET scene_type_val = 'product-intro';
        END CASE;
        
        -- 随机对话轮数
        SET turns = 10 + FLOOR(RAND() * 20);
        
        -- 随机分数（60-95分）
        SET score = 60 + FLOOR(RAND() * 35);
        
        -- 插入陪练会话
        INSERT INTO practice_sessions (
            session_id, 
            user_id, 
            scene_id, 
            scene_name, 
            scene_type,
            start_time, 
            end_time, 
            duration_seconds, 
            turns,
            status, 
            is_deleted,
            created_at,
            updated_at
        ) VALUES (
            CONCAT('session_', target_user_id, '_', UNIX_TIMESTAMP(session_date)),
            target_user_id,
            scene,
            scene_name_val,
            scene_type_val,
            session_date,
            DATE_ADD(session_date, INTERVAL duration SECOND),
            duration,
            turns,
            'completed',
            0,
            session_date,
            session_date
        );
        
        -- 插入陪练报告
        INSERT INTO practice_reports (
            session_id,
            total_score,
            score_breakdown,
            ability_dimensions,
            created_at,
            updated_at
        ) VALUES (
            CONCAT('session_', target_user_id, '_', UNIX_TIMESTAMP(session_date)),
            score,
            JSON_OBJECT(
                'professionalism', score + FLOOR(RAND() * 10) - 5,
                'communication', score + FLOOR(RAND() * 10) - 5,
                'problem_solving', score + FLOOR(RAND() * 10) - 5
            ),
            JSON_OBJECT(
                'technical_skills', score + FLOOR(RAND() * 10) - 5,
                'service_attitude', score + FLOOR(RAND() * 10) - 5,
                'sales_ability', score + FLOOR(RAND() * 10) - 5
            ),
            session_date,
            session_date
        );
        
        SET i = i + 1;
    END WHILE;
END//

DELIMITER ;

-- 为所有学员角色用户生成陪练数据
-- 获取所有学员用户ID并为每个用户生成15-25条记录

-- user_id = 1 (superadmin) - 20条
CALL generate_practice_sessions(1, 20);

-- user_id = 5 (consultant_001) - 25条
CALL generate_practice_sessions(5, 25);

-- user_id = 7 (therapist_001) - 22条
CALL generate_practice_sessions(7, 22);

-- user_id = 8 (receptionist_001) - 18条
CALL generate_practice_sessions(8, 18);

-- 统计结果
SELECT '========================================' AS '';
SELECT '✅ 陪练数据生成完成！' AS '';
SELECT '========================================' AS '';

SELECT 
    u.id,
    u.username,
    u.role,
    COUNT(ps.id) as practice_count
FROM users u
LEFT JOIN practice_sessions ps ON u.id = ps.user_id AND ps.is_deleted = 0
WHERE u.is_deleted = 0
GROUP BY u.id, u.username, u.role
ORDER BY u.id;

-- 清理存储过程
DROP PROCEDURE IF EXISTS generate_practice_sessions;

