-- 初始化用户数据 - 解决登录问题
-- 执行方式: mysql -h 8.148.151.56 -u root -preading_platform_pwd reading_platform < init-users.sql

USE reading_platform;

-- ============================================
-- 1. 检查并创建必要的表
-- ============================================

-- 创建 admin_users 表（如果不存在）
CREATE TABLE IF NOT EXISTS admin_users (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(50) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    name VARCHAR(50) NOT NULL,
    email VARCHAR(100),
    phone VARCHAR(20),
    avatar_url VARCHAR(500),
    status VARCHAR(20) DEFAULT 'active',
    last_login_at DATETIME,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted TINYINT DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 创建 teachers 表（如果不存在）
CREATE TABLE IF NOT EXISTS teachers (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    tenant_id BIGINT NOT NULL,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(255) NOT NULL,
    name VARCHAR(50) NOT NULL,
    phone VARCHAR(20),
    email VARCHAR(100),
    avatar_url VARCHAR(500),
    gender VARCHAR(10),
    bio TEXT,
    status VARCHAR(20) DEFAULT 'active',
    last_login_at DATETIME,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted TINYINT DEFAULT 0,
    UNIQUE KEY uk_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 创建 parents 表（如果不存在）
CREATE TABLE IF NOT EXISTS parents (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    tenant_id BIGINT NOT NULL,
    username VARCHAR(50) NOT NULL,
    password VARCHAR(255) NOT NULL,
    name VARCHAR(50) NOT NULL,
    phone VARCHAR(20),
    email VARCHAR(100),
    avatar_url VARCHAR(500),
    gender VARCHAR(10),
    status VARCHAR(20) DEFAULT 'active',
    last_login_at DATETIME,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted TINYINT DEFAULT 0,
    UNIQUE KEY uk_username (username)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 创建 tenants 表（如果不存在）
CREATE TABLE IF NOT EXISTS tenants (
    id BIGINT PRIMARY KEY AUTO_INCREMENT,
    name VARCHAR(100) NOT NULL,
    code VARCHAR(50) NOT NULL UNIQUE,
    username VARCHAR(50) UNIQUE,
    password VARCHAR(255),
    contact_name VARCHAR(50),
    contact_phone VARCHAR(20),
    contact_email VARCHAR(100),
    address VARCHAR(255),
    logo_url VARCHAR(500),
    status VARCHAR(20) DEFAULT 'active',
    expire_at DATETIME,
    max_students INT DEFAULT 0,
    max_teachers INT DEFAULT 0,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    deleted TINYINT DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 添加 username/password 字段到 tenants 表（如果不存在）
ALTER TABLE tenants ADD COLUMN IF NOT EXISTS username VARCHAR(50) UNIQUE AFTER code;
ALTER TABLE tenants ADD COLUMN IF NOT EXISTS password VARCHAR(255) AFTER username;

-- ============================================
-- 2. 插入测试数据
-- 密码都是 123456
-- BCrypt hash: $2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi
-- ============================================

-- 清理旧数据
DELETE FROM admin_users WHERE username IN ('admin');
DELETE FROM teachers WHERE username IN ('teacher1', 'teacher2');
DELETE FROM parents WHERE username IN ('parent1', 'parent2');
DELETE FROM tenants WHERE id IN (1);

-- 插入超管用户
INSERT INTO admin_users (id, username, password, name, email, phone, status) VALUES
(1, 'admin', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', '平台管理员', 'admin@example.com', '13800138000', 'active');

-- 插入租户（学校）
INSERT INTO tenants (id, name, code, username, password, contact_name, contact_phone, status) VALUES
(1, '测试幼儿园', 'SCHOOL001', 'school1', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', '张校长', '13800138001', 'active');

-- 插入教师用户
INSERT INTO teachers (id, tenant_id, username, password, name, phone, email, status) VALUES
(1, 1, 'teacher1', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', '测试教师1', '13800138002', 'teacher1@example.com', 'active'),
(2, 1, 'teacher2', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', '测试教师2', '13800138003', 'teacher2@example.com', 'active');

-- 插入家长用户
INSERT INTO parents (id, tenant_id, username, password, name, phone, email, status) VALUES
(1, 1, 'parent1', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', '测试家长1', '13800138004', 'parent1@example.com', 'active'),
(2, 1, 'parent2', '$2a$10$N.zmdr9k7uOCQb376NoUnuTJ8iAt6Z5EHsM8lE9lBOsl7iKTVKIUi', '测试家长2', '13800138005', 'parent2@example.com', 'active');

-- ============================================
-- 3. 验证数据
-- ============================================

SELECT '=== Admin Users ===' AS '';
SELECT id, username, name, status FROM admin_users;

SELECT '=== Tenants ===' AS '';
SELECT id, username, name, code, status FROM tenants;

SELECT '=== Teachers ===' AS '';
SELECT id, username, name, tenant_id, status FROM teachers;

SELECT '=== Parents ===' AS '';
SELECT id, username, name, tenant_id, status FROM parents;
