package service

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

// UserRoleLevelDetailsService
type userRoleLevelDetailsService struct {
	Repo repository.UserRoleLevelDetailsRepository
	Log  zerolog.Logger
}

// UserRoleLevelDetailsService define interface of IUserRoleLevelDetailsService
type UserRoleLevelDetailsService interface {
	All(req request.UserRoleLevelDetailsQueryRequest) (userRoleLevelDetails []*response.UserRoleLevelDetailsResponse, paging paginator.Pagination, err error)
	Show(id uint) (userRoleLevelDetails *response.UserRoleLevelDetailsResponse, err error)
	Save(req request.UserRoleLevelDetailsCreateRequest) (err error)
	Update(id uint, req request.UserRoleLevelDetailsUpdateRequest) (err error)
	Delete(id uint) error
}

// NewUserRoleLevelDetailsService init UserRoleLevelDetailsService
func NewUserRoleLevelDetailsService(repo repository.UserRoleLevelDetailsRepository, log zerolog.Logger) UserRoleLevelDetailsService {

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

// All implement interface of UserRoleLevelDetailsService
func (_i *userRoleLevelDetailsService) All(req request.UserRoleLevelDetailsQueryRequest) (userRoleLevelDetailss []*response.UserRoleLevelDetailsResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(req)
	if err != nil {
		return
	}

	for _, result := range results {
		userRoleLevelDetailss = append(userRoleLevelDetailss, mapper.UserRoleLevelDetailsResponseMapper(result))
	}

	return
}

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

	return mapper.UserRoleLevelDetailsResponseMapper(result), nil
}

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

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

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

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