package service

import (
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/app/module/article_comments/mapper"
	"campaign-pool-be/app/module/article_comments/repository"
	"campaign-pool-be/app/module/article_comments/request"
	"campaign-pool-be/app/module/article_comments/response"
	usersRepository "campaign-pool-be/app/module/users/repository"
	"campaign-pool-be/utils/paginator"

	"github.com/rs/zerolog"

	utilSvc "campaign-pool-be/utils/service"
)

// ArticleCommentsService
type articleCommentsService struct {
	Repo      repository.ArticleCommentsRepository
	UsersRepo usersRepository.UsersRepository
	Log       zerolog.Logger
}

// ArticleCommentsService define interface of IArticleCommentsService
type ArticleCommentsService interface {
	All(req request.ArticleCommentsQueryRequest) (articleComments []*response.ArticleCommentsResponse, paging paginator.Pagination, err error)
	Show(id uint) (articleComments *response.ArticleCommentsResponse, err error)
	Save(req request.ArticleCommentsCreateRequest, authToken string) (articleComments *entity.ArticleComments, err error)
	Update(id uint, req request.ArticleCommentsUpdateRequest) (err error)
	Delete(id uint) error
	Approval(id uint, req request.ArticleCommentsApprovalRequest) (err error)
}

// NewArticleCommentsService init ArticleCommentsService
func NewArticleCommentsService(repo repository.ArticleCommentsRepository, log zerolog.Logger, usersRepo usersRepository.UsersRepository) ArticleCommentsService {

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

// All implement interface of ArticleCommentsService
func (_i *articleCommentsService) All(req request.ArticleCommentsQueryRequest) (articleCommentss []*response.ArticleCommentsResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(req)
	if err != nil {
		return
	}

	for _, result := range results {
		articleCommentss = append(articleCommentss, mapper.ArticleCommentsResponseMapper(result, _i.UsersRepo))
	}

	return
}

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

	return mapper.ArticleCommentsResponseMapper(result, _i.UsersRepo), nil
}

func (_i *articleCommentsService) Save(req request.ArticleCommentsCreateRequest, authToken string) (articleComments *entity.ArticleComments, err error) {
	_i.Log.Info().Interface("data", req).Msg("")

	newReq := req.ToEntity()

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

	return _i.Repo.Create(newReq)
}

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

func (_i *articleCommentsService) 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 *articleCommentsService) Approval(id uint, req request.ArticleCommentsApprovalRequest) (err error) {
	_i.Log.Info().Interface("data", req).Msg("")
	newReq := req.ToEntity()

	if newReq.StatusId == 1 || newReq.StatusId == 2 {
		newReq.IsPublic = true
	} else {
		newReq.IsPublic = false
	}

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