package main

import (
	"context"
	"fmt"
	"math/rand"
	"strconv"
	"time"

	mhayaTime "github.com/mhaya/extend/time"
	"github.com/mhaya/game/game_cluster/internal/constant"
	"github.com/mhaya/game/game_cluster/internal/mdb/models"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.mongodb.org/mongo-driver/mongo/readpref"
)

type NameData struct {
	User_name string
	Nick_name string
	Balance   int
}

func main() {
	uri := "mongodb://127.0.0.1:27017"
	dbName := "db_mhaya"

	o := options.Client().ApplyURI(uri)
	if err := o.Validate(); err != nil {
		fmt.Println(err)
		return
	}

	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	client, err := mongo.Connect(ctx, o)
	if err != nil {
		fmt.Println(err)
		return
	}

	err = client.Ping(context.Background(), readpref.Primary())
	if err != nil {
		fmt.Println(err)
		return
	}

	db := client.Database(dbName)

	// 写入提现假数据
	addresss := make([]string, 0, 8)
	for i := 0; i < 5; i++ {
		addresss = append(addresss, "address"+strconv.Itoa(i+1))
	}

	nameDatas := make([]*NameData, 0, 8)
	for i := 0; i < 5; i++ {
		nameDatas = append(nameDatas, &NameData{
			User_name: "test" + strconv.Itoa(i),
			Nick_name: "test" + strconv.Itoa(i),
			Balance:   1000000,
		})
	}

	for i := 0; i < 100; i++ {
		index := rand.Intn(len(nameDatas))
		nameDataObj := nameDatas[index]

		amount := rand.Int63n(500)
		nameDataObj.Balance -= int(amount)

		_, err = db.Collection(constant.CNameCashOutRecord).InsertOne(context.Background(), &models.CashOutRecord{
			UserName: nameDataObj.User_name,
			NickName: nameDataObj.Nick_name,
			Status: func() int {
				if rand.Int63n(3) == 1 {
					return 1
				}

				if rand.Int63n(3) == 2 {
					return 2
				}

				return 0
			}(), // 0:未审核 1:审核通过 2:审核失败
			Type:        1,
			AfterAmount: nameDataObj.Balance,
			Amount:      int(amount),
			Address: func() string {
				index := rand.Intn(len(addresss))
				return addresss[index]
			}(),
			Withdrawal: func() int {
				if rand.Int63n(5) == 1 {
					return 1
				}

				if rand.Int63n(5) == 2 {
					return 2
				}

				if rand.Int63n(5) == 3 {
					return 3
				}

				if rand.Int63n(5) == 4 {
					return 4
				}

				return 0
			}(), // 提现 0 :未体现 1:提现成功 2:提现中 3：提现失败 4：拒绝提现
			CreateAt: mhayaTime.Now().Unix(),
		})
		if err != nil {
			fmt.Println(err)
			return
		}
	}
}
