package service

import (
	"github.com/rs/zerolog"
	"go-humas-be/app/database/entity"
	userLevelsRepository "go-humas-be/app/module/user_levels/repository"
	userRoleLevelDetailsRepository "go-humas-be/app/module/user_role_level_details/repository"
	"go-humas-be/app/module/user_roles/mapper"
	"go-humas-be/app/module/user_roles/repository"
	"go-humas-be/app/module/user_roles/request"
	"go-humas-be/app/module/user_roles/response"
	usersRepository "go-humas-be/app/module/users/repository"
	"go-humas-be/utils/paginator"
	utilSvc "go-humas-be/utils/service"
)

// UserRolesService
type userRolesService struct {
	Repo                     repository.UserRolesRepository
	UsersRepo                usersRepository.UsersRepository
	UserLevelsRepo           userLevelsRepository.UserLevelsRepository
	UserRoleLevelDetailsRepo userRoleLevelDetailsRepository.UserRoleLevelDetailsRepository
	Log                      zerolog.Logger
}

// UserRolesService define interface of IUserRolesService
type UserRolesService interface {
	All(req request.UserRolesQueryRequest) (userRoles []*response.UserRolesResponse, paging paginator.Pagination, err error)
	Show(id uint) (userRoles *response.UserRolesResponse, err error)
	Save(req request.UserRolesCreateRequest, authToken string) (err error)
	Update(id uint, req request.UserRolesUpdateRequest) (err error)
	Delete(id uint) error
}

// NewUserRolesService init UserRolesService
func NewUserRolesService(
	repo repository.UserRolesRepository,
	usersRepo usersRepository.UsersRepository,
	userLevelsRepo userLevelsRepository.UserLevelsRepository,
	userRoleLevelDetailsRepo userRoleLevelDetailsRepository.UserRoleLevelDetailsRepository,
	log zerolog.Logger,
) UserRolesService {

	return &userRolesService{
		Repo:                     repo,
		UsersRepo:                usersRepo,
		UserLevelsRepo:           userLevelsRepo,
		UserRoleLevelDetailsRepo: userRoleLevelDetailsRepo,
		Log:                      log,
	}
}

// All implement interface of UserRolesService
func (_i *userRolesService) All(req request.UserRolesQueryRequest) (userRoless []*response.UserRolesResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(req)

	if err != nil {
		return
	}

	for _, result := range results {
		userRoless = append(userRoless, mapper.UserRolesResponseMapper(result))
	}

	return
}

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

	return mapper.UserRolesResponseMapper(result), nil
}

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

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

	dataResult, err := _i.Repo.Create(newReq)

	for _, levelId := range req.UserLevelIds {
		var userRoleLevelDetails *entity.UserRoleLevelDetails
		userRoleLevelDetails = &entity.UserRoleLevelDetails{
			UserRoleId:  dataResult.ID,
			UserLevelId: uint(levelId),
		}
		err := _i.UserRoleLevelDetailsRepo.Create(userRoleLevelDetails)
		if err != nil {
			return err
		}
	}

	return err
}

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

func (_i *userRolesService) Delete(id uint) (err error) {
	result, err := _i.Repo.FindOne(id)
	if err != nil {
		return err
	}

	isActive := false
	result.IsActive = &isActive
	return _i.Repo.Update(id, result)
}
