package main

import (
	"context"
	"log"
	"time"

	"github.com/mmcdole/gofeed"
)

func fetchRSSFeeds() {
	ctx := context.Background()

	feeds, err := fetchPendingFeeds(ctx)
	if err != nil {
		log.Printf("fetchPendingFeeds failed: %v", err)
		return
	}

	if len(feeds) == 0 {
		log.Println("no feeds due for refresh")
		return
	}

	parser := gofeed.NewParser()

	for _, feed := range feeds {
		fp, err := parser.ParseURL(feed.URL)
		if err != nil {
			log.Printf("parse feed %s: %v", feed.URL, err)
			if err := setFeedAccess(ctx, feed.ID, false); err != nil {
				log.Printf("disable feed %d failed: %v", feed.ID, err)
			}
			continue
		}

		var articles []Article
		for _, item := range fp.Items {
			var published *time.Time
			if item.PublishedParsed != nil {
				published = item.PublishedParsed
			}

			summary := item.Description
			if summary == "" {
				summary = item.Content
			}

			articles = append(articles, Article{
				ArticleID:   articleIDFromLink(item.Link),
				FeedID:      feed.ID,
				Title:       item.Title,
				Link:        item.Link,
				Summary:     summary,
				PublishedAt: published,
			})
		}

		if err := saveArticles(ctx, feed.ID, articles); err != nil {
			log.Printf("save articles for feed %d: %v", feed.ID, err)
			continue
		}

		log.Printf("processed feed %d (%s) with %d items", feed.ID, feed.URL, len(articles))
	}
}
