# -*- coding: utf-8 -*-

import json
import sqlite3
import os
from datetime import datetime, timedelta
import email.utils


def create_database():
    conn = sqlite3.connect('./resources/db/seebug.db')
    cursor = conn.cursor()
    cursor.execute('''CREATE TABLE IF NOT EXISTS articles (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        title TEXT,
        link TEXT,
        category TEXT,
        description TEXT,
        pubDate DATETIME
    )''')
    conn.commit()
    conn.close()


def insert_data(data):
    conn = sqlite3.connect('./resources/db/seebug.db')
    cursor = conn.cursor()
    for entry in data:
        # 解析并格式化时间
        parsed_date = email.utils.parsedate_to_datetime(entry['pubDate'])
        formatted_date = parsed_date.strftime('%Y-%m-%d %H:%M:%S')
        
        cursor.execute('''
            INSERT INTO articles (title, link, category, description, pubDate)
            VALUES (?, ?, ?, ?, ?)
        ''', (entry['title'], entry['link'], entry['category'], entry['description'], formatted_date))
    conn.commit()
    conn.close()

def get_seebug_json():
    # 检查文件是否存在
    if not os.path.exists('./resources/JSON/seebug.json'):
        raise FileNotFoundError(f"seebug.json文件不存在，请检查程序是否运行正常！")
    
    # 打开并读取JSON文件
    with open('./resources/JSON/seebug.json', 'r', encoding='utf-8') as file:
        content = file.read()
        if not content:
            pass
        else:
            data = json.loads(content)
    
    # 假设data是一个包含多个JSON对象的列表
    if not isinstance(data, list):
        raise ValueError("JSON文件格式错误，请检查common.py是否异常！")
    
    # 提取所需字段并编号
    total_data = []
    for index, item in enumerate(data, start=1):
        entry = {
            "id": index,
            "title": item.get("title", ""),
            "link": item.get("link", ""),
            "description": item.get("description", ""),
            "pubDate": item.get("pubDate", ""),
            "category": item.get("category", "")
        }
        total_data.append(entry)
    
    return total_data

def select_articles(e_hour):
    conn = sqlite3.connect('./resources/db/seebug.db')
    cursor = conn.cursor()
    
    # 获取当前日期和时间
    now = datetime.now()
    start_time = now - timedelta(hours=e_hour)
    end_time = now
    
    # 查询指定时间段内的数据
    cursor.execute('''
        SELECT * FROM articles
        WHERE pubDate BETWEEN ? AND ?
    ''', (start_time.strftime('%Y-%m-%d %H:%M:%S'), end_time.strftime('%Y-%m-%d %H:%M:%S')))
    
    results = cursor.fetchall()
    conn.close()
    return results

def clear_table():
    conn = sqlite3.connect('./resources/db/seebug.db')
    cursor = conn.cursor()
    cursor.execute('DELETE FROM articles')
    conn.commit()
    conn.close()

def record(title, link, author, upload_time, description=None, source=None, category=None):
    db_path = './resources/db/web.db'
    conn = sqlite3.connect(db_path)
    cursor = conn.cursor()
    
    # 创建表（如果不存在）
    cursor.execute('''CREATE TABLE IF NOT EXISTS sec_news (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        article_title TEXT,
        link TEXT,
        author TEXT,
        description TEXT,
        source TEXT,
        upload_time DATETIME,
        category TEXT
    )''')
    
    # 插入数据
    cursor.execute('''
        INSERT INTO sec_news (article_title, link, author, description, source, upload_time, category)
        VALUES (?, ?, ?, ?, ?, ?, ?)
    ''', (title, link, author, description, source, upload_time, category))
    
    conn.commit()
    conn.close()

def get_filtered_articles(entries):
    result_long = ""
    result_short = ""
    short_results = []

    for entry in entries:
        # 构建长文本结果
        result_long += f"文章：[{entry[1]}]({entry[2]})\n类型：{entry[3]}\n"
        result_long += f"上传时间：{entry[5]}\n"
        result_long += f"{entry[4]}\n"
        result_long += "\n" + "-" * 40 + "\n"  # 添加分隔线以便区分不同文章

        # 构建短文本结果并进行分块处理
        current_entry = (
            f"文章：[{entry[1]}]({entry[2]})\n"
            f"上传时间：{entry[5]}\n"
            "\n" + "-" * 3 + "\n"  # 添加分隔线以便区分不同文章
        )
        temp_result = result_short + current_entry
        if len(temp_result.encode('utf-8')) > 4096:
            short_results.append(result_short)
            result_short = current_entry
        else:
            result_short = temp_result

        record(
            title=entry[1],
            link=entry[2],
            author=None,
            upload_time=entry[5],
            description=entry[4],
            source="Seebug",
            category=entry[3]
        )

    # 处理最后一个结果
    if result_short:
        short_results.append(result_short)

    return result_long, short_results

def Src_seebug(e_hour):
    if not os.path.exists('./resources/db/seebug.db'):
        # 创建数据库和表
        create_database()

    # 清空表
    clear_table()

    # 获取 JSON 数据
    M_seebug_data = get_seebug_json()

    # 插入数据到数据库
    insert_data(M_seebug_data)

    # 查询指定时间段内的数据
    filtered_articles = select_articles(e_hour)
    # print(filtered_articles)

    if filtered_articles:
        result_long, short_results = get_filtered_articles(filtered_articles)
        return result_long, short_results
    else:
        return False

if __name__ == "__main__":
    results = Src_seebug(100)
    if results != False:
        result_long, short_results = results
        
        # 打印长文本结果
        print("长文本结果：")
        print(result_long)
        print("\n" + "-" * 40 + "\n")
        
        # 打印分块的短文本结果
        print("分块的短文本结果：")
        for short_result in short_results:
            print(short_result)
            print("\n" + "-" * 40 + "\n")
    else:
        # 如果为空，则跳过执行
        print("-" * 40)
        print("Seebug社区数据为空，跳过执行。")