import sys
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build

if hasattr(sys.stdout, 'reconfigure'):
    try:
        sys.stdout.reconfigure(encoding='utf-8')
        sys.stderr.reconfigure(encoding='utf-8')
    except Exception:
        pass

VIDEOS_TO_CHECK = [
    ("Ep 1", "DT202N1I4-A"),
    ("Ep 2", "FKPhXdwFbII"),
    ("Ep 3", "gZ40mP451Rc"),
    ("Ep 4", "O-8w_ONx3as"),
    ("Short 1", "l3lwPorZ70A"),
    ("Short 2", "MtB7VLHfcDk"),
    ("Full Movie", "c8Akru2j7ok")
]

def main():
    creds = Credentials.from_authorized_user_file('token.json')
    youtube = build('youtube', 'v3', credentials=creds)

    print("--- 1. KIỂM TRA TỪNG VIDEO ---")
    for name, vid in VIDEOS_TO_CHECK:
        try:
            req = youtube.commentThreads().list(part="snippet,replies", videoId=vid, maxResults=20)
            res = req.execute()
            items = res.get("items", [])
            print(f"[{name} - {vid}]: {len(items)} bình luận")
            for it in items:
                top = it["snippet"]["topLevelComment"]["snippet"]
                print(f"   -> [{top['authorDisplayName']}]: {top['textDisplay']} (ID: {it['id']})")
        except Exception as e:
            print(f"[{name} - {vid}]: Lỗi {e}")

    print("\n--- 2. KIỂM TRA BÌNH LUẬN CHỜ KIỂM DUYỆT (HELD FOR REVIEW) ---")
    try:
        req_held = youtube.commentThreads().list(
            part="snippet",
            allThreadsRelatedToChannelId="UClZnv08RDTD4vgLOw1jmOuA",
            moderationStatus="heldForReview",
            maxResults=20
        )
        res_held = req_held.execute()
        held_items = res_held.get("items", [])
        print(f"Bình luận chờ duyệt: {len(held_items)}")
        for it in held_items:
            top = it["snippet"]["topLevelComment"]["snippet"]
            print(f"   -> [{top['authorDisplayName']}]: {top['textDisplay']}")
    except Exception as e:
        print(f"Lỗi kiểm tra heldForReview: {e}")

if __name__ == "__main__":
    main()
