package models

import (
	"context"
	"fmt"
	"math/rand"
	"testing"

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

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

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

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

type PostsTestSuite struct {
	suite.Suite

	PostsInjectParams
}

func Test_Posts(t *testing.T) {
	providers := testx.Default().With(Provide)
	testx.Serve(providers, t, func(params PostsInjectParams) {
		suite.Run(t, &PostsTestSuite{
			PostsInjectParams: params,
		})
	})
}

func (s *PostsTestSuite) Test_BatchInsert() {
	Convey("Test_Demo", s.T(), func() {
		database.Truncate(context.Background(), db, table.Posts.TableName())

		count := 100
		for i := 0; i < count; i++ {
			model := model.Posts{
				Status:      fields.PostStatusPublished,
				Title:       fmt.Sprintf("test-title-%d", i),
				Description: fmt.Sprintf("test-description-%d", i),
				Content:     fmt.Sprintf("test-content-%d", i),
				Price:       rand.Int63n(10000),
				Discount:    int16(rand.Intn(100)),
				Views:       rand.Int63n(10000),
				Likes:       rand.Int63n(10000),
				Tags:        fields.ToJson([]string{"tag1", "tag2", "tag3"}),
				Assets: fields.ToJson([]fields.MediaAsset{
					{
						Type:  "video/mp4",
						Media: rand.Int63n(10000),
					},
					{
						Type:  "video/mp4",
						Media: rand.Int63n(10000),
					},
				}),
			}

			if err := Posts.Create(context.Background(), &model); err != nil {
				s.T().Fatal(err)
			}
		}
	})
}

func (s *PostsTestSuite) Test_BatchInsertUserPosts() {
	Convey("Test_Demo", s.T(), func() {
		database.Truncate(context.Background(), db, table.UserPosts.TableName())

		count := 10
		for i := 0; i < count; i++ {
			if err := Users.Own(context.Background(), 1, int64(i)+1); err != nil {
				s.T().Fatal(err)
			}
		}
	})
}
