package service

import (
	"github.com/rs/zerolog"
	"go-humas-be/app/module/ppid_data_approval_histories/mapper"
	"go-humas-be/app/module/ppid_data_approval_histories/repository"
	"go-humas-be/app/module/ppid_data_approval_histories/request"
	"go-humas-be/app/module/ppid_data_approval_histories/response"
	usersRepository "go-humas-be/app/module/users/repository"
	utilSvc "go-humas-be/utils/service"
)

// PpidDataApprovalHistoriesService
type ppidDataApprovalHistoriesService struct {
	Repo      repository.PpidDataApprovalHistoriesRepository
	UsersRepo usersRepository.UsersRepository
	Log       zerolog.Logger
}

// PpidDataApprovalHistoriesService define interface of IPpidDataApprovalHistoriesService
type PpidDataApprovalHistoriesService interface {
	All() (ppidDataApprovalHistories []*response.PpidDataApprovalHistoriesResponse, err error)
	Show(id uint) (ppidDataApprovalHistories *response.PpidDataApprovalHistoriesResponse, err error)
	ShowByPpidData(ppidDataId uint) (ppidDataApprovalHistories []*response.PpidDataApprovalHistoriesResponse, err error)
	Save(req request.PpidDataApprovalHistoriesCreateRequest, authToken string) (err error)
	Delete(id uint) error
}

// NewPpidDataApprovalHistoriesService init PpidDataApprovalHistoriesService
func NewPpidDataApprovalHistoriesService(repo repository.PpidDataApprovalHistoriesRepository, usersRepo usersRepository.UsersRepository, log zerolog.Logger) PpidDataApprovalHistoriesService {

	return &ppidDataApprovalHistoriesService{
		Repo:      repo,
		UsersRepo: usersRepo,
		Log:       log,
	}
}

// All implement interface of PpidDataApprovalHistoriesService
func (_i *ppidDataApprovalHistoriesService) All() (ppidDataApprovalHistories []*response.PpidDataApprovalHistoriesResponse, err error) {
	results, err := _i.Repo.GetAll()
	if err != nil {
		return
	}

	for _, result := range results {
		ppidDataApprovalHistories = append(ppidDataApprovalHistories, mapper.PpidDataApprovalHistoriesResponseMapper(result, _i.UsersRepo))
	}

	return
}

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

	return mapper.PpidDataApprovalHistoriesResponseMapper(result, _i.UsersRepo), nil
}

func (_i *ppidDataApprovalHistoriesService) ShowByPpidData(ppidDataId uint) (ppidDataApprovalHistories []*response.PpidDataApprovalHistoriesResponse, err error) {
	results, err := _i.Repo.FindByPpidData(ppidDataId)
	if err != nil {
		return nil, err
	}

	for _, result := range results {
		ppidDataApprovalHistories = append(ppidDataApprovalHistories, mapper.PpidDataApprovalHistoriesResponseMapper(result, _i.UsersRepo))
	}

	return
}

func (_i *ppidDataApprovalHistoriesService) Save(req request.PpidDataApprovalHistoriesCreateRequest, authToken string) (err error) {
	_i.Log.Info().Interface("data", req).Msg("")
	newReq := req.ToEntity()

	createdBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	newReq.ApprovalBy = createdBy.ID

	return _i.Repo.Create(newReq)
}

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