from datetime import datetime
import os
from LLMS import initialize_llm
from graph import create_news_search_workflow,create_article_scraper_workflow,create_blog_generator_workflow


def generate_ai_news_blog(groq_api_key=None, tavily_api_key=None, date=None):
    """
    Main function to generate AI news blog
    
    Args:
        groq_api_key (str, optional): Groq API key
        tavily_api_key (str, optional): Tavily API key
        date (str, optional): Date to search for news (YYYY-MM-DD format)
        
    Returns:
        str: Generated blog content in markdown format
    """
    # Set API keys if provided
    if groq_api_key:
        os.environ["GROQ_API_KEY"] = groq_api_key
    if tavily_api_key:
        os.environ["TAVILY_API_KEY"] = tavily_api_key
    
    # Initialize LLM with the API key
    initialize_llm(groq_api_key)
    
    # Get date
    if not date:
        today = datetime.now().strftime("%Y-%m-%d")
    else:
        today = date
    
    # Step 1: Search for AI news
    news_search = create_news_search_workflow()
    news_results = news_search.invoke({"query": "latest artificial intelligence news", "date": today})
    
    print(f"Found {len(news_results['news_items'])} AI news items")
    
    # Step 2: Scrape content for each news item
    article_scraper = create_article_scraper_workflow()
    news_contents = []
    
    for item in news_results["news_items"]:
        print(f"Scraping: {item['title']} from {item['source']}")
        result = article_scraper.invoke({"url": item['url']})
        
        # Skip if not in English
        if "not in English" in result["article_content"]:
            print(f"Skipping non-English content: {item['title']}")
            continue
        
        news_contents.append({
            "title": item['title'],
            "url": item['url'],
            "source": item['source'],
            "description": item['description'],
            "content": result["article_content"]
        })
    
    # Check if we have any news items
    if not news_contents:
        return "No English language AI news items found for the specified date. Please try a different date."
        
    # Format news content for the blog generator
    formatted_content = "\n\n".join([
        f"TITLE: {item['title']}\nSOURCE: {item['source']}\nURL: {item['url']}\nDESCRIPTION: {item['description']}\nCONTENT: {item['content'][:2000]}..."
        for item in news_contents
    ])
    
    # Step 3: Generate the blog
    blog_generator = create_blog_generator_workflow()
    blog_result = blog_generator.invoke({
        "content": formatted_content,
        "completed_sections": []
    })
    
    return blog_result["final_report"]