package models

import (
	"context"
	"testing"

	"quyun/app/service/testx"
	"quyun/database"
	"quyun/database/fields"
	"quyun/database/schemas/public/model"
	"quyun/database/schemas/public/table"

	"github.com/samber/lo"
	. "github.com/smartystreets/goconvey/convey"
	"go.ipao.vip/atom/contracts"

	// . "github.com/go-jet/jet/v2/postgres"
	gonanoid "github.com/matoous/go-nanoid/v2"
	"github.com/stretchr/testify/suite"
	"go.uber.org/dig"
)

type UsersInjectParams struct {
	dig.In
	Initials []contracts.Initial `group:"initials"`
}

type UsersTestSuite struct {
	suite.Suite

	UsersInjectParams
}

func Test_Users(t *testing.T) {
	providers := testx.Default().With(Provide)
	testx.Serve(providers, t, func(params UsersInjectParams) {
		suite.Run(t, &UsersTestSuite{
			UsersInjectParams: params,
		})
	})
}

func (s *UsersTestSuite) Test_BatchInsert() {
	Convey("Test_BatchInsert", s.T(), func() {
		database.Truncate(context.Background(), db, table.Users.TableName())

		count := 100
		for i := 0; i < count; i++ {
			// generate openid use nanoid
			openID, err := gonanoid.New(10)
			So(err, ShouldBeNil)

			user := &model.Users{
				Status:   fields.UserStatusOk,
				OpenID:   openID,
				Username: "Username_" + openID,
				Avatar:   lo.ToPtr("Avatar_" + openID),
			}
			m, err := Users.Create(context.Background(), user)
			So(err, ShouldBeNil)
			So(m, ShouldNotBeNil)
		}
	})
}
