INSERT INTO upms.tb_sys_tenant (
    tenant_id, parent_tenant_id, tenant_name, tenant_type, adcode, tenant_path
) VALUES
    ('SCH-HQ', NULL, 'K12Study 总校', 'HEAD_SCHOOL', '330100', '/SCH-HQ/'),
    ('SCH-ZJ-HZ-01', 'SCH-HQ', '杭州分校', 'CITY_SCHOOL', '330100', '/SCH-HQ/SCH-ZJ-HZ-01/')
ON CONFLICT (tenant_id) DO NOTHING;

INSERT INTO upms.tb_sys_dept (
    dept_id, parent_dept_id, tenant_id, dept_name, dept_type, adcode, tenant_path, dept_path
) VALUES
    ('DEPT-HQ', NULL, 'SCH-HQ', '总校教学部', 'GRADE', '330100', '/SCH-HQ/', '/DEPT-HQ/'),
    ('DEPT-HQ-MATH', 'DEPT-HQ', 'SCH-HQ', '数学学科组', 'SUBJECT', '330100', '/SCH-HQ/', '/DEPT-HQ/DEPT-HQ-MATH/'),
    ('DEPT-HQ-ADMIN', 'DEPT-HQ', 'SCH-HQ', '总校运营组', 'ADMIN', '330100', '/SCH-HQ/', '/DEPT-HQ/DEPT-HQ-ADMIN/')
ON CONFLICT (dept_id) DO NOTHING;

INSERT INTO upms.tb_sys_user (
    user_id, username, display_name, password_hash, mobile_phone, mobile_bind_status, mobile_verified_at,
    adcode, tenant_id, tenant_path, dept_id, dept_path
) VALUES
    ('U10001', 'admin', 'K12Study 管理员', 'admin123', NULL, 'UNBOUND', NULL, '330100', 'SCH-HQ', '/SCH-HQ/', 'DEPT-HQ-ADMIN', '/DEPT-HQ/DEPT-HQ-ADMIN/'),
    ('U20001', 'student01', '张同学', 'stud123', '13800000001', 'BOUND', CURRENT_TIMESTAMP, '330100', 'SCH-HQ', '/SCH-HQ/', 'DEPT-HQ', '/DEPT-HQ/')
ON CONFLICT (user_id) DO NOTHING;

INSERT INTO upms.tb_sys_role (
    role_id, role_code, role_name, adcode, tenant_id, tenant_path, dept_id, dept_path
) VALUES
    ('ROLE-ORG-ADMIN', 'ORG_ADMIN', '机构管理员', '330100', 'SCH-HQ', '/SCH-HQ/', 'DEPT-HQ-ADMIN', '/DEPT-HQ/DEPT-HQ-ADMIN/'),
    ('ROLE-STUDENT', 'STUDENT', '学生', '330100', 'SCH-HQ', '/SCH-HQ/', 'DEPT-HQ', '/DEPT-HQ/')
ON CONFLICT (role_id) DO NOTHING;

INSERT INTO upms.tb_sys_user_role (
    user_id, role_id, tenant_id
) VALUES
    ('U10001', 'ROLE-ORG-ADMIN', 'SCH-HQ'),
    ('U20001', 'ROLE-STUDENT', 'SCH-HQ')
ON CONFLICT (user_id, role_id) DO NOTHING;


INSERT INTO upms.tb_sys_menu (
    route_id, parent_route_id, route_path, route_name, component_key, layout_type, title, icon, permission_code,
    hidden, adcode, tenant_id, tenant_path, dept_id, dept_path
) VALUES
    ('ROUTE-DASHBOARD', NULL, '/', 'dashboard', 'dashboard', 'SIDEBAR', '控制台', 'layout-dashboard', 'dashboard:view', FALSE,
        '330100', 'SCH-HQ', '/SCH-HQ/', 'DEPT-HQ-ADMIN', '/DEPT-HQ/DEPT-HQ-ADMIN/'),
    ('ROUTE-TENANT', NULL, '/tenant', 'tenant-management', 'tenant', 'SIDEBAR', '租户组织', 'building-2', 'tenant:view', FALSE,
        '330100', 'SCH-HQ', '/SCH-HQ/', 'DEPT-HQ-ADMIN', '/DEPT-HQ/DEPT-HQ-ADMIN/')
ON CONFLICT (route_id) DO NOTHING;

INSERT INTO upms.tb_sys_role_menu (
    role_id, route_id, adcode, tenant_id, tenant_path, dept_id, dept_path
) VALUES
    ('ROLE-ORG-ADMIN', 'ROUTE-DASHBOARD', '330100', 'SCH-HQ', '/SCH-HQ/', 'DEPT-HQ-ADMIN', '/DEPT-HQ/DEPT-HQ-ADMIN/'),
    ('ROLE-ORG-ADMIN', 'ROUTE-TENANT', '330100', 'SCH-HQ', '/SCH-HQ/', 'DEPT-HQ-ADMIN', '/DEPT-HQ/DEPT-HQ-ADMIN/')
ON CONFLICT (role_id, route_id) DO NOTHING;

INSERT INTO upms.tb_school_class (
    class_id, tenant_id, dept_id, class_code, class_name, grade_code, status, adcode, tenant_path, dept_path, created_by
) VALUES
    ('CLS-2026-01', 'SCH-HQ', 'DEPT-HQ', 'G1-CLASS-1', '高一（1）班', 'G1', 'ACTIVE', '330100', '/SCH-HQ/', '/DEPT-HQ/', 'U10001')
ON CONFLICT (class_id) DO NOTHING;

INSERT INTO upms.tb_school_class_member (
    class_id, user_id, member_role, member_status, tenant_id
) VALUES
    ('CLS-2026-01', 'U20001', 'STUDENT', 'ACTIVE', 'SCH-HQ'),
    ('CLS-2026-01', 'U10001', 'HEAD_TEACHER', 'ACTIVE', 'SCH-HQ')
ON CONFLICT (class_id, user_id) DO NOTHING;

INSERT INTO upms.tb_school_class_course_rel (
    class_id, course_id, relation_status, tenant_id
) VALUES
    ('CLS-2026-01', 'COURSE-MATH-G1', 'ACTIVE', 'SCH-HQ')
ON CONFLICT (class_id, course_id) DO NOTHING;

INSERT INTO upms.tb_sys_message (
    message_id, message_type, biz_type, title, content, content_object_type, content_object_id, web_jump_url,
    send_channel, sender_user_id, adcode, tenant_id, tenant_path, message_status, send_at, ext_json
) VALUES
    (
        'MSG-20260415001',
        'TODO',
        'SUBJECTIVE_REVIEW',
        '有 1 份主观题待审阅',
        '高一数学《函数综合训练》有主观题待你审阅，请尽快处理。',
        'GRADING_TASK',
        'GD-TASK-20260415001',
        '/grading/review?gradingTaskId=GD-TASK-20260415001&messageId=MSG-20260415001',
        'INBOX',
        NULL,
        '330100',
        'SCH-HQ',
        '/SCH-HQ/',
        'ACTIVE',
        CURRENT_TIMESTAMP,
        '{"priority":"high","source":"grading"}'::JSONB
    ),
    (
        'MSG-20260415002',
        'INFO',
        'TENANT_NOTICE',
        '杭州分校租户信息有更新',
        '杭州分校基础资料已更新，可进入租户组织页查看详情。',
        'TENANT',
        'SCH-ZJ-HZ-01',
        '/tenant?tenantId=SCH-ZJ-HZ-01&messageId=MSG-20260415002',
        'INBOX',
        NULL,
        '330100',
        'SCH-HQ',
        '/SCH-HQ/',
        'ACTIVE',
        CURRENT_TIMESTAMP,
        '{"priority":"normal","source":"upms"}'::JSONB
    )
ON CONFLICT (message_id) DO NOTHING;

INSERT INTO upms.tb_sys_message_recipient (
    message_id, recipient_user_id, delivery_status, read_status, read_at, clicked_at, read_source,
    tenant_id, tenant_path, created_at, updated_at
) VALUES
    (
        'MSG-20260415001',
        'U10001',
        'DELIVERED',
        'UNREAD',
        NULL,
        NULL,
        NULL,
        'SCH-HQ',
        '/SCH-HQ/',
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP
    ),
    (
        'MSG-20260415002',
        'U10001',
        'DELIVERED',
        'READ',
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP,
        'WEB',
        'SCH-HQ',
        '/SCH-HQ/',
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP
    ),
    (
        'MSG-20260415001',
        'U20001',
        'DELIVERED',
        'UNREAD',
        NULL,
        NULL,
        NULL,
        'SCH-HQ',
        '/SCH-HQ/',
        CURRENT_TIMESTAMP,
        CURRENT_TIMESTAMP
    )
ON CONFLICT (message_id, recipient_user_id) DO NOTHING;
