import sys
import json
import time
from pathlib import Path
from bot_cdp import post_content_via_cdp

def post_us_story(post_id: int):
    json_path = Path(r'C:\Projects\Historical\content\us_history_posts.json')
    with open(json_path, 'r', encoding='utf-8') as f:
        posts = json.load(f)
        
    target = None
    for p in posts:
        if p['id'] == post_id:
            target = p
            break
            
    if not target:
        print(f'❌ Không tìm thấy bài đăng có ID {post_id}')
        return
        
    print(f'=== ĐĂNG BÀI LỊCH SỬ MỸ #{post_id}: {target[\"title\"]} ===')
    temp_txt = Path(r'C:\Projects\Historical\FacebookAutoPoster\temp_us_post.txt')
    temp_txt.write_text(target['content'], encoding='utf-8')
    
    post_content_via_cdp(str(temp_txt))
    print(f'✅ Hoàn tất đăng bài #{post_id}!')

if __name__ == '__main__':
    pid = int(sys.argv[1]) if len(sys.argv) > 1 else 1
    post_us_story(pid)
