package service

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

// MasterStatusesService
type masterStatusesService struct {
	Repo repository.MasterStatusesRepository
	Log  zerolog.Logger
}

// MasterStatusesService define interface of IMasterStatusesService
type MasterStatusesService interface {
	All(req request.MasterStatusesQueryRequest) (masterStatuses []*response.MasterStatusesResponse, paging paginator.Pagination, err error)
	Show(id uint) (masterStatuses *response.MasterStatusesResponse, err error)
	Save(req request.MasterStatusesCreateRequest) (err error)
	Update(id uint, req request.MasterStatusesUpdateRequest) (err error)
	Delete(id uint) error
}

// NewMasterStatusesService init MasterStatusesService
func NewMasterStatusesService(repo repository.MasterStatusesRepository, log zerolog.Logger) MasterStatusesService {

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

// All implement interface of MasterStatusesService
func (_i *masterStatusesService) All(req request.MasterStatusesQueryRequest) (masterStatusess []*response.MasterStatusesResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(req)
	if err != nil {
		return
	}

	for _, result := range results {
		masterStatusess = append(masterStatusess, mapper.MasterStatusesResponseMapper(result))
	}

	return
}

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

	return mapper.MasterStatusesResponseMapper(result), nil
}

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

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

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

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