package service

import (
	"mime/multipart"

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

	"web-qudo-be/app/database/entity"
	"web-qudo-be/app/module/our_product_content_images/repository"
	minioStorage "web-qudo-be/config/config"
	"web-qudo-be/utils/storage"
)

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

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

func NewOurProductContentImagesService(
	repo repository.OurProductContentImagesRepository,
	minio *minioStorage.MinioStorage,
	log zerolog.Logger,
) OurProductContentImagesService {
	return &ourProductContentImagesService{
		Repo:         repo,
		MinioStorage: minio,
		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) SaveWithFile(ourProductContentID uuid.UUID, file *multipart.FileHeader) (*entity.OurProductContentImage, error) {
	key, url, err := storage.UploadCMSObject(s.MinioStorage, "our-products", file, false)
	if err != nil {
		return nil, err
	}
	data := &entity.OurProductContentImage{
		OurProductContentID: ourProductContentID,
		ImagePath:         key,
		ImageURL:          url,
	}
	return s.Save(data)
}

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) UpdateWithFile(id uuid.UUID, file *multipart.FileHeader) error {
	key, url, err := storage.UploadCMSObject(s.MinioStorage, "our-products", file, false)
	if err != nil {
		return err
	}
	return s.Repo.Update(id, &entity.OurProductContentImage{
		ImagePath: key,
		ImageURL:  url,
	})
}

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
}
