#!/usr/bin/env julia

"""
AI News Digest - Main Entry Point
Aggregates, filters, and compiles AI/tech news from multiple RSS sources
"""

using HTTP, XML, JSON, Dates, ArgParse

include("src/config.jl")
include("src/rss_parser.jl")  
include("src/ai_filter.jl")
include("src/digest_compiler.jl")
include("src/output_formatter.jl")

function parse_command_args()
    s = ArgParseSettings()
    @add_arg_table! s begin
        "--sources"
            help = "Feed categories: all, tech, ai"
            default = "all"
        "--limit"
            help = "Max stories in digest"
            arg_type = Int
            default = 5
        "--hours"
            help = "Look back period in hours"
            arg_type = Int
            default = 24
        "--output"
            help = "Output format: json, markdown, email"
            default = "markdown"
        "--config"
            help = "Config file path"
            default = "config.json"
        "--verbose", "-v"
            help = "Verbose output"
            action = :store_true
    end
    return parse_args(s)
end

function main()
    # Parse command line arguments
    args = parse_command_args()
    
    if args["verbose"]
        println("🤖 AI News Digest - Starting aggregation...")
    end
    
    try
        # Load configuration
        config = load_config(args["config"])
        
        # Get RSS feeds based on source filter
        feeds = get_feeds_by_category(config, args["sources"])
        
        if args["verbose"]
            println("📡 Fetching from $(length(feeds)) sources...")
        end
        
        # Fetch and parse all RSS feeds
        all_stories = []
        for feed in feeds
            try
                stories = fetch_rss_stories(feed, args["hours"])
                append!(all_stories, stories)
                if args["verbose"]
                    println("  ✓ $(feed["name"]): $(length(stories)) stories")
                end
            catch e
                if args["verbose"]
                    println("  ✗ $(feed["name"]): Error - $e")
                end
            end
        end
        
        if args["verbose"]
            println("🧠 Processing $(length(all_stories)) total stories...")
        end
        
        # Filter for AI relevance
        ai_stories = filter_ai_content(all_stories, config["ai_filter"])
        
        if args["verbose"]
            println("🎯 Found $(length(ai_stories)) AI-relevant stories")
        end
        
        # Remove duplicates and rank by relevance
        digest_stories = compile_digest(ai_stories, args["limit"])
        
        if args["verbose"]
            println("📰 Compiled digest with $(length(digest_stories)) top stories")
        end
        
        # Generate output in requested format
        output = format_output(digest_stories, args["output"], config)
        
        # Print result
        println(output)
        
        if args["verbose"]
            println("✅ Digest complete!")
        end
        
    catch e
        println("❌ Error: $e")
        exit(1)
    end
end

if abspath(PROGRAM_FILE) == @__FILE__
    main()
end