package repository

import (
	"jaecoo-be/app/database"
	"jaecoo-be/app/database/entity"
	"jaecoo-be/app/module/product_specifications/request"
	"jaecoo-be/utils/paginator"
	"strings"

	"github.com/rs/zerolog"
)

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

type ProductSpecificationsRepository interface {
	GetAll(req request.ProductSpecificationsQueryRequest) (specs []*entity.ProductSpecifications, paging paginator.Pagination, err error)
	FindOne(id uint) (spec *entity.ProductSpecifications, err error)
	Create(spec *entity.ProductSpecifications) (specReturn *entity.ProductSpecifications, err error)
	Update(id uint, spec *entity.ProductSpecifications) (err error)
	Delete(id uint) (err error)
	FindByThumbnailPath(thumbnailPath string) (spec *entity.ProductSpecifications, err error)
}

func NewProductSpecificationsRepository(db *database.Database, log zerolog.Logger) ProductSpecificationsRepository {
	return &productSpecificationsRepository{
		DB:  db,
		Log: log,
	}
}

func (_i *productSpecificationsRepository) GetAll(req request.ProductSpecificationsQueryRequest) (specs []*entity.ProductSpecifications, paging paginator.Pagination, err error) {
	var count int64

	query := _i.DB.DB.Model(&entity.ProductSpecifications{})

	if req.ProductID != nil {
		query = query.Where("product_id = ?", *req.ProductID)
	}

	if req.Title != nil && *req.Title != "" {
		title := strings.ToLower(*req.Title)
		query = query.Where("LOWER(title) LIKE ?", "%"+title+"%")
	}

	query = query.Where("is_active = ?", true)

	query.Count(&count)

	req.Pagination.Count = count
	req.Pagination = paginator.Paging(req.Pagination)

	query = query.Order("created_at DESC")

	err = query.Offset(req.Pagination.Offset).Limit(req.Pagination.Limit).Find(&specs).Error
	if err != nil {
		return
	}

	paging = *req.Pagination

	return
}

func (_i *productSpecificationsRepository) FindOne(id uint) (spec *entity.ProductSpecifications, err error) {
	spec = &entity.ProductSpecifications{}
	err = _i.DB.DB.Where("id = ? AND is_active = ?", id, true).First(spec).Error
	return
}

func (_i *productSpecificationsRepository) Create(spec *entity.ProductSpecifications) (specReturn *entity.ProductSpecifications, err error) {
	if err = _i.DB.DB.Create(spec).Error; err != nil {
		return
	}
	specReturn = spec
	return
}

func (_i *productSpecificationsRepository) Update(id uint, spec *entity.ProductSpecifications) (err error) {
	err = _i.DB.DB.Model(&entity.ProductSpecifications{}).Where("id = ?", id).Updates(spec).Error
	return
}

func (_i *productSpecificationsRepository) Delete(id uint) (err error) {
	err = _i.DB.DB.Model(&entity.ProductSpecifications{}).Where("id = ?", id).Update("is_active", false).Error
	return
}

func (_i *productSpecificationsRepository) FindByThumbnailPath(thumbnailPath string) (spec *entity.ProductSpecifications, err error) {
	spec = &entity.ProductSpecifications{}
	err = _i.DB.DB.Where("thumbnail_path LIKE ? AND is_active = ?", "%"+thumbnailPath, true).First(spec).Error
	return
}

