package service

import (
	"netidhub-saas-be/app/database/entity"
	"netidhub-saas-be/app/module/user_levels/mapper"
	"netidhub-saas-be/app/module/user_levels/repository"
	"netidhub-saas-be/app/module/user_levels/request"
	"netidhub-saas-be/app/module/user_levels/response"
	usersRepository "netidhub-saas-be/app/module/users/repository"
	"netidhub-saas-be/utils/paginator"
	utilSvc "netidhub-saas-be/utils/service"

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

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

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

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

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

// All implement interface of UserLevelsService
func (_i *userLevelsService) All(authToken string, req request.UserLevelsQueryRequest) (userLevelss []*response.UserLevelsResponse, paging paginator.Pagination, err error) {
	// Extract clientId from authToken
	var clientId *uuid.UUID
	if authToken != "" {
		user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
		if user != nil && user.ClientId != nil {
			clientId = user.ClientId
			_i.Log.Info().Interface("clientId", clientId).Msg("Extracted clientId from auth token")
		}
	}

	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(authToken string, id uint) (userLevels *response.UserLevelsResponse, err error) {
	// Extract clientId from authToken
	var clientId *uuid.UUID
	if authToken != "" {
		user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
		if user != nil && user.ClientId != nil {
			clientId = user.ClientId
			_i.Log.Info().Interface("clientId", clientId).Msg("Extracted clientId from auth token")
		}
	}

	result, err := _i.Repo.FindOne(clientId, id)
	if err != nil {
		return nil, err
	}

	return mapper.UserLevelsResponseMapper(result), nil
}

func (_i *userLevelsService) ShowByAlias(authToken string, alias string) (userLevels *response.UserLevelsResponse, err error) {
	// Extract clientId from authToken
	var clientId *uuid.UUID
	if authToken != "" {
		user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
		if user != nil && user.ClientId != nil {
			clientId = user.ClientId
			_i.Log.Info().Interface("clientId", clientId).Msg("Extracted clientId from auth token")
		}
	}

	result, err := _i.Repo.FindOneByAlias(clientId, alias)
	if err != nil {
		return nil, err
	}

	return mapper.UserLevelsResponseMapper(result), nil
}

func (_i *userLevelsService) Save(authToken string, req request.UserLevelsCreateRequest) (userLevels *entity.UserLevels, err error) {
	// Extract clientId from authToken
	var clientId *uuid.UUID
	if authToken != "" {
		user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
		if user != nil && user.ClientId != nil {
			clientId = user.ClientId
			_i.Log.Info().Interface("clientId", clientId).Msg("Extracted clientId from auth token")
		}
	}

	_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(authToken string, id uint, req request.UserLevelsUpdateRequest) (err error) {
	// Extract clientId from authToken
	var clientId *uuid.UUID
	if authToken != "" {
		user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
		if user != nil && user.ClientId != nil {
			clientId = user.ClientId
			_i.Log.Info().Interface("clientId", clientId).Msg("Extracted clientId from auth token")
		}
	}

	//_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(authToken string, id uint) error {
	// Extract clientId from authToken
	var clientId *uuid.UUID
	if authToken != "" {
		user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
		if user != nil && user.ClientId != nil {
			clientId = user.ClientId
			_i.Log.Info().Interface("clientId", clientId).Msg("Extracted clientId from auth token")
		}
	}

	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(authToken string, id uint, isApprovalActive bool) (err error) {
	// Extract clientId from authToken
	var clientId *uuid.UUID
	if authToken != "" {
		user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
		if user != nil && user.ClientId != nil {
			clientId = user.ClientId
			_i.Log.Info().Interface("clientId", clientId).Msg("Extracted clientId from auth token")
		}
	}

	result, err := _i.Repo.FindOne(clientId, id)
	if err != nil {
		return err
	}

	*result.IsApprovalActive = isApprovalActive

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