package newssvc

import (
	newsdomain "legalgo-BE-go/internal/domain/news"
	timeutils "legalgo-BE-go/internal/utilities/time_utils"
	"legalgo-BE-go/internal/utilities/utils"
)

func (i *impl) Update(id string, spec newsdomain.NewsUpdate) error {
	tags, err := i.tagRepo.GetByIDs(spec.Tags)
	if err != nil {
		return err
	}

	categories, err := i.categoryRepo.GetByIDs(spec.Categories)
	if err != nil {
		return err
	}

	newSpec := newsdomain.News{
		ID:            spec.ID,
		AuthorID:      id,
		UpdatedAt:     timeutils.Now(),
		Slug:          utils.TitleToSlug(spec.Title),
		LiveAt:        spec.LiveAt,
		Title:         spec.Title,
		IsPremium:     spec.IsPremium,
		Content:       spec.Content,
		FeaturedImage: spec.FeaturedImage,
		Tags:          tags,
		Categories:    categories,
	}

	if err := i.newsRepo.Update(newSpec); err != nil {
		return err
	}

	return nil
}
