package service

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

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

type ourProductContentImagesService struct {
	Repo repository.OurProductContentImagesRepository
	Log  zerolog.Logger
}

type OurProductContentImagesService interface {
	FindByContentID(contentID uuid.UUID) ([]entity.OurProductContentImage, error)
	Save(data *entity.OurProductContentImage) (*entity.OurProductContentImage, error)
	Update(id uuid.UUID, data *entity.OurProductContentImage) error
	Delete(id uuid.UUID) error
}

func NewOurProductContentImagesService(
	repo repository.OurProductContentImagesRepository,
	log zerolog.Logger,
) OurProductContentImagesService {
	return &ourProductContentImagesService{
		Repo: repo,
		Log:  log,
	}
}

func (s *ourProductContentImagesService) FindByContentID(contentID uuid.UUID) ([]entity.OurProductContentImage, error) {
	data, err := s.Repo.FindByContentID(contentID)
	if err != nil {
		s.Log.Error().Err(err).Msg("failed get our product content images")
		return nil, err
	}

	return data, nil
}

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

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

	return result, nil
}

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

	return nil
}

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

	return nil
}