#!/bin/bash

# =====================================================
# 校园思政新闻平台数据库重新初始化脚本
# 版本: 1.0.0
# 说明: 该脚本会删除现有数据库，重新创建并初始化所有数据
# 注意: 执行前请确保MySQL服务已启动，并且有足够的权限
# =====================================================

# 设置颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 数据库配置
DB_HOST="localhost"
DB_PORT="3306"
DB_USER="root"
DB_PASSWORD="123456"
DB_NAME="school_news"

# 脚本目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# 日志文件
LOG_FILE="$SCRIPT_DIR/reInit.log"

# 打印带颜色的消息
print_message() {
    local color=$1
    local message=$2
    echo -e "${color}${message}${NC}"
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" >> "$LOG_FILE"
}

# 检查MySQL连接
check_mysql_connection() {
    print_message $BLUE "检查MySQL连接..."
    
    if command -v mysql &> /dev/null; then
        if mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" -e "SELECT 1;" &> /dev/null; then
            print_message $GREEN "MySQL连接成功"
            return 0
        else
            print_message $RED "MySQL连接失败，请检查配置"
            return 1
        fi
    else
        print_message $RED "MySQL客户端未安装或不在PATH中"
        return 1
    fi
}

# 备份现有数据库（如果存在）
backup_database() {
    if mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" -e "USE $DB_NAME;" &> /dev/null; then
        print_message $YELLOW "发现现有数据库，正在备份..."
        local backup_file="$SCRIPT_DIR/backup_${DB_NAME}_$(date +%Y%m%d_%H%M%S).sql"
        mysqldump -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" > "$backup_file"
        if [ $? -eq 0 ]; then
            print_message $GREEN "数据库备份成功: $backup_file"
        else
            print_message $YELLOW "数据库备份失败，继续执行..."
        fi
    fi
}

# 删除现有数据库
drop_database() {
    print_message $YELLOW "删除现有数据库..."
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" -e "DROP DATABASE IF EXISTS $DB_NAME;"
    if [ $? -eq 0 ]; then
        print_message $GREEN "数据库删除成功"
    else
        print_message $RED "数据库删除失败"
        exit 1
    fi
}

# 执行初始化脚本
execute_init_script() {
    print_message $BLUE "开始执行数据库初始化..."
    
    # 切换到脚本目录
    cd "$SCRIPT_DIR"
    
    # 执行initAll.sql
    mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" < initAll.sql
    
    if [ $? -eq 0 ]; then
        print_message $GREEN "数据库初始化成功"
    else
        print_message $RED "数据库初始化失败"
        exit 1
    fi
}

# 验证初始化结果
verify_initialization() {
    print_message $BLUE "验证初始化结果..."
    
    # 检查数据库是否存在
    if mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" -e "USE $DB_NAME;" &> /dev/null; then
        print_message $GREEN "数据库创建成功"
    else
        print_message $RED "数据库创建失败"
        exit 1
    fi
    
    # 检查表数量
    local table_count=$(mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" -D"$DB_NAME" -e "SHOW TABLES;" | wc -l)
    print_message $GREEN "创建了 $table_count 张表"
    
    # 检查默认用户
    local user_count=$(mysql -h"$DB_HOST" -P"$DB_PORT" -u"$DB_USER" -p"$DB_PASSWORD" -D"$DB_NAME" -e "SELECT COUNT(*) FROM tb_sys_user WHERE username='admin';" | tail -n 1)
    if [ "$user_count" -gt 0 ]; then
        print_message $GREEN "默认用户创建成功"
    else
        print_message $YELLOW "默认用户创建失败"
    fi
}

# 显示初始化信息
show_initialization_info() {
    print_message $BLUE "====================================================="
    print_message $GREEN "数据库初始化完成！"
    print_message $BLUE "====================================================="
    print_message $YELLOW "数据库信息:"
    print_message $YELLOW "  数据库名: $DB_NAME"
    print_message $YELLOW "  主机: $DB_HOST"
    print_message $YELLOW "  端口: $DB_PORT"
    print_message $YELLOW "  用户: $DB_USER"
    print_message $BLUE "====================================================="
    print_message $YELLOW "默认账户信息:"
    print_message $YELLOW "  用户名: admin"
    print_message $YELLOW "  密码: admin123"
    print_message $YELLOW "  角色: 管理员"
    print_message $BLUE "====================================================="
    print_message $YELLOW "系统功能:"
    print_message $YELLOW "  - 用户管理"
    print_message $YELLOW "  - 权限管理"
    print_message $YELLOW "  - 资源管理"
    print_message $YELLOW "  - 学习管理"
    print_message $YELLOW "  - AI智能体"
    print_message $YELLOW "  - 系统配置"
    print_message $YELLOW "  - 文件管理"
    print_message $BLUE "====================================================="
}

# 主函数
main() {
    print_message $BLUE "====================================================="
    print_message $BLUE "校园思政新闻平台数据库重新初始化脚本"
    print_message $BLUE "版本: 1.0.0"
    print_message $BLUE "====================================================="
    
    # 检查MySQL连接
    if ! check_mysql_connection; then
        exit 1
    fi
    
    # 备份现有数据库
    # backup_database
    
    # 删除现有数据库
    # drop_database
    
    # 执行初始化脚本
    execute_init_script
    
    # 验证初始化结果
    verify_initialization
    
    # 显示初始化信息
    show_initialization_info
    
    print_message $GREEN "初始化完成！"
}

# 脚本入口
if [ "${BASH_SOURCE[0]}" == "${0}" ]; then
    main "$@"
fi
