package service

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

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

type ourProductContentService struct {
	Repo         repository.OurProductContentRepository
	ImageService ourProductContentImagesService.OurProductContentImagesService
	Log          zerolog.Logger
}

type OurProductContentService interface {
	Show() ([]entity.OurProductContent, error)
	Save(data *entity.OurProductContent) (*entity.OurProductContent, error)
	Update(id uuid.UUID, data *entity.OurProductContent) error
	Delete(id uuid.UUID) error
}

func NewOurProductContentService(
	repo repository.OurProductContentRepository,
	imageService ourProductContentImagesService.OurProductContentImagesService,
	log zerolog.Logger,
) OurProductContentService {
	return &ourProductContentService{
		Repo:         repo,
		ImageService: imageService,
		Log:          log,
	}
}

func (s *ourProductContentService) Show() ([]entity.OurProductContent, error) {
	rows, err := s.Repo.GetAll()
	if err != nil {
		s.Log.Error().Err(err).Msg("failed list our product contents")
		return nil, err
	}
	return rows, nil
}

func (s *ourProductContentService) Save(data *entity.OurProductContent) (*entity.OurProductContent, 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")
		return nil, err
	}

	return result, nil
}

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

	return nil
}

func (s *ourProductContentService) Delete(id uuid.UUID) error {
	return s.Repo.Delete(id)
}