package service

import (
	"context"
	"fmt"
	"math/rand"
	"strconv"
	"time"
	"youngee_b_api/db"
	"youngee_b_api/model/gorm_model"
	"youngee_b_api/model/http_model"

	"github.com/issue9/conv"

	log "github.com/sirupsen/logrus"
)

var Enterprise *enterprise

type enterprise struct {
}

func (*enterprise) CreateEnterpriseUser(ctx context.Context, newEnterprise http_model.RegisterRequest) (*http_model.RegisterData, error) {
	user := gorm_model.YounggeeUser{
		Phone:         newEnterprise.Phone,
		User:          "1001",
		Username:      newEnterprise.BusinessName,
		Password:      "1001",
		RealName:      newEnterprise.RealName,
		Role:          "3",
		Email:         newEnterprise.Email,
		LastLoginTime: time.Now().UTC().Local(),
	}
	userId, err := db.CreateUser(ctx, user)
	if err != nil {
		log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
		return nil, err
	} else {
		rand.Seed(time.Now().UnixNano())
		th := strconv.Itoa(time.Now().Hour())
		tm := strconv.Itoa(time.Now().Minute())
		if len(tm) < 2 {
			tm = "0" + tm
		}
		if len(th) < 2 {
			th = "0" + th
		}
		//ShowEnterpriseID := "1" + th + tm + conv.MustString(rand.Intn(10000-1000)+1000
		ShowEnterpriseID := "1" + th + tm + fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(10000))
		enterprise := &gorm_model.Enterprise{
			EnterpriseID:     ShowEnterpriseID,
			Industry:         newEnterprise.Industry,
			BusinessName:     newEnterprise.BusinessName,
			UserID:           *userId,
			Balance:          0,
			FrozenBalance:    0,
			AvailableBalance: 0,
		}
		enterpriseId, err := db.CreateEnterprise(ctx, *enterprise)
		if err != nil {
			log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
			return nil, err
		}
		res := &http_model.RegisterData{
			EnterpriseId: *enterpriseId,
			UserID:       *userId,
		}
		return res, nil
	}
}

func (*enterprise) CreateEnterprise(ctx context.Context, phone string) (*http_model.RegisterData, error) {
	user := gorm_model.YounggeeUser{
		Phone:         phone,
		User:          "1001",
		Username:      phone,
		Password:      "1001",
		RealName:      "",
		Role:          "3",
		Email:         "",
		LastLoginTime: time.Now().UTC().Local(),
	}
	userId, err := db.CreateUser(ctx, user)
	if err != nil {
		log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
		return nil, err
	} else {
		rand.Seed(time.Now().UnixNano())
		th := conv.MustString(time.Now().Hour())
		tm := conv.MustString(time.Now().Minute())
		if len(tm) < 2 {
			tm = "0" + tm
		}
		if len(tm) < 2 {
			th = "0" + th
		}
		ShowEnterpriseID := "1" + th + tm + conv.MustString(rand.Intn(10000-1000)+1000)
		enterprise := &gorm_model.Enterprise{
			EnterpriseID:     ShowEnterpriseID,
			Industry:         1,
			BusinessName:     phone,
			UserID:           *userId,
			Balance:          0,
			FrozenBalance:    0,
			AvailableBalance: 0,
		}
		enterpriseId, err := db.CreateEnterprise(ctx, *enterprise)
		if err != nil {
			log.Infof("[CreateEnterpriseUser] fail,err:%+v", err)
			return nil, err
		}
		res := &http_model.RegisterData{
			EnterpriseId: *enterpriseId,
			UserID:       *userId,
		}
		return res, nil
	}
}
