package service

import (
	"github.com/rs/zerolog"

	"web-qudo-be/app/database/entity"
	ourServiceContentImagesService "web-qudo-be/app/module/our_service_content_images/service"
	"web-qudo-be/app/module/our_service_contents/repository"
)

type ourServiceContentService struct {
	Repo         repository.OurServiceContentRepository
	ImageService ourServiceContentImagesService.OurServiceContentImagesService
	Log          zerolog.Logger
}

type OurServiceContentService interface {
	Show() ([]entity.OurServiceContent, error)
	Save(data *entity.OurServiceContent) (*entity.OurServiceContent, error)
	Update(id uint, data *entity.OurServiceContent) error
	Delete(id uint) error
}

func NewOurServiceContentService(
	repo repository.OurServiceContentRepository,
	imageService ourServiceContentImagesService.OurServiceContentImagesService,
	log zerolog.Logger,
) OurServiceContentService {
	return &ourServiceContentService{
		Repo:         repo,
		ImageService: imageService,
		Log:          log,
	}
}

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

func (s *ourServiceContentService) Save(data *entity.OurServiceContent) (*entity.OurServiceContent, error) {
	result, err := s.Repo.Create(data)
	if err != nil {
		s.Log.Error().Err(err).Msg("failed create our service content")
		return nil, err
	}

	// 🔥 kalau nanti mau save images sekalian
	// contoh:
	// for _, img := range data.Images {
	//     img.OurServiceContentID = result.ID
	//     s.ImageService.Save(&img)
	// }

	return result, nil
}

func (s *ourServiceContentService) Update(id uint, data *entity.OurServiceContent) error {
	err := s.Repo.Update(id, data)
	if err != nil {
		s.Log.Error().Err(err).Msg("failed update our service content")
		return err
	}

	// 🔥 handle update images (opsional)
	// bisa delete lama → insert baru
	// atau update satu-satu

	return nil
}

func (s *ourServiceContentService) Delete(id uint) error {
	return s.Repo.Delete(id)
}