package mysqlkindboxreq_test

import (
	"context"
	"testing"

	"git.gocasts.ir/ebhomengo/niki/entity"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
	mysqlkindboxreq "git.gocasts.ir/ebhomengo/niki/repository/mysql/kind_box_req"
	testutils "git.gocasts.ir/ebhomengo/niki/test"
	"git.gocasts.ir/ebhomengo/niki/test/seed"
	"github.com/brianvoe/gofakeit/v6"
	"github.com/stretchr/testify/assert"
)

func TestAddKindBoxReq(t *testing.T) {
	mysqlRepo := testutils.Setup(t)
	mysqlKindboxReq := mysqlkindboxreq.New(mysqlRepo)

	benefactor, cleanupBenefactor := seed.CreateBenefactor(t, mysqlRepo)
	defer cleanupBenefactor()
	address, cleanupAddress := seed.CreateAddress(t, mysqlRepo, benefactor.ID)
	defer cleanupAddress()

	testCases := []struct {
		name        string
		repoErr     bool
		expectedErr error
		kindBoxReq  entity.KindBoxReq
	}{
		{
			name:        "repo fails",
			repoErr:     true,
			expectedErr: richerror.New("mysqlkindboxreq.AddKindBoxReq").WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected),
			kindBoxReq: entity.KindBoxReq{
				KindBoxType:    entity.KindBoxStandUp,
				AddressID:      address.ID,
				CountRequested: gofakeit.UintRange(1, 100),
				ReferDate:      gofakeit.Date(),
				Status:         entity.KindBoxReqPendingStatus,
			},
		},
		{
			name: "ordinary",
			kindBoxReq: entity.KindBoxReq{
				BenefactorID:   benefactor.ID,
				KindBoxType:    entity.KindBoxStandUp,
				AddressID:      address.ID,
				CountRequested: gofakeit.UintRange(1, 100),
				ReferDate:      gofakeit.Date(),
				Status:         entity.KindBoxReqPendingStatus,
			},
		},
	}

	for _, tc := range testCases {
		t.Run(tc.name, func(t *testing.T) {
			ctx := context.Background()
			kindBoxReq, err := mysqlKindboxReq.AddKindBoxReq(ctx, tc.kindBoxReq)

			if tc.expectedErr != nil {
				assert.Equal(t, tc.expectedErr.Error(), err.Error())
				assert.Empty(t, kindBoxReq)
				return
			}
			assert.NoError(t, err)
			assert.NotEmpty(t, kindBoxReq)
			seed.DeleteBenefactor(t, mysqlRepo, kindBoxReq.ID)
		})
	}
}
