from pymongo import MongoClient
from typing import List, Any
from connect_mongo import connect_to_mongo

class NewsDatabaseHandler:
    def __init__(self, uri: str, db_name: str):
        """
        Khởi tạo kết nối MongoDB một lần duy nhất.
        """
        self.client = MongoClient(uri)
        self.db = self.client[db_name]

    def get_all(self, collection_name: str) -> List[dict]:
        """
        Lấy tất cả bản ghi từ collection.
        """
        collection = self.db[collection_name]
        return list(collection.find())

    def find_by_category(self, collection_name: str, category: str) -> List[dict]:
        """
        Tìm các bản ghi theo category.
        """
        collection = self.db[collection_name]
        query = {"category": category}
        return list(collection.find(query))

    def save_to_mongo(self, data: List[dict], collection_name: str):
        """
        Lưu dữ liệu vào MongoDB.
        """
        collection = self.db[collection_name]
        collection.insert_many(data)

    def close(self):
        """
        Đóng kết nối MongoDB.
        """
        self.client.close()

from datetime import datetime

def find_key():
    db_name = "news"
    collection_name = "articles"
    
    collection = connect_to_mongo(db_name, collection_name)
    
    # Tìm tất cả các bài viết có "name" chứa từ "Campuchia" và thuộc về TuoiTre hoặc VnExpress
    vnexpress_or_tuoitre_news = collection.find({
        "$in": [
            {"VnExpress.name": {"$regex": "Campuchia", "$options": "i"}}  # Tìm kiếm trong VnExpres  # Tìm kiếm trong TuoiTre
        ]
    })

    # In ra các kết quả tìm được
    for result in vnexpress_or_tuoitre_news:
        # Nếu bạn chỉ muốn thấy bài viết chứa từ "Campuchia" trong tên:
        if "Campuchia" in result.get('VnExpress', [{}])[0].get('name', '') or "Campuchia" in result.get('TuoiTre', [{}])[0].get('name', ''):
            print(result)

# Gọi hàm tìm kiếm
# find_key()

    
def get_vnexpress_news(category):
    db_name = "news"
    collection_name = "articles"
    
    collection = connect_to_mongo(db_name, collection_name)
    
    
    # # Lấy thời gian hiện tại
    # today = datetime.now().strftime("%Y-%m-%d")
    # start_time = f"{today} 21:40:00"
    # print(f"Start time: {start_time}")
    
    # # Truy vấn MongoDB
    # vnexpress_news = collection.find({
    #     "category": category,
    #     "VnExpress.time": {"$gte": start_time}  # Điều kiện so sánh thời gian
    # })

 

    # filtered_news = []
    # # Duyệt qua các bài báo và lọc ra các bài hợp lệ
    # for result in vnexpress_news:
    #     if "VnExpress" in result:
    #         for news in result["VnExpress"]:
    #             # Lọc bỏ các mục trống hoặc không có dữ liệu
    #             if news and "time" in news:
    #                 print(f"Checking time for news: {news['time']}")
    #                 if news["time"] >= start_time:
    #                     filtered_news.append(news)

    # return filtered_news

# Gọi hàm và in kết quả
# news = get_vnexpress_news("VnExpress")


# if not news:
#     print("No news found matching the criteria.")
# else:
#     for article in news:
#         print(f"Found article: {article}")
# Cấu hình kết nối



db_name = "test"
collection_name = "articles"
collection = connect_to_mongo(db_name, collection_name)

import datetime


# Tính thời gian 1h30 trước từ thời điểm hiện tại
time_limit = datetime.datetime.now() - datetime.timedelta(minutes=20)

time_limit = time_limit.replace(tzinfo=datetime.timezone.utc)
print(time_limit)


query = {
    "category": "VnExpress",
    "time": {"$gte": time_limit}
}

results = collection.find(query)

for result in results:
    print("Bản ghi:")
    print(result)
    print("\n")