#!/usr/bin/env python3
"""
数据库初始化脚本
运行此脚本来创建必要的数据库表和初始数据
"""

import os
import sys
from pathlib import Path

# 添加项目路径
sys.path.append(str(Path(__file__).parent))

from utils.database import DatabaseManager
import logging

# 配置日志
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

logger = logging.getLogger(__name__)

def main():
    """初始化数据库"""
    try:
        # 确保databases目录存在
        db_dir = Path(__file__).parent / "databases"
        db_dir.mkdir(exist_ok=True)
        
        # 初始化数据库
        db_path = str(db_dir / "discord_bot.db")
        logger.info(f"初始化数据库: {db_path}")
        
        db_manager = DatabaseManager(db_path)
        
        # 验证数据库是否正确创建
        tables = db_manager.execute_query("""
            SELECT name FROM sqlite_master 
            WHERE type='table' AND name NOT LIKE 'sqlite_%'
        """)
        
        logger.info("已创建的表:")
        for table in tables:
            logger.info(f"  - {table['name']}")
        
        # 检查术语字典是否有数据
        term_count = db_manager.execute_query("SELECT COUNT(*) as count FROM term_dic")[0]['count']
        logger.info(f"术语字典包含 {term_count} 条记录")
        
        logger.info("数据库初始化完成！")
        
    except Exception as e:
        logger.error(f"数据库初始化失败: {e}")
        sys.exit(1)

if __name__ == "__main__":
    main()