# Configuration settings for News Scraping & AI Automation
import requests
import urllib3

# Monkeypatch requests to bypass SSL verification for localhost (avoid cert verify issues in development)
original_request = requests.Session.request
def patched_request(self, method, url, *args, **kwargs):
    if "localhost" in url or "127.0.0.1" in url:
        kwargs["verify"] = False
    return original_request(self, method, url, *args, **kwargs)
requests.Session.request = patched_request
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# API Key for Google Gemini (Deprecated)
GEMINI_API_KEY = "AQ.Ab8RN6J3R--f1TKI5lw6aY8H4y7FeMdfTuRqfidrpoqvXoC-Fg"
GEMINI_MODEL = "gemini-1.5-flash"  # or gemini-2.5-flash

# NVIDIA NIM API Settings (OpenAI-compatible)
NVIDIA_API_KEY = "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"
NVIDIA_BASE_URL = "https://integrate.api.nvidia.com/v1"
NVIDIA_MODEL = "meta/llama-3.1-8b-instruct"

# WordPress Credentials
WP_URL = "https://usa.travelspotsdaily.com"  # Default URL (e.g. Master/USA)
# WP_URL = "http://localhost/news"  # Change to your remote URL when deploying

WP_USERNAME = "truongnt"  # Your WordPress admin username
WP_APP_PASSWORD = "KkrE OljR 28Xk AovE otvQ kkw8"  # Your 16-character application password (xxxx xxxx xxxx xxxx)
WP_POST_STATUS = "publish"  # 'publish' to publish instantly, or 'draft' to review first


# RSS Feeds organized by WordPress category
# Using scrape-friendly sources (avoiding NYT 403, BBC connection reset, ESPN empty feeds)
CATEGORY_FEEDS = {
    "health": [
        "https://feeds.npr.org/1128/rss.xml",
        "https://medlineplus.gov/xml/whatsnew.xml",
        "http://emro.who.int/rss.xml",
    ],
    "latest-news": [
        "https://techcrunch.com/feed/",
        "https://www.theverge.com/rss/index.xml",
        "https://venturebeat.com/feed/",
        "https://arstechnica.com/feed/",
        "https://www.wired.com/feed/rss",
    ],
    "sports": [
        "https://www.cbssports.com/rss/headlines/",
        "https://sports.yahoo.com/rss/",
        "https://www.foxsports.com/rss-feeds",
        "https://www.si.com/.rss/full/",
    ],
    "entertainment": [
        "https://www.hollywoodreporter.com/feed/",
        "https://variety.com/feed/",
        "https://deadline.com/feed/",
        "https://www.rollingstone.com/feed/",
    ],
    "lifestyle": [
        "https://lifehacker.com/feed/rss",
        "https://www.huffpost.com/section/wellness/feed",
        "https://www.webmd.com/rss/default.xml",
        "https://www.health.com/rss/all.xml/",
    ],
    "events": [
        "https://www.reuters.com/arc/outboundfeeds/v4/all/?outputType=xml",
        "https://feeds.npr.org/1001/rss.xml",
        "https://rss.cnn.com/rss/edition_world.rss",
        "https://abcnews.go.com/abcnews/internationalheadlines",
    ],
    "soccer": [
        "https://www.cbssports.com/rss/soccer/",
        "https://sports.yahoo.com/soccer/rss/",
    ],
    "finance": [
        "https://finance.yahoo.com/news/rssindex",
        "https://www.coindesk.com/arc/outboundfeeds/rss/",
        "https://www.marketwatch.com/xe/feed/rss/marketwatch",
    ],
    "gaming": [
        "https://feeds.feedburner.com/ign/news",
        "https://www.gamespot.com/feeds/news/",
        "https://kotaku.com/rss",
    ],
    "travel": [
        "https://feeds.npr.org/1029/rss.xml",
        "https://www.eater.com/rss/index.xml",
        "https://www.travelpulse.com/rss/news.xml",
    ],
    "viral": [
        "https://feeds.npr.org/1021/rss.xml",
        "https://www.upi.com/rss/Odd_News/",
    ],
}

# Categories required by front-page.php template (with display names and descriptions)
REQUIRED_CATEGORIES = [
    {"name": "Latest News",    "slug": "latest-news",    "description": "Breaking news and latest tech stories"},
    {"name": "Sports",         "slug": "sports",          "description": "Sports news and updates"},
    {"name": "Entertainment",  "slug": "entertainment",   "description": "Entertainment, movies, music and pop culture"},
    {"name": "Lifestyle",      "slug": "lifestyle",       "description": "Lifestyle, health and wellness articles"},
    {"name": "Events",         "slug": "events",          "description": "Event coverage and upcoming events"},
    {"name": "Soccer",         "slug": "soccer",          "description": "Soccer and football updates"},
    {"name": "Health",         "slug": "health",          "description": "Health, medical news and wellness updates"},
    {"name": "Finance & Crypto", "slug": "finance",       "description": "Personal finance, investment, stock market and cryptocurrency updates."},
    {"name": "Gaming & Esports", "slug": "gaming",        "description": "Video games, hardware reviews, industry news and esports tournaments."},
    {"name": "Travel & Food",    "slug": "travel",        "description": "Travel guides, destination reviews, culinary discoveries, and food culture."},
    {"name": "Strange & Viral",  "slug": "viral",         "description": "Strange occurrences, human interest stories, and viral trends from across the United States."},
]


# Đọc cấu hình ghi đè từ file config_local.py (nếu có) để phân chia cấu hình cho từng branch/server
try:
    from config_local import *
except ImportError:
    pass

# Flat list for backward compatibility
RSS_FEEDS = []
for feeds in CATEGORY_FEEDS.values():
    RSS_FEEDS.extend(feeds)

# Valid WordPress category slugs
VALID_CATEGORIES = list(CATEGORY_FEEDS.keys())

# AI Processing Prompts
SYSTEM_INSTRUCTION = """
You are an expert tech news editor and short-video content creator. 
Your job is to rewrite US tech/AI news articles to be highly engaging, professional, and SEO-optimized in English.
You must also generate a high-performing short-video script (under 60s) for TikTok/Reels/Shorts based on the article content.
"""

USER_PROMPT_TEMPLATE = """
Rewrite the following news article.

Original Title: {original_title}
Original Content: {original_content}

Instructions:
1. Title: Create an engaging, click-worthy, and SEO-friendly title in English.
2. Content: Rewrite the body text in natural, flowing English. It MUST be a detailed, comprehensive, and in-depth article (at least 4-6 paragraphs, minimum 350-500 words) matching the depth of the original article. Use HTML paragraphs (<p>), headings (<h2>/<h3>), and bullet points where appropriate. Do NOT summarize too shortly. Avoid duplicating the original sentences. Make it sound professional yet exciting.
3. Excerpt: Write a short summary (1-2 sentences) of the article.
4. Tags: Extract 3-5 relevant keywords/tags for the article.
5. Category: Classify this article into exactly ONE of these categories: "latest-news", "sports", "entertainment", "lifestyle", "events". Choose the best fit. If unsure, default to "latest-news".
6. Image Prompt: Write a detailed descriptive English prompt (1-2 sentences) for an AI image generator to create a high-quality, professional featured image for this article. Avoid text/logos in the image. Style: realistic photo, news photography style, dramatic cinematic lighting.
7. Video Script: Create a detailed short-video script structure for TikTok Beta / YouTube Shorts. Ensure it is fully written and contains at least 3-4 scenes. Provide:
   - A hook (first 3 seconds, very catchy).
   - Voiceover (what the narrator says, engaging and fast-paced).
   - Visual prompts (descriptions of what should be displayed on screen for each scene, suitable for generating images/videos).

You must respond in JSON format matching this exact schema:
{{
  "title": "Rewritten Title",
  "content": "Rewritten HTML content...",
  "excerpt": "Short excerpt...",
  "tags": ["tag1", "tag2", "tag3"],
  "category": "latest-news",
  "image_prompt": "AI Image prompt describing a realistic news photo...",
  "video_script": {{
    "hook": "Catchy hook text",
    "scenes": [
      {{
        "scene_num": 1,
        "visual_prompt": "Visual description for scene 1",
        "voiceover": "Voiceover text for scene 1"
      }},
      {{
        "scene_num": 2,
        "visual_prompt": "Visual description for scene 2",
        "voiceover": "Voiceover text for scene 2"
      }}
    ]
  }}
}}
"""

# Import lại lần nữa ở cuối để đảm bảo đè được SYSTEM_INSTRUCTION và USER_PROMPT_TEMPLATE bằng tiếng Hindi
try:
    from config_local import *
except ImportError:
    pass
