package newssvc

import (
	newsdomain "legalgo-BE-go/internal/domain/news"
	"strings"
)

func (i *impl) GetAll(filter newsdomain.Filter) ([]newsdomain.NewsResponse, error) {
	var err error

	categories := []string{}
	tags := []string{}
	news := []newsdomain.NewsResponse{}

	tagCodeArr := strings.Split(filter.Tags, " ")
	categoryCodeArr := strings.Split(filter.Category, " ")

	if len(tagCodeArr) > 0 && tagCodeArr[0] != "" {
		tags, err = i.tagRepo.GetIDsByCodes(tagCodeArr)
		if err != nil {
			return news, err
		}

		if len(tags) < 1 {
			return news, nil
		}
	}

	if len(categoryCodeArr) > 0 && categoryCodeArr[0] != "" {
		categories, err = i.categoryRepo.GetIDByCode(categoryCodeArr)
		if err != nil {
			return news, err
		}

		if len(categories) < 1 {
			return news, nil
		}
	}

	filterSpec := newsdomain.NewsFilter{
		Tags:     tags,
		Category: categories,
		Active:   filter.Active,
		Search:   filter.Search,
	}
	return i.newsRepo.GetAll(filterSpec)
}
