package service

import (
	"github.com/rs/zerolog"
	"go-humas-be/app/module/article_category_details/mapper"
	"go-humas-be/app/module/article_category_details/repository"
	"go-humas-be/app/module/article_category_details/request"
	"go-humas-be/app/module/article_category_details/response"
	"go-humas-be/utils/paginator"
)

// ArticleCategoryDetailsService
type articleCategoryDetailsService struct {
	Repo repository.ArticleCategoryDetailsRepository
	Log  zerolog.Logger
}

// ArticleCategoryDetailsService define interface of IArticleCategoryDetailsService
type ArticleCategoryDetailsService interface {
	All(req request.ArticleCategoryDetailsQueryRequest) (articleCategoryDetails []*response.ArticleCategoryDetailsResponse, paging paginator.Pagination, err error)
	Show(id uint) (articleCategoryDetails *response.ArticleCategoryDetailsResponse, err error)
	Save(req request.ArticleCategoryDetailsCreateRequest) (err error)
	Update(id uint, req request.ArticleCategoryDetailsUpdateRequest) (err error)
	Delete(id uint) error
}

// NewArticleCategoryDetailsService init ArticleCategoryDetailsService
func NewArticleCategoryDetailsService(repo repository.ArticleCategoryDetailsRepository, log zerolog.Logger) ArticleCategoryDetailsService {

	return &articleCategoryDetailsService{
		Repo: repo,
		Log:  log,
	}
}

// All implement interface of ArticleCategoryDetailsService
func (_i *articleCategoryDetailsService) All(req request.ArticleCategoryDetailsQueryRequest) (articleCategoryDetailss []*response.ArticleCategoryDetailsResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(req)
	if err != nil {
		return
	}

	for _, result := range results {
		articleCategoryDetailss = append(articleCategoryDetailss, mapper.ArticleCategoryDetailsResponseMapper(result))
	}

	return
}

func (_i *articleCategoryDetailsService) Show(id uint) (articleCategoryDetails *response.ArticleCategoryDetailsResponse, err error) {
	result, err := _i.Repo.FindOne(id)
	if err != nil {
		return nil, err
	}

	return mapper.ArticleCategoryDetailsResponseMapper(result), nil
}

func (_i *articleCategoryDetailsService) Save(req request.ArticleCategoryDetailsCreateRequest) (err error) {
	_i.Log.Info().Interface("data", req).Msg("")

	return _i.Repo.Create(req.ToEntity())
}

func (_i *articleCategoryDetailsService) Update(id uint, req request.ArticleCategoryDetailsUpdateRequest) (err error) {
	_i.Log.Info().Interface("data", req).Msg("")
	return _i.Repo.Update(id, req.ToEntity())
}

func (_i *articleCategoryDetailsService) Delete(id uint) error {
	return _i.Repo.Delete(id)
}
