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) {
	userInfo := gorm_model.YounggeeUser{
		Phone:         phone,
		User:          "1001",
		Username:      phone,
		Password:      "1001",
		RealName:      "",
		Role:          "3",
		Email:         "",
		LastLoginTime: time.Now().UTC().Local(),
	}
	userId, createUserErr := db.CreateUser(ctx, userInfo)
	if createUserErr != nil {
		log.Infof("[CreateEnterpriseUser] fail,err:%+v", createUserErr)
		return nil, createUserErr
	} 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)
		enterpriseInfo := &gorm_model.Enterprise{
			EnterpriseID:     ShowEnterpriseID,
			Industry:         1,
			BusinessName:     phone,
			UserID:           *userId,
			Balance:          0,
			FrozenBalance:    0,
			AvailableBalance: 0,
			Phone:            phone,
		}
		enterpriseId, createEnterpriseErr := db.CreateEnterprise(ctx, *enterpriseInfo)
		if createEnterpriseErr != nil {
			log.Infof("[CreateEnterpriseUser] fail,err:%+v", createEnterpriseErr)
			return nil, createEnterpriseErr
		}
		res := &http_model.RegisterData{
			EnterpriseId: *enterpriseId,
			UserID:       *userId,
		}
		return res, nil
	}
}

// GetEnterpriseAccountInfo 查询商家账号信息
func (*enterprise) GetEnterpriseAccountInfo(ctx context.Context, req *http_model.GetAccountInfoRequest) (*http_model.GetAccountInfoData, error) {

	var enterpriseUserInfo *http_model.GetAccountInfoData
	enterpriseUserInfo = &http_model.GetAccountInfoData{}

	if req.SubAccountId == 0 {
		// 1. 商家主账号
		enterpriseInfo, enterpriseInfoErr := db.GetEnterpriseByEnterpriseID(ctx, req.EnterpriseId)
		if enterpriseInfoErr != nil {
			log.Infof("[GetSupplierAccountInfo] fail,err:%+v", enterpriseInfoErr)
			return nil, enterpriseInfoErr
		}
		if enterpriseInfo != nil {
			enterpriseUserInfo.EnterpriseName = enterpriseInfo.EnterpriseName
			enterpriseUserInfo.Type = 1
			enterpriseUserInfo.Avatar = enterpriseInfo.Avatar
			enterpriseUserInfo.Phone = enterpriseInfo.Phone
		}
	} else {
		// 2. 商家子账号
		subAccountInfo, subAccountInfoErr := db.FindSubAccountById(ctx, req.SubAccountId)
		if subAccountInfoErr != nil {
			log.Infof("[GetSupplierAccountInfo] fail,err:%+v", subAccountInfoErr)
			return nil, subAccountInfoErr
		}
		if subAccountInfo != nil {
			enterpriseUserInfo.SubAccountName = subAccountInfo.SubAccountName
			enterpriseUserInfo.Type = 2
			enterpriseUserInfo.Avatar = subAccountInfo.Avatar
			enterpriseUserInfo.Phone = subAccountInfo.PhoneNumber
			jobInfo, jobInfoErr := db.FindJobByJobId(ctx, subAccountInfo.JobId)
			if jobInfoErr != nil {
				log.Infof("[GetSupplierAccountInfo] fail,err:%+v", jobInfoErr)
				return nil, subAccountInfoErr
			}
			if jobInfo != nil {
				enterpriseUserInfo.JobName = jobInfo.JobName
			}
		}
	}
	return enterpriseUserInfo, nil
}

// GetEnterpriseReviewInfo 查询商家认证信息
func (*enterprise) GetEnterpriseReviewInfo(ctx context.Context, req *http_model.GetReviewInfoRequest) (*http_model.GetReviewInfoData, error) {

	var enterpriseUserInfo *http_model.GetReviewInfoData
	enterpriseUserInfo = &http_model.GetReviewInfoData{}

	enterpriseInfo, enterpriseInfoErr := db.GetEnterpriseByEnterpriseID(ctx, req.EnterpriseId)
	if enterpriseInfoErr != nil {
		log.Infof("[GetEnterpriseByEnterpriseID] fail,err:%+v", enterpriseInfoErr)
		return nil, enterpriseInfoErr
	}
	if enterpriseInfo != nil {
		enterpriseUserInfo.ReviewType = 1
		if enterpriseInfo.AuthStatus == 1 {
			enterpriseUserInfo.ReviewStatus = 2
		} else {
			enterpriseUserInfo.ReviewStatus = 1
		}
		enterpriseUserInfo.BusinessLicense = enterpriseInfo.BusinessLicense
		enterpriseUserInfo.USCI = enterpriseInfo.Usci
		enterpriseUserInfo.CompanyName = enterpriseInfo.BusinessName
	}
	return enterpriseUserInfo, nil
}

// GetEnterpriseContactInfo 查询商家联系方式
func (*enterprise) GetEnterpriseContactInfo(ctx context.Context, req *http_model.GetContactInfoRequest) (*http_model.GetContactInfoData, error) {

	var contactInfo *http_model.GetContactInfoData
	contactInfo = &http_model.GetContactInfoData{}
	// 1. 商家主账号
	if req.SubAccountId == 0 {
		enterpriseInfo, enterpriseInfoErr := db.GetEnterpriseByEnterpriseID(ctx, req.EnterpriseId)
		if enterpriseInfoErr != nil {
			log.Infof("[enterpriseInfoErr] fail,err:%+v", enterpriseInfoErr)
			return nil, enterpriseInfoErr
		}
		if enterpriseInfo != nil {
			contactInfo.ContactPhone = enterpriseInfo.ContactPhone
			contactInfo.WechatQRCode = enterpriseInfo.WechatQrCode
			contactInfo.WechatNumber = enterpriseInfo.WechatNumber
		}
	} else {
		// 2. 商家子账号
		subAccountInfo, subAccountInfoErr := db.FindSubAccountById(ctx, req.SubAccountId)
		if subAccountInfoErr != nil {
			log.Infof("[GetSupplierContactInfo] fail,err:%+v", subAccountInfoErr)
			return nil, subAccountInfoErr
		}
		if subAccountInfo != nil {
			contactInfo.ContactPhone = subAccountInfo.ContactPhone
			contactInfo.WechatQRCode = subAccountInfo.WechatQRCode
			contactInfo.WechatNumber = subAccountInfo.WechatNumber
		}
	}
	return contactInfo, nil
}
