package service

import (
	"github.com/google/uuid"
	"github.com/rs/zerolog"
	"netidhub-saas-be/app/database/entity"
	"netidhub-saas-be/app/module/feedbacks/mapper"
	"netidhub-saas-be/app/module/feedbacks/repository"
	"netidhub-saas-be/app/module/feedbacks/request"
	"netidhub-saas-be/app/module/feedbacks/response"
	usersRepository "netidhub-saas-be/app/module/users/repository"
	"netidhub-saas-be/utils/paginator"
)

// FeedbacksService
type feedbacksService struct {
	Repo      repository.FeedbacksRepository
	UsersRepo usersRepository.UsersRepository
	Log       zerolog.Logger
}

// FeedbacksService define interface of IFeedbacksService
type FeedbacksService interface {
	All(clientId *uuid.UUID, req request.FeedbacksQueryRequest) (feedbacks []*response.FeedbacksResponse, paging paginator.Pagination, err error)
	Show(clientId *uuid.UUID, id uint) (feedbacks *response.FeedbacksResponse, err error)
	Save(clientId *uuid.UUID, req request.FeedbacksCreateRequest, authToken string) (feedbacks *entity.Feedbacks, err error)
	Update(clientId *uuid.UUID, id uint, req request.FeedbacksUpdateRequest) (err error)
	Delete(clientId *uuid.UUID, id uint) error
	FeedbackMonthlyStats(clientId *uuid.UUID, authToken string, year *int) (articleMonthlyStats []*response.FeedbacksMonthlyStats, err error)
}

// NewFeedbacksService init FeedbacksService
func NewFeedbacksService(repo repository.FeedbacksRepository, log zerolog.Logger, usersRepo usersRepository.UsersRepository) FeedbacksService {

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

// All implement interface of FeedbacksService
func (_i *feedbacksService) All(clientId *uuid.UUID, req request.FeedbacksQueryRequest) (feedbackss []*response.FeedbacksResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(clientId, req)
	if err != nil {
		return
	}

	for _, result := range results {
		feedbackss = append(feedbackss, mapper.FeedbacksResponseMapper(result))
	}

	return
}

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

	return mapper.FeedbacksResponseMapper(result), nil
}

func (_i *feedbacksService) Save(clientId *uuid.UUID, req request.FeedbacksCreateRequest, authToken string) (feedbacks *entity.Feedbacks, err error) {
	_i.Log.Info().Interface("data", req).Msg("")

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

	return _i.Repo.Create(entity)
}

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

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

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

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

func (_i *feedbacksService) FeedbackMonthlyStats(clientId *uuid.UUID, authToken string, year *int) (articleMonthlyStats []*response.FeedbacksMonthlyStats, err error) {
	//user := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)

	//var userLevelId *uint
	//var userLevelNumber *int
	//
	//if user != nil {
	//	userLevelId = &user.UserLevelId
	//	userLevelNumber = &user.UserLevel.LevelNumber
	//}

	result, err := _i.Repo.FeedbacksMonthlyStats(clientId, *year)
	if err != nil {
		return nil, err
	}
	return result, nil
}
