#!/usr/bin/env python3
"""
修改数据库结构脚本
将is_interested字段改为score字段，实现10分评分制度
"""

import sqlite3
import os
from loguru import logger

def modify_database_structure():
    """修改数据库结构，将is_interested字段改为score字段"""
    # 获取当前脚本所在目录的数据库文件路径
    script_dir = os.path.dirname(os.path.abspath(__file__))
    db_path = os.path.join(script_dir, "tophub_data.db")
    
    # 检查数据库文件是否存在
    if not os.path.exists(db_path):
        logger.error(f"数据库文件不存在: {db_path}")
        return False
    
    try:
        # 连接数据库
        conn = sqlite3.connect(db_path)
        cursor = conn.cursor()
        
        # 检查is_interested字段是否存在
        cursor.execute("PRAGMA table_info(articles)")
        columns = cursor.fetchall()
        column_names = [column[1] for column in columns]
        
        if "is_interested" not in column_names:
            logger.info("is_interested字段不存在，无需修改")
            conn.close()
            return True
            
        # 检查score字段是否已存在
        if "score" in column_names:
            logger.info("score字段已存在，无需添加")
            conn.close()
            return True
        
        # 添加score字段，默认值为5
        logger.info("正在添加score字段...")
        cursor.execute("ALTER TABLE articles ADD COLUMN score INTEGER DEFAULT 5")
        
        # 将is_interested的值转换为score
        logger.info("正在转换is_interested数据到score字段...")
        
        # 获取所有记录
        cursor.execute("SELECT id, is_interested FROM articles")
        records = cursor.fetchall()
        
        # 转换数据
        for record in records:
            article_id, is_interested = record
            # 转换逻辑：is_interested=1转为score=7，is_interested=0转为score=5
            score = 7 if is_interested == 1 else 5
            cursor.execute("UPDATE articles SET score = ? WHERE id = ?", (score, article_id))
        
        # 提交更改
        conn.commit()
        logger.info("成功添加score字段并转换数据")
        
        # 验证字段是否添加成功
        cursor.execute("PRAGMA table_info(articles)")
        columns = cursor.fetchall()
        column_names = [column[1] for column in columns]
        
        if "score" in column_names:
            logger.info("验证成功：score字段已添加到articles表")
        else:
            logger.error("验证失败：score字段未成功添加")
            conn.close()
            return False
            
        # 检查数据转换结果
        cursor.execute("SELECT COUNT(*) FROM articles WHERE score = 7")
        count_7 = cursor.fetchone()[0]
        
        cursor.execute("SELECT COUNT(*) FROM articles WHERE score = 5")
        count_5 = cursor.fetchone()[0]
        
        logger.info(f"数据转换结果: score=7的记录数: {count_7}, score=5的记录数: {count_5}")
            
        conn.close()
        return True
        
    except sqlite3.Error as e:
        logger.error(f"数据库操作出错: {str(e)}")
        return False
    except Exception as e:
        logger.error(f"修改数据库结构时出错: {str(e)}")
        return False

if __name__ == "__main__":
    logger.add("db_modify_score.log", rotation="10 MB", level="INFO")
    if modify_database_structure():
        logger.info("数据库结构修改完成")
    else:
        logger.error("数据库结构修改失败")