package service

import (
	"github.com/rs/zerolog"

	"web-qudo-be/app/module/popup_news_contents/mapper"
	"web-qudo-be/app/module/popup_news_contents/repository"
	"web-qudo-be/app/module/popup_news_contents/request"
	"web-qudo-be/app/module/popup_news_contents/response"
	"web-qudo-be/utils/paginator"
)

// service struct
type popupNewsContentsService struct {
	Repo repository.PopupNewsContentsRepository
	Log  zerolog.Logger
}

// interface
type PopupNewsContentsService interface {
	All(req request.PopupNewsContentsQueryRequest) (data []*response.PopupNewsContentsResponse, paging paginator.Pagination, err error)
	Show(id uint) (*response.PopupNewsContentsResponse, error)
	Save(req request.PopupNewsContentsCreateRequest) error
	Update(id uint, req request.PopupNewsContentsUpdateRequest) error
	Delete(id uint) error
}

// constructor
func NewPopupNewsContentsService(repo repository.PopupNewsContentsRepository, log zerolog.Logger) PopupNewsContentsService {
	return &popupNewsContentsService{
		Repo: repo,
		Log:  log,
	}
}

// All
func (_i *popupNewsContentsService) All(req request.PopupNewsContentsQueryRequest) (datas []*response.PopupNewsContentsResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(req)
	if err != nil {
		return
	}

	for _, result := range results {
		datas = append(datas, mapper.PopupNewsContentsResponseMapper(result))
	}

	return
}

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

	return mapper.PopupNewsContentsResponseMapper(result), nil
}

// Save
func (_i *popupNewsContentsService) Save(req request.PopupNewsContentsCreateRequest) error {
	_i.Log.Info().Interface("data", req).Msg("create popup news content")

	return _i.Repo.Create(req.ToEntity())
}

// Update
func (_i *popupNewsContentsService) Update(id uint, req request.PopupNewsContentsUpdateRequest) error {
	_i.Log.Info().Interface("data", req).Msg("update popup news content")

	return _i.Repo.Update(id, req.ToEntity())
}

// Delete
func (_i *popupNewsContentsService) Delete(id uint) error {
	return _i.Repo.Delete(id)
}