package adminseederservice

import (
	"context"

	"git.gocasts.ir/ebhomengo/niki/entity"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
	"github.com/brianvoe/gofakeit/v7"
)

func (s Service) AddKindBox(ctx context.Context, agentId uint, status entity.KindBoxStatus) (entity.KindBox, error) {
	const op = "adminseederservice.AddKindBox"

	kindBoxReq, cErr := s.AddKindBoxReq(ctx, entity.KindBoxReqAcceptedStatus)
	if cErr != nil {
		return entity.KindBox{}, richerror.New(op).WithErr(cErr).WithKind(richerror.KindUnexpected)
	}

	address, aErr := s.getAddress(ctx, kindBoxReq.BenefactorID)
	if aErr != nil {
		return entity.KindBox{}, richerror.New(op).WithErr(aErr).WithKind(richerror.KindUnexpected)
	}

	kindBox, kErr := s.kindBoxRepo.AddKindBox(ctx, entity.KindBox{
		KindBoxReqID:       kindBoxReq.ID,
		BenefactorID:       kindBoxReq.BenefactorID,
		KindBoxType:        new(entity.KindBoxType).GetRandom(),
		Status:             status,
		DeliverReferTimeID: uint(gofakeit.IntRange(1, 7)),
		DeliverReferDate:   gofakeit.Date(),
		DeliverAddressID:   address.ID,
		SenderAgentID:      agentId,
		DeliveredAt:        gofakeit.Date(),
	})
	if kErr != nil {
		return entity.KindBox{}, richerror.New(op).WithErr(kErr).WithKind(richerror.KindUnexpected)
	}

	return kindBox, nil
}
