package adminkindboxreqservice

import (
	"context"

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

// @see
//	When confirming a request, should the senderID field, which represents the person sending the kind-box to the beneficiary,
//	be filled at the same time? Or is it acceptable to confirm the request first and fill in the senderID field later?
//

func (s Service) Accept(ctx context.Context, req param.KindBoxReqAcceptRequest) (param.KindBoxReqAcceptResponse, error) {
	const op = "adminkindboxreqservice.Accept"
	if fieldErrors, vErr := s.vld.ValidateAcceptRequest(ctx, req); vErr != nil {
		return param.KindBoxReqAcceptResponse{FieldErrors: fieldErrors}, richerror.New(op).WithErr(vErr)
	}
	err := s.repo.AcceptKindBoxReq(ctx, req.ID, req.CountAccepted)
	if err != nil {
		return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(err)
	}

	kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID)
	if gErr != nil {
		return param.KindBoxReqAcceptResponse{}, richerror.New(op).WithErr(gErr)
	}

	return param.KindBoxReqAcceptResponse{Data: param.Data{
		ID:                 kindBoxReq.ID,
		BenefactorID:       kindBoxReq.BenefactorID,
		KindBoxType:        kindBoxReq.KindBoxType,
		CountRequested:     kindBoxReq.CountRequested,
		CountAccepted:      kindBoxReq.CountAccepted,
		Description:        kindBoxReq.Description,
		Status:             kindBoxReq.Status,
		DeliverReferTimeID: kindBoxReq.DeliverReferTimeID,
		DeliverReferDate:   kindBoxReq.DeliverReferDate,
		DeliverAddressID:   kindBoxReq.DeliverAddressID,
		SenderAgentID:      kindBoxReq.SenderAgentID,
		DeliveredAt:        response.GetNullDate(kindBoxReq.DeliveredAt),
	}}, nil
}
