package service

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

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

type heroContentsService struct {
	Repo         repository.HeroContentsRepository
	ImageService heroContentImagesService.HeroContentImagesService
	Log          zerolog.Logger
}

type HeroContentsService interface {
	Show() (*entity.HeroContents, error)
	Save(data *entity.HeroContents) (*entity.HeroContents, error)
	Update(id uuid.UUID, data *entity.HeroContents) error
	Delete(id uuid.UUID) error
}

func NewHeroContentsService(
	repo repository.HeroContentsRepository,
	imageService heroContentImagesService.HeroContentImagesService,
	log zerolog.Logger,
) HeroContentsService {
	return &heroContentsService{
		Repo:         repo,
		ImageService: imageService,
		Log:          log,
	}
}

func (s *heroContentsService) Show() (*entity.HeroContents, error) {
	data, err := s.Repo.Get()
	if err != nil {
		s.Log.Error().Err(err).Msg("failed get hero content")
		return nil, err
	}

	return data, nil
}

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

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

	return result, nil
}

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

	return nil
}

func (s *heroContentsService) Delete(id uuid.UUID) error {
	result, err := s.Repo.Get()
	if err != nil {
		return err
	}

	isActive := false
	result.IsActive = &isActive

	return s.Repo.Update(id, result)
}