package service

import (
	"github.com/rs/zerolog"
	"go-humas-be/app/module/magazine_files/mapper"
	"go-humas-be/app/module/magazine_files/repository"
	"go-humas-be/app/module/magazine_files/request"
	"go-humas-be/app/module/magazine_files/response"
	"go-humas-be/utils/paginator"
)

// MagazineFilesService
type magazineFilesService struct {
	Repo repository.MagazineFilesRepository
	Log  zerolog.Logger
}

// MagazineFilesService define interface of IMagazineFilesService
type MagazineFilesService interface {
	All(req request.MagazineFilesQueryRequest) (magazineFiles []*response.MagazineFilesResponse, paging paginator.Pagination, err error)
	Show(id uint) (magazineFiles *response.MagazineFilesResponse, err error)
	Save(req request.MagazineFilesCreateRequest) (err error)
	Update(id uint, req request.MagazineFilesUpdateRequest) (err error)
	Delete(id uint) error
}

// NewMagazineFilesService init MagazineFilesService
func NewMagazineFilesService(repo repository.MagazineFilesRepository, log zerolog.Logger) MagazineFilesService {

	return &magazineFilesService{
		Repo: repo,
		Log:  log,
	}
}

// All implement interface of MagazineFilesService
func (_i *magazineFilesService) All(req request.MagazineFilesQueryRequest) (magazineFiless []*response.MagazineFilesResponse, paging paginator.Pagination, err error) {
	results, paging, err := _i.Repo.GetAll(req)
	if err != nil {
		return
	}

	for _, result := range results {
		magazineFiless = append(magazineFiless, mapper.MagazineFilesResponseMapper(result))
	}

	return
}

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

	return mapper.MagazineFilesResponseMapper(result), nil
}

func (_i *magazineFilesService) Save(req request.MagazineFilesCreateRequest) (err error) {
	_i.Log.Info().Interface("data", req).Msg("")

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

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

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