package newssvc

import (
	newsdomain "legalgo-BE-go/internal/domain/news"
	"legalgo-BE-go/internal/utilities/utils"
	"time"

	"github.com/google/uuid"
)

func (i *impl) Create(spec newsdomain.NewsReq, staffId string) error {
	slug := utils.TitleToSlug(spec.Title)

	tags, err := i.tagRepo.GetByIDs(spec.Tags)
	if err != nil {
		return err
	}

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

	parsedTime, err := time.Parse(time.RFC3339, spec.LiveAt)
	if err != nil {
		return err
	}

	newSpec := newsdomain.News{
		ID:            uuid.NewString(),
		Title:         spec.Title,
		Content:       spec.Content,
		FeaturedImage: spec.FeaturedImage,
		IsPremium:     spec.IsPremium,
		Slug:          slug,
		LiveAt:        parsedTime,
		AuthorID:      staffId,
		Tags:          tags,
		Categories:    categories,
	}

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

	return nil
}
