"""
RSS feed parsing and story extraction
"""

using HTTP, EzXML, Dates

struct Story
    title::String
    url::String
    summary::String
    published::DateTime
    source::String
    raw_content::String
end

function fetch_rss_stories(feed::Dict, hours_back::Int)
    """Fetch and parse stories from an RSS feed"""
    stories = Story[]
    
    try
        # Fetch RSS feed
        response = HTTP.get(feed["url"], timeout=30)
        xml_content = String(response.body)
        
        # Parse XML
        doc = parsexml(xml_content)
        root = doc.root
        
        # Find RSS items/entries
        items = findall(".//item", root)  # RSS 2.0
        if isempty(items)
            items = findall(".//entry", root)  # Atom
        end
        
        cutoff_time = now() - Hour(hours_back)
        
        for item in items
            try
                story = parse_rss_item(item, feed["name"])
                
                # Filter by time
                if story.published >= cutoff_time
                    push!(stories, story)
                end
            catch e
                # Skip malformed items
                continue
            end
        end
        
    catch e
        # Return empty array on feed fetch error
        @warn "Failed to fetch RSS feed $(feed["name"]): $e"
    end
    
    return stories
end

function parse_rss_item(item, source_name::String)
    """Parse individual RSS item into Story struct"""
    
    # Extract title
    title_node = findfirst(".//title", item)
    title = title_node !== nothing ? nodecontent(title_node) : "Untitled"
    title = clean_html_text(title)
    
    # Extract URL
    link_node = findfirst(".//link", item)
    if link_node !== nothing
        url = nodecontent(link_node)
        if isempty(url) && haskey(link_node, "href")
            url = link_node["href"]
        end
    else
        url = ""
    end
    
    # Extract summary/description
    desc_node = findfirst(".//description", item)
    if desc_node === nothing
        desc_node = findfirst(".//summary", item)
    end
    if desc_node === nothing
        desc_node = findfirst(".//content", item)
    end
    
    summary = desc_node !== nothing ? nodecontent(desc_node) : ""
    summary = clean_html_text(summary)
    
    # Extract publication date
    pub_date = parse_publication_date(item)
    
    # Create story
    return Story(title, url, summary, pub_date, source_name, summary)
end

function parse_publication_date(item)
    """Parse publication date from RSS item"""
    
    # Try different date fields
    date_fields = [".//pubDate", ".//published", ".//date", ".//dc:date"]
    
    for field in date_fields
        date_node = findfirst(field, item)
        if date_node !== nothing
            date_str = nodecontent(date_node)
            try
                # Parse RFC 2822 format (most common in RSS)
                return parse_rfc2822_date(date_str)
            catch
                try
                    # Parse ISO 8601 format (Atom feeds)
                    return DateTime(date_str[1:19])
                catch
                    continue
                end
            end
        end
    end
    
    # Fallback to current time
    return now()
end

function parse_rfc2822_date(date_str::String)
    """Parse RFC 2822 date format"""
    # This is a simplified parser - in production use a proper date parsing library
    try
        # Remove timezone info for simplicity
        date_clean = replace(date_str, r"[+-]\\d{4}$" => "")
        date_clean = replace(date_clean, r"\\s+[A-Z]{3}$" => "")
        
        # Try to parse as datetime
        return DateTime(date_clean, "e, d u y H:M:S")
    catch
        return now()
    end
end

function clean_html_text(html::String)
    """Remove HTML tags and clean text"""
    # Remove HTML tags
    text = replace(html, r"<[^>]*>" => "")
    
    # Decode common HTML entities
    text = replace(text, "&amp;" => "&")
    text = replace(text, "&lt;" => "<") 
    text = replace(text, "&gt;" => ">")
    text = replace(text, "&quot;" => "\"")
    text = replace(text, "&#39;" => "'")
    text = replace(text, "&nbsp;" => " ")
    
    # Clean whitespace
    text = replace(text, r"\\s+" => " ")
    text = strip(text)
    
    return text
end