package repository

import (
	"web-qudo-be/app/database"
	"web-qudo-be/app/database/entity"

	"github.com/rs/zerolog"
)

type ourServiceContentImagesRepository struct {
	DB  *database.Database
	Log zerolog.Logger
}

type OurServiceContentImagesRepository interface {
	FindByContentID(contentID uint) ([]entity.OurServiceContentImage, error)
	Create(data *entity.OurServiceContentImage) (*entity.OurServiceContentImage, error)
	Update(id uint, data *entity.OurServiceContentImage) error
	Delete(id uint) error
}

func NewOurServiceContentImagesRepository(db *database.Database, log zerolog.Logger) OurServiceContentImagesRepository {
	return &ourServiceContentImagesRepository{
		DB:  db,
		Log: log,
	}
}

func (r *ourServiceContentImagesRepository) FindByContentID(contentID uint) ([]entity.OurServiceContentImage, error) {
	var data []entity.OurServiceContentImage

	err := r.DB.DB.
		Where("our_service_content_id = ?", contentID).
		Find(&data).Error

	if err != nil {
		r.Log.Error().Err(err).Msg("failed get our service content images")
		return nil, err
	}

	return data, nil
}

func (r *ourServiceContentImagesRepository) Create(data *entity.OurServiceContentImage) (*entity.OurServiceContentImage, error) {
	err := r.DB.DB.Create(data).Error
	if err != nil {
		r.Log.Error().Err(err).Msg("failed create our service content images")
		return nil, err
	}

	return data, nil
}

func (r *ourServiceContentImagesRepository) Update(id uint, data *entity.OurServiceContentImage) error {
	err := r.DB.DB.
		Model(&entity.OurServiceContentImage{}).
		Where("id = ?", id).
		Updates(data).Error

	if err != nil {
		r.Log.Error().Err(err).Msg("failed update our service content images")
		return err
	}

	return nil
}

func (r *ourServiceContentImagesRepository) Delete(id uint) error {
	err := r.DB.DB.Delete(&entity.OurServiceContentImage{}, id).Error
	if err != nil {
		r.Log.Error().Err(err).Msg("failed delete our service content images")
		return err
	}

	return nil
}