package adminbenefactorservice

import (
	"context"

	paginationparam "git.gocasts.ir/ebhomengo/niki/param"
	param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
	response "git.gocasts.ir/ebhomengo/niki/pkg/response_builder"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (s Service) GetAllBenefactor(ctx context.Context, req param.BenefactorGetAllRequest) (param.BenefactorGetAllResponse, error) {
	const op = "adminbenefactorservice.GetAllBenefactor"

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

	benefactors, total, err := s.repo.GetAllBenefactor(ctx, req.Filter, req.Pagination, req.Sort, req.Search.GetSearch())
	if err != nil {
		return param.BenefactorGetAllResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
	}

	resp := make([]param.Data, 0)
	for _, benefactor := range benefactors {
		resp = append(resp, param.Data{
			ID:          benefactor.ID,
			FirstName:   benefactor.FirstName,
			LastName:    benefactor.LastName,
			PhoneNumber: benefactor.PhoneNumber,
			Description: benefactor.Description,
			Email:       benefactor.Email,
			Gender:      benefactor.Gender,
			BirthDate:   response.GetNullDate(benefactor.BirthDate),
			Status:      benefactor.Status,
		})
	}

	return param.BenefactorGetAllResponse{
		Data: resp,
		Pagination: paginationparam.PaginationResponse{
			PageSize:   req.Pagination.PageSize,
			PageNumber: req.Pagination.PageNumber,
			Total:      total,
		},
	}, nil
}
