package service

import (
	"github.com/google/uuid"
	"github.com/rs/zerolog"

	"web-qudo-be/app/database/entity"
	"web-qudo-be/app/module/hero_content_images/repository"
)

type heroContentImagesService struct {
	Repo repository.HeroContentImagesRepository
	Log  zerolog.Logger
}

type HeroContentImagesService interface {
	FindByHeroID(heroID uuid.UUID) (*entity.HeroContentImages, error)
	Save(data *entity.HeroContentImages) (*entity.HeroContentImages, error)
	Update(id uuid.UUID, data *entity.HeroContentImages) error
	Delete(id uuid.UUID) error
}

func NewHeroContentImagesService(
	repo repository.HeroContentImagesRepository,
	log zerolog.Logger,
) HeroContentImagesService {
	return &heroContentImagesService{
		Repo: repo,
		Log:  log,
	}
}

func (s *heroContentImagesService) FindByHeroID(heroID uuid.UUID) (*entity.HeroContentImages, error) {
	data, err := s.Repo.FindByHeroID(heroID)
	if err != nil {
		s.Log.Error().Err(err).Msg("failed get hero content image")
		return nil, err
	}

	return data, nil
}

func (s *heroContentImagesService) Save(data *entity.HeroContentImages) (*entity.HeroContentImages, error) {
	data.ID = uuid.New()

	result, err := s.Repo.Create(data)
	if err != nil {
		s.Log.Error().Err(err).Msg("failed create hero content image")
		return nil, err
	}

	return result, nil
}

func (s *heroContentImagesService) Update(id uuid.UUID, data *entity.HeroContentImages) error {
	err := s.Repo.Update(id, data)
	if err != nil {
		s.Log.Error().Err(err).Msg("failed update hero content image")
		return err
	}

	return nil
}

func (s *heroContentImagesService) Delete(id uuid.UUID) error {
	err := s.Repo.Delete(id)
	if err != nil {
		s.Log.Error().Err(err).Msg("failed delete hero content image")
		return err
	}

	return nil
}