package services

import (
	"fmt"

	datasql "gitlab.ost.ch/marketminds/marketminds/pkg/data/sql"
	"gitlab.ost.ch/marketminds/marketminds/pkg/entity"
)

func (s *Service) GetAllNamedEntities() ([]*entity.NamedEntity, error) {
	session := datasql.NewSqlRepository(s.SqlInstance)

	namedEntities, err := session.GetAllNamedEntities()

	if err != nil {
		return nil, err
	}

	return namedEntities, nil
}

func (s *Service) GetNamedEntity(id string) (*entity.NamedEntity, error) {
	session := datasql.NewSqlRepository(s.SqlInstance)

	namedEntity, err := session.GetNamedEntityByName(id)

	if err != nil {
		return nil, err
	}

	newsItemsByNamedEntity, err := session.GetNewsItemsByNamedEntity(id)
	if err != nil {
		fmt.Println("Error getting news items by named entity:", err)
	}

	for _, newsItem := range newsItemsByNamedEntity {
		newsItem.NewsChannel, err = session.GetNewsChannelById(fmt.Sprint(newsItem.NewsChannelId))
		if err != nil {
			fmt.Println("Error getting news channel by id:", err)
		}
	}

	namedEntity.NewsItems = newsItemsByNamedEntity

	return namedEntity, nil
}
