from tools.utils.mysql_helper import MySQLHelper
from datetime import datetime
from typing import Optional, Dict

class MysqlStore:
    def __init__(self):
        self._db_helper = MySQLHelper()

    # ------------------ 标准表基础操作 ------------------
    # ------------------ 标准表基础操作 ------------------
    def create_standard(self, code: str, name: str) -> int:
        """创建标准记录，返回插入ID"""
        sql = """INSERT INTO pdf_standard 
                   (standard_code, standard_name, created_at, updated_at)
                   VALUES (%s, %s, NOW(), NOW())"""  # 改用NOW()获取完整时间戳
        with self._db_helper as db:
            return db.execute(sql, (code, name))

    def get_standard(self, standard_id: int) -> Optional[Dict]:
        """根据ID获取标准"""
        sql = "SELECT * FROM pdf_standard WHERE id = %s"
        with self._db_helper as db:
            return db.fetch_one(sql, (standard_id,))
    def get_all_standards(self):
        sql = "SELECT * FROM pdf_standard ORDER BY created_at DESC"
        with self._db_helper as db:
            return db.execute_query(sql)

    def get_all_pdf_records(self):
        sql = "SELECT chapter_name, pdf_path, created_at FROM pdf_records ORDER BY created_at DESC"
        with self._db_helper as db:
            return db.execute_query(sql)
    # ------------------ PDF记录表核心操作 ------------------
    def add_pdf_record(
            self,
            standard_name: str,
            pdf_path: str,
            image_path: str,
            markdown_text: str,
            chapter: str = "",
            section: str = "",
            subsection: str = ""
    ) -> int:
        """添加PDF处理记录，返回插入ID"""
        sql = """INSERT INTO pdf_records 
                   (standard_name, chapter_name, section_name, subsection_name,
                    pdf_path, image_path, markdown_text, created_at, updated_at)
                   VALUES (%s, %s, %s, %s, %s, %s, %s, NOW(), NOW())"""  # 增加时间字段
        params = (standard_name, chapter, section, subsection,
                  pdf_path, image_path, markdown_text)
        with self._db_helper as db:
            return db.execute(sql, params)

    def update_markdown(self, record_id: int, new_text: str) -> bool:
        """更新Markdown内容"""
        sql = "UPDATE pdf_records SET markdown_text = %s, updated_at = NOW() WHERE id = %s"
        with self._db_helper as db:
            return db.execute(sql, (new_text, record_id))

    def get_standard_by_name(self, name: str) -> dict:
        """根据标准名称获取标准"""
        sql = "SELECT * FROM pdf_standard WHERE standard_name = %s"
        with self._db_helper as db:
            return db.fetch_one(sql, (name,))

    def update_pdf_record(self, markdown_text: str, image_paths: str, by_image_path: str):
        """更新已有的PDF记录"""
        with self._db_helper as db:
            sql = """UPDATE pdf_records 
                     SET markdown_text = %s, 
                         image_path = %s 
                     WHERE image_path LIKE %s"""
            db.execute(sql, (markdown_text, image_paths, by_image_path))
