package repository

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

	"github.com/rs/zerolog"
)

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

// interface
type AboutUsContentImageRepository interface {
	GetAll() (images []*entity.AboutUsContentImage, err error)
	FindOne(id uint) (image *entity.AboutUsContentImage, err error)
	FindByContentID(contentID uint) (images []*entity.AboutUsContentImage, err error)
	Create(image *entity.AboutUsContentImage) (imageReturn *entity.AboutUsContentImage, err error)
	Delete(id uint) (err error)
}

// constructor
func NewAboutUsContentImageRepository(db *database.Database, log zerolog.Logger) AboutUsContentImageRepository {
	return &aboutUsContentImageRepository{
		DB:  db,
		Log: log,
	}
}

// ==============================
// IMPLEMENTATION
// ==============================

// GET ALL
func (_i *aboutUsContentImageRepository) GetAll() (images []*entity.AboutUsContentImage, err error) {
	err = _i.DB.DB.Find(&images).Error
	return
}

// GET BY ID
func (_i *aboutUsContentImageRepository) FindOne(id uint) (image *entity.AboutUsContentImage, err error) {
	if err := _i.DB.DB.First(&image, id).Error; err != nil {
		return nil, err
	}
	return image, nil
}

// GET BY ABOUT US CONTENT ID
func (_i *aboutUsContentImageRepository) FindByContentID(contentID uint) (images []*entity.AboutUsContentImage, err error) {
	err = _i.DB.DB.
		Where("about_us_content_id = ?", contentID).
		Find(&images).Error
	return
}

// CREATE
func (_i *aboutUsContentImageRepository) Create(image *entity.AboutUsContentImage) (imageReturn *entity.AboutUsContentImage, err error) {
	result := _i.DB.DB.Create(image)
	return image, result.Error
}

// DELETE (hard delete, bisa ubah ke soft delete kalau mau)
func (_i *aboutUsContentImageRepository) Delete(id uint) (err error) {
	return _i.DB.DB.Delete(&entity.AboutUsContentImage{}, id).Error
}