package service

import (
	"github.com/google/uuid"
	"github.com/rs/zerolog"
	"web-qudo-be/app/database/entity"
	"web-qudo-be/app/module/article_comments/mapper"
	"web-qudo-be/app/module/article_comments/repository"
	"web-qudo-be/app/module/article_comments/request"
	"web-qudo-be/app/module/article_comments/response"
	usersRepository "web-qudo-be/app/module/users/repository"
	"web-qudo-be/utils/paginator"

	utilSvc "web-qudo-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(clientId *uuid.UUID, req request.ArticleCommentsQueryRequest) (articleComments []*response.ArticleCommentsResponse, paging paginator.Pagination, err error)
	Show(clientId *uuid.UUID, id uint) (articleComments *response.ArticleCommentsResponse, err error)
	Save(clientId *uuid.UUID, req request.ArticleCommentsCreateRequest, authToken string) (articleComments *entity.ArticleComments, err error)
	Update(clientId *uuid.UUID, id uint, req request.ArticleCommentsUpdateRequest) (err error)
	Delete(clientId *uuid.UUID, id uint) error
	Approval(clientId *uuid.UUID, 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(clientId *uuid.UUID, req request.ArticleCommentsQueryRequest) (articleCommentss []*response.ArticleCommentsResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(clientId, req)
	if err != nil {
		return
	}

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

	return
}

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

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

func (_i *articleCommentsService) Save(clientId *uuid.UUID, 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(clientId, newReq)
}

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

func (_i *articleCommentsService) 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 *articleCommentsService) Approval(clientId *uuid.UUID, 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(clientId, id, newReq)
}
