-- 初三年级班级数据设置脚本
-- 专门为初三1-6班创建完整的数据结构

-- 步骤1: 创建或更新基础数据结构

-- 创建学校（如果不存在）
INSERT INTO public.ak_schools (id, name, type) 
VALUES 
    ('school-middle-001', '某某中学', '中学')
ON CONFLICT (id) DO UPDATE SET
    name = EXCLUDED.name,
    type = EXCLUDED.type;

-- 创建初三年级（如果不存在）
INSERT INTO public.ak_grades (id, school_id, name) 
VALUES 
    ('grade-junior3-001', 'school-middle-001', '初三年级')
ON CONFLICT (id) DO UPDATE SET
    school_id = EXCLUDED.school_id,
    name = EXCLUDED.name;

-- 创建初三1-6班（如果不存在，否则更新）
INSERT INTO public.ak_classes (id, grade_id, name) 
VALUES 
    ('class-junior3-01', 'grade-junior3-001', '初三(1)班'),
    ('class-junior3-02', 'grade-junior3-001', '初三(2)班'),
    ('class-junior3-03', 'grade-junior3-001', '初三(3)班'),
    ('class-junior3-04', 'grade-junior3-001', '初三(4)班'),
    ('class-junior3-05', 'grade-junior3-001', '初三(5)班'),
    ('class-junior3-06', 'grade-junior3-001', '初三(6)班')
ON CONFLICT (id) DO UPDATE SET
    grade_id = EXCLUDED.grade_id,
    name = EXCLUDED.name;

-- 步骤2: 如果您已有班级数据，我们可以将现有的class_id映射到标准结构
-- 这部分需要根据您的实际数据调整

-- 查看现有班级数据，以便进行映射
SELECT 
    id as current_class_id,
    name as current_class_name
FROM public.ak_classes 
WHERE name LIKE '%初三%' OR name LIKE '%三年级%' OR name LIKE '%九年级%'
ORDER BY name;

-- 步骤3: 更新现有学生的学校和年级信息
-- 假设您的学生已经有class_id，我们需要补充school_id和grade_id

UPDATE public.ak_users 
SET 
    school_id = 'school-middle-001',
    grade_id = 'grade-junior3-001'
WHERE role = 'student' 
    AND class_id IS NOT NULL
    AND (school_id IS NULL OR grade_id IS NULL);

-- 步骤4: 创建测试教师并分配班级管理权限

-- 创建初三年级的班主任教师
INSERT INTO public.ak_users (
    id, 
    username, 
    email, 
    password_hash, 
    role,
    school_id,
    grade_id
) 
VALUES 
    ('teacher-jr3-001', '王老师', 'wang.teacher@school.edu', '$2b$10$dummy.hash.for.testing', 'teacher', 'school-middle-001', 'grade-junior3-001'),
    ('teacher-jr3-002', '陈老师', 'chen.teacher@school.edu', '$2b$10$dummy.hash.for.testing', 'teacher', 'school-middle-001', 'grade-junior3-001'),
    ('teacher-jr3-003', '刘老师', 'liu.teacher@school.edu', '$2b$10$dummy.hash.for.testing', 'teacher', 'school-middle-001', 'grade-junior3-001')
ON CONFLICT (id) DO UPDATE SET
    username = EXCLUDED.username,
    email = EXCLUDED.email,
    school_id = EXCLUDED.school_id,
    grade_id = EXCLUDED.grade_id;

-- 步骤5: 创建教师-班级关系
-- 每个老师负责2个班级

-- 王老师负责初三(1)班和初三(2)班
INSERT INTO public.ak_teacher_roles (id, user_id, class_id)
SELECT 
    gen_random_uuid(),
    'teacher-jr3-001',
    c.id
FROM public.ak_classes c
WHERE c.name IN ('初三(1)班', '初三(2)班')
ON CONFLICT (user_id, class_id) DO NOTHING;

-- 陈老师负责初三(3)班和初三(4)班  
INSERT INTO public.ak_teacher_roles (id, user_id, class_id)
SELECT 
    gen_random_uuid(),
    'teacher-jr3-002',
    c.id
FROM public.ak_classes c
WHERE c.name IN ('初三(3)班', '初三(4)班')
ON CONFLICT (user_id, class_id) DO NOTHING;

-- 刘老师负责初三(5)班和初三(6)班
INSERT INTO public.ak_teacher_roles (id, user_id, class_id)
SELECT 
    gen_random_uuid(),
    'teacher-jr3-003',
    c.id
FROM public.ak_classes c
WHERE c.name IN ('初三(5)班', '初三(6)班')
ON CONFLICT (user_id, class_id) DO NOTHING;

-- 步骤6: 验证和查看结果

-- 查看初三年级的完整结构
SELECT 
    s.name as school_name,
    g.name as grade_name,
    c.name as class_name,
    c.id as class_id,
    COUNT(u.id) as student_count
FROM public.ak_classes c
JOIN public.ak_grades g ON c.grade_id = g.id
JOIN public.ak_schools s ON g.school_id = s.id
LEFT JOIN public.ak_users u ON u.class_id = c.id AND u.role = 'student'
WHERE g.name LIKE '%初三%'
GROUP BY s.name, g.name, c.name, c.id
ORDER BY c.name;

-- 查看教师负责的班级情况
SELECT 
    u.username as teacher_name,
    string_agg(c.name, ', ') as managed_classes,
    COUNT(DISTINCT c.id) as class_count,
    SUM(CASE WHEN students.student_count IS NOT NULL THEN students.student_count ELSE 0 END) as total_students
FROM public.ak_teacher_roles tr
JOIN public.ak_users u ON tr.user_id = u.id
JOIN public.ak_classes c ON tr.class_id = c.id
LEFT JOIN (
    SELECT 
        class_id, 
        COUNT(*) as student_count 
    FROM public.ak_users 
    WHERE role = 'student' 
    GROUP BY class_id
) students ON students.class_id = c.id
WHERE u.role = 'teacher'
GROUP BY u.id, u.username
ORDER BY u.username;

-- 检查数据完整性
SELECT 
    'Students without class' as check_type,
    COUNT(*) as count
FROM public.ak_users 
WHERE role = 'student' AND class_id IS NULL

UNION ALL

SELECT 
    'Students without school' as check_type,
    COUNT(*) as count
FROM public.ak_users 
WHERE role = 'student' AND school_id IS NULL

UNION ALL

SELECT 
    'Students without grade' as check_type,
    COUNT(*) as count
FROM public.ak_users 
WHERE role = 'student' AND grade_id IS NULL;

-- 如果您需要重新分配学生到各个班级，可以使用以下脚本
-- （注释掉，仅在需要时使用）
/*
-- 将现有学生随机分配到初三1-6班
WITH class_list AS (
    SELECT id, name, ROW_NUMBER() OVER (ORDER BY name) as class_order
    FROM public.ak_classes c
    JOIN public.ak_grades g ON c.grade_id = g.id
    WHERE g.name LIKE '%初三%'
),
student_assignment AS (
    SELECT 
        u.id as user_id,
        cl.id as new_class_id
    FROM public.ak_users u
    CROSS JOIN class_list cl
    WHERE u.role = 'student'
        AND (u.class_id IS NULL OR u.class_id NOT IN (SELECT id FROM class_list))
        AND (hashtext(u.id::text) % 6 + 1) = cl.class_order
)
UPDATE public.ak_users 
SET 
    class_id = sa.new_class_id,
    school_id = 'school-middle-001',
    grade_id = 'grade-junior3-001'
FROM student_assignment sa
WHERE ak_users.id = sa.user_id;
*/
