package response

import (
	"time"

	"gitlab.ost.ch/marketminds/marketminds/pkg/entity"
)

type newsItem struct {
	Id              int              `json:"id"`
	Title           string           `json:"title"`
	Description     string           `json:"description"`
	ThumbnailUrl    string           `json:"thumbnail_url"`
	Link            string           `json:"link"`
	PubDate         string           `json:"pub_date"`
	Category        string           `json:"category"`
	Guid            string           `json:"guid"`
	EnclosureUrl    string           `json:"enclosure_url"`
	MediaCopyright  string           `json:"media_copyright"`
	MediaTitle      string           `json:"media_title"`
	Version         string           `json:"version"`
	CreatedAt       string           `json:"created_at"`
	NewsChannel     *newsChannel     `json:"news_channel"`
	SentimentResult *sentimentResult `json:"sentiment_result"`
	NERResult       *nerResult       `json:"ner_result"`
}

func newNewsItem(input *entity.NewsItem) *newsItem {
	var newsChannel *newsChannel
	if input.NewsChannel != nil {
		newsChannel = newNewsChannel(input.NewsChannel)
	}

	var sentimentResult *sentimentResult
	if input.SentimentResult != nil {
		sentimentResult = newSentimentResult(input.SentimentResult)
	}

	var nerResult *nerResult
	if input.NERResult != nil {
		nerResult = newNerResult(input.NERResult)
	}

	return &newsItem{
		Id:              int(input.Id),
		Title:           input.Title,
		Description:     input.Description.String,
		ThumbnailUrl:    input.ThumbnailUrl.String,
		Link:            input.Link.String,
		PubDate:         input.PubDate.Time.UTC().Format(time.RFC3339),
		Category:        input.Category.String,
		Guid:            input.Guid.String,
		EnclosureUrl:    input.EnclosureUrl.String,
		MediaCopyright:  input.MediaCopyright.String,
		MediaTitle:      input.MediaTitle.String,
		Version:         input.Version.String,
		CreatedAt:       input.CreatedAt.UTC().Format(time.RFC3339),
		NewsChannel:     newsChannel,
		SentimentResult: sentimentResult,
		NERResult:       nerResult,
	}
}

func newNewsItems(input []*entity.NewsItem) []*newsItem {
	var newsItems []*newsItem
	for _, item := range input {
		newsItems = append(newsItems, newNewsItem(item))
	}
	return newsItems
}
