package adminbenefactorservice

import (
	"context"

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

func (s Service) Update(ctx context.Context, req param.BenefactorUpdateRequest) (param.BenefactorUpdateResponse, error) {
	const op = "adminbenefactorservice.Update"

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

	benefactor, err := s.repo.GetByID(ctx, req.ID)
	if err != nil {
		return param.BenefactorUpdateResponse{}, richerror.New(op).WithErr(err)
	}

	benefactor.FirstName = req.FirstName
	benefactor.LastName = req.LastName
	benefactor.BirthDate = req.BirthDate.Time

	if uErr := s.repo.UpdateGeneralBenefactor(ctx, benefactor); uErr != nil {
		return param.BenefactorUpdateResponse{}, richerror.New(op).WithErr(uErr)
	}

	return param.BenefactorUpdateResponse{}, nil
}
