package service

import (
	"jaecoo-be/app/database/entity"
	"jaecoo-be/app/module/feedbacks/mapper"
	"jaecoo-be/app/module/feedbacks/repository"
	"jaecoo-be/app/module/feedbacks/request"
	"jaecoo-be/app/module/feedbacks/response"
	usersRepository "jaecoo-be/app/module/users/repository"
	"jaecoo-be/utils/paginator"

	"github.com/rs/zerolog"
)

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

// FeedbacksService define interface of IFeedbacksService
type FeedbacksService interface {
	All(req request.FeedbacksQueryRequest) (feedbacks []*response.FeedbacksResponse, paging paginator.Pagination, err error)
	Show(id uint) (feedbacks *response.FeedbacksResponse, err error)
	Save(req request.FeedbacksCreateRequest, authToken string) (feedbacks *entity.Feedbacks, err error)
	Update(id uint, req request.FeedbacksUpdateRequest) (err error)
	Delete(id uint) error
	FeedbackMonthlyStats(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(req request.FeedbacksQueryRequest) (feedbackss []*response.FeedbacksResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(req)
	if err != nil {
		return
	}

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

	return
}

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

	return mapper.FeedbacksResponseMapper(result), nil
}

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

	entity := req.ToEntity()

	return _i.Repo.Create(entity)
}

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

	entity := req.ToEntity()

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

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

func (_i *feedbacksService) FeedbackMonthlyStats(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(*year)
	if err != nil {
		return nil, err
	}
	return result, nil
}
