package client

import (
	"net/url"
	"strings"

	"github.com/0x2e/fusion/model"
	"github.com/0x2e/fusion/pkg/ptr"

	"github.com/mmcdole/gofeed"
)

func ParseGoFeedItems(feedURL string, gfItems []*gofeed.Item) []*model.Item {
	items := make([]*model.Item, 0, len(gfItems))
	for _, item := range gfItems {
		if item == nil {
			continue
		}

		unread := true
		content := item.Content
		if content == "" {
			content = item.Description
		}
		guid := item.GUID
		if guid == "" {
			guid = item.Link
		}
		pubDate := item.PublishedParsed
		if pubDate == nil {
			pubDate = item.UpdatedParsed
		}
		items = append(items, &model.Item{
			Title:   &item.Title,
			GUID:    &guid,
			Link:    ptr.To(parseLink(feedURL, item.Link)),
			Content: &content,
			PubDate: pubDate,
			Unread:  &unread,
		})
	}

	return items
}

func parseLink(feedURL string, linkURL string) string {
	// If the link URL is not a relative path, treat it as a full URL.
	if !strings.HasPrefix(linkURL, "/") {
		return linkURL
	}

	baseURL, err := url.Parse(feedURL)
	// If we can't parse the feed URL, we can't repair a relative path, so just
	// return whatever is in the link URL.
	if err != nil {
		return linkURL
	}

	pathURL, err := url.Parse(linkURL)
	// If we can't parse the link URL, we can't repair a relative path, so just
	// return whatever is in the link URL.
	if err != nil {
		return linkURL
	}

	// Combine the feed base URL with the relative path to create a full URL.
	return baseURL.ResolveReference(pathURL).String()
}
