package adminrefertimeservice

import (
	"context"

	"git.gocasts.ir/ebhomengo/niki/entity"
	param "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (s Service) GetAll(ctx context.Context, req param.GetAllReferTimeRequest) (param.GetAllReferTimeResponse, error) {
	const op = "adminrefertimeservice.GetAllReferTime"
	status := entity.ReferTimeActiveStatus

	if fieldErrors, vErr := s.vld.ValidateGetAll(req); vErr != nil {
		return param.GetAllReferTimeResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr)
	}

	if str, ok := req.Filter["status"].(string); ok {
		status = entity.ReferTimeStatus(str)
	}

	referTimes, err := s.repo.GetAll(ctx, status)
	if err != nil {
		return param.GetAllReferTimeResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
	}

	return param.GetAllReferTimeResponse{Data: referTimes}, nil
}
