package service

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

	"github.com/rs/zerolog"
)

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

// CustomStaticPagesService define interface of ICustomStaticPagesService
type CustomStaticPagesService interface {
	All(req request.CustomStaticPagesQueryRequest) (customStaticPages []*response.CustomStaticPagesResponse, paging paginator.Pagination, err error)
	Show(id uint) (customStaticPages *response.CustomStaticPagesResponse, err error)
	ShowBySlug(slug string) (customStaticPages *response.CustomStaticPagesResponse, err error)
	Save(req request.CustomStaticPagesCreateRequest, authToken string) (customStaticPages *entity.CustomStaticPages, err error)
	Update(id uint, req request.CustomStaticPagesUpdateRequest) (err error)
	Delete(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(req request.CustomStaticPagesQueryRequest) (customStaticPagess []*response.CustomStaticPagesResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(req)
	if err != nil {
		return
	}

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

	return
}

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

	return mapper.CustomStaticPagesResponseMapper(result), nil
}

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

	return mapper.CustomStaticPagesResponseMapper(result), nil
}

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

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

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

	result.IsActive = false
	return _i.Repo.Update(id, result)
}
