package service

import (
	"web-qudo-be/app/database/entity"
	"web-qudo-be/app/module/user_levels/mapper"
	"web-qudo-be/app/module/user_levels/repository"
	"web-qudo-be/app/module/user_levels/request"
	"web-qudo-be/app/module/user_levels/response"
	"web-qudo-be/utils/paginator"

	"github.com/google/uuid"
	"github.com/rs/zerolog"
)

// UserLevelsService
type userLevelsService struct {
	Repo repository.UserLevelsRepository
	Log  zerolog.Logger
}

// UserLevelsService define interface of IUserLevelsService
type UserLevelsService interface {
	All(clientId *uuid.UUID, req request.UserLevelsQueryRequest) (userLevels []*response.UserLevelsResponse, paging paginator.Pagination, err error)
	Show(clientId *uuid.UUID, id uint) (userLevels *response.UserLevelsResponse, err error)
	ShowByAlias(clientId *uuid.UUID, alias string) (userLevels *response.UserLevelsResponse, err error)
	Save(clientId *uuid.UUID, req request.UserLevelsCreateRequest) (userLevels *entity.UserLevels, err error)
	Update(clientId *uuid.UUID, id uint, req request.UserLevelsUpdateRequest) (err error)
	Delete(clientId *uuid.UUID, id uint) error
	EnableApproval(clientId *uuid.UUID, id uint, isApprovalActive bool) (err error)
}

// NewUserLevelsService init UserLevelsService
func NewUserLevelsService(repo repository.UserLevelsRepository, log zerolog.Logger) UserLevelsService {

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

// All implement interface of UserLevelsService
func (_i *userLevelsService) All(clientId *uuid.UUID, req request.UserLevelsQueryRequest) (userLevelss []*response.UserLevelsResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(clientId, req)
	if err != nil {
		return
	}

	for _, result := range results {
		userLevelss = append(userLevelss, mapper.UserLevelsResponseMapper(result))
	}

	return
}

func (_i *userLevelsService) Show(clientId *uuid.UUID, id uint) (userLevels *response.UserLevelsResponse, err error) {
	result, err := _i.Repo.FindOne(clientId, id)
	if err != nil {
		return nil, err
	}

	return mapper.UserLevelsResponseMapper(result), nil
}

func (_i *userLevelsService) ShowByAlias(clientId *uuid.UUID, alias string) (userLevels *response.UserLevelsResponse, err error) {
	result, err := _i.Repo.FindOneByAlias(clientId, alias)
	if err != nil {
		return nil, err
	}

	return mapper.UserLevelsResponseMapper(result), nil
}

func (_i *userLevelsService) Save(clientId *uuid.UUID, req request.UserLevelsCreateRequest) (userLevels *entity.UserLevels, err error) {
	_i.Log.Info().Interface("data", req).Msg("")

	entity := req.ToEntity()
	// Set ClientId on entity
	entity.ClientId = clientId

	saveUserLevelsRes, err := _i.Repo.Create(clientId, entity)
	if err != nil {
		return nil, err
	}

	return saveUserLevelsRes, nil
}

func (_i *userLevelsService) Update(clientId *uuid.UUID, id uint, req request.UserLevelsUpdateRequest) (err error) {
	//_i.Log.Info().Interface("data", req).Msg("")

	_i.Log.Info().Interface("data", req.ToEntity()).Msg("")

	// Set ClientId on entity
	entity := req.ToEntity()
	entity.ClientId = clientId

	return _i.Repo.Update(clientId, id, entity)
}

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

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

func (_i *userLevelsService) EnableApproval(clientId *uuid.UUID, id uint, isApprovalActive bool) (err error) {
	result, err := _i.Repo.FindOne(clientId, id)
	if err != nil {
		return err
	}

	*result.IsApprovalActive = isApprovalActive

	return _i.Repo.Update(clientId, id, result)
}
