package service

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

// CustomStaticPagesService
type customStaticPagesService struct {
	Repo      repository.CustomStaticPagesRepository
	UsersRepo usersRepository.UsersRepository
	Log       zerolog.Logger
}

// CustomStaticPagesService define interface of ICustomStaticPagesService
type CustomStaticPagesService interface {
	All(clientId *uuid.UUID, req request.CustomStaticPagesQueryRequest) (customStaticPages []*response.CustomStaticPagesResponse, paging paginator.Pagination, err error)
	Show(clientId *uuid.UUID, id uint) (customStaticPages *response.CustomStaticPagesResponse, err error)
	ShowBySlug(clientId *uuid.UUID, slug string) (customStaticPages *response.CustomStaticPagesResponse, err error)
	Save(clientId *uuid.UUID, req request.CustomStaticPagesCreateRequest, authToken string) (customStaticPages *entity.CustomStaticPages, err error)
	Update(clientId *uuid.UUID, id uint, req request.CustomStaticPagesUpdateRequest) (err error)
	Delete(clientId *uuid.UUID, id uint) error
}

// NewCustomStaticPagesService init CustomStaticPagesService
func NewCustomStaticPagesService(repo repository.CustomStaticPagesRepository, log zerolog.Logger, usersRepo usersRepository.UsersRepository) CustomStaticPagesService {

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

// All implement interface of CustomStaticPagesService
func (_i *customStaticPagesService) All(clientId *uuid.UUID, req request.CustomStaticPagesQueryRequest) (customStaticPagess []*response.CustomStaticPagesResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(clientId, req)
	if err != nil {
		return
	}

	for _, result := range results {
		customStaticPagess = append(customStaticPagess, mapper.CustomStaticPagesResponseMapper(result))
	}

	return
}

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

	return mapper.CustomStaticPagesResponseMapper(result), nil
}

func (_i *customStaticPagesService) ShowBySlug(clientId *uuid.UUID, slug string) (customStaticPages *response.CustomStaticPagesResponse, err error) {
	result, err := _i.Repo.FindOneBySlug(clientId, slug)
	if err != nil {
		return nil, err
	}

	return mapper.CustomStaticPagesResponseMapper(result), nil
}

func (_i *customStaticPagesService) Save(clientId *uuid.UUID, req request.CustomStaticPagesCreateRequest, authToken string) (customStaticPages *entity.CustomStaticPages, err error) {
	_i.Log.Info().Interface("data", req).Msg("")

	newReq := req.ToEntity()

	//createdBy := utilSvc.GetUserInfo(_i.Log, _i.UsersRepo, authToken)
	//newReq.CreatedById = &createdBy.ID

	return _i.Repo.Create(clientId, newReq)
}

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

func (_i *customStaticPagesService) 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)
}
