package service

import (
	"fmt"
	"math/rand"
	"time"
	"youngee_b_api/app/vo"
)

type ViewDataService struct{}

func (v ViewDataService) SelectionTalentDataDetail(param *vo.SelectionShowDataParam) (vo.ReSelectionTalentData, error) {
	// 设置随机种子
	rand.Seed(time.Now().UnixNano())

	// 随机生成 AddWindowNum 列表
	var rangeLength int64
	if param.Type == 1 {
		rangeLength = 7
	} else if param.Type == 2 {
		rangeLength = 30
	} else {
		rangeLength = 90
	}
	addWindowNum := make([]int64, rangeLength)
	for i := range addWindowNum {
		addWindowNum[i] = rand.Int63n(1000) // 随机生成0-999的数
	}

	// 随机生成 SalesDistribution 和 FansDistribution 列表
	salesDistribution := []int64{
		rand.Int63n(1000),
		rand.Int63n(500),
		rand.Int63n(100),
		rand.Int63n(50),
		rand.Int63n(10),
		rand.Int63n(5),
	}
	fansDistribution := []int64{
		rand.Int63n(1000),
		rand.Int63n(20),
		rand.Int63n(5),
		rand.Int63n(3),
	}

	// 随机生成 RegionRank 列表
	regions := []string{"广东", "广西", "海南", "湖南", "浙江", "福建", "河南", "江苏", "四川", "江西"}
	regionRank := make([]vo.RankItem, len(regions))
	totalPercent := 0.0
	for i := 0; i < len(regions)-1; i++ {
		percent := rand.Float64() * (1 - totalPercent) / 2 // 确保生成的总和小于 1
		totalPercent += percent
		regionRank[i] = vo.RankItem{
			Region:  regions[i],
			Percent: percent,
		}
	}
	// 最后一个元素的 Percent 设置为 1 - totalPercent，确保总和小于 1
	regionRank[len(regions)-1] = vo.RankItem{
		Region:  regions[len(regions)-1],
		Percent: 1 - totalPercent,
	}

	reSelectionTalentData := vo.ReSelectionTalentData{
		AddWindowNum:      addWindowNum,
		AvgFanNum:         rand.Int63n(10000), // 平均粉丝数
		SalesDistribution: salesDistribution,
		FansDistribution:  fansDistribution,
		MaleNum:           rand.Int63n(500), // 随机生成男性人数
		FemaleNum:         rand.Int63n(500), // 随机生成女性人数
		RegionRank:        regionRank,
	}

	return reSelectionTalentData, nil
}

func (v ViewDataService) SelectionSampleDataDetail(param *vo.SelectionShowDataParam) (vo.ReSelectionSampleData, error) {
	// 设置随机种子
	rand.Seed(time.Now().UnixNano())

	// 随机生成 AddWindowNum 列表
	var rangeLength int64
	if param.Type == 1 {
		rangeLength = 7
	} else if param.Type == 2 {
		rangeLength = 30
	} else {
		rangeLength = 90
	}
	enrollNum := make([]int64, rangeLength)
	for i := range enrollNum {
		enrollNum[i] = rand.Int63n(1000) // 随机生成0-999的数
	}

	// 随机生成 SalesDistribution 和 FansDistribution 列表
	salesDistribution := []int64{
		rand.Int63n(1000),
		rand.Int63n(500),
		rand.Int63n(100),
		rand.Int63n(50),
		rand.Int63n(10),
		rand.Int63n(5),
	}
	fansDistribution := []int64{
		rand.Int63n(1000),
		rand.Int63n(20),
		rand.Int63n(5),
		rand.Int63n(3),
	}

	// 随机生成 RegionRank 列表
	regions := []string{"广东", "广西", "海南", "湖南", "浙江", "福建", "河南", "江苏", "四川", "江西"}
	regionRank := make([]vo.RankItem, len(regions))
	totalPercent := 0.0
	for i := 0; i < len(regions)-1; i++ {
		percent := rand.Float64() * (1 - totalPercent) / 2 // 确保生成的总和小于 1
		totalPercent += percent
		regionRank[i] = vo.RankItem{
			Region:  regions[i],
			Percent: percent,
		}
	}
	// 最后一个元素的 Percent 设置为 1 - totalPercent，确保总和小于 1
	regionRank[len(regions)-1] = vo.RankItem{
		Region:  regions[len(regions)-1],
		Percent: 1 - totalPercent,
	}

	reSelectionTalentData := vo.ReSelectionSampleData{
		EnrollNum:         enrollNum,
		AgreeSampleNum:    rand.Int63n(10000),
		SampledNum:        rand.Int63n(1000),
		AvgFanNum:         rand.Int63n(10000),
		SalesDistribution: salesDistribution,
		FansDistribution:  fansDistribution,
		MaleNum:           rand.Int63n(500), // 随机生成男性人数
		FemaleNum:         rand.Int63n(500), // 随机生成女性人数
		RegionRank:        regionRank,
	}

	return reSelectionTalentData, nil
}

func (v ViewDataService) SelectionDataDetail(param *vo.SelectionShowDataParam) (vo.ReSelectionData, error) {
	// 设置随机种子
	rand.Seed(time.Now().UnixNano())

	// 随机生成 AddWindowNum 列表
	var rangeLength int64
	if param.Type == 1 {
		rangeLength = 7
	} else if param.Type == 2 {
		rangeLength = 30
	} else {
		rangeLength = 90
	}
	payData := make([]float64, rangeLength)
	for i := range payData {
		payData[i] = rand.Float64() * 10000
	}
	estimateData := make([]float64, rangeLength)
	for i := range estimateData {
		estimateData[i] = rand.Float64() * 10000
	}
	payOrderNum := make([]int64, rangeLength)
	for i := range payOrderNum {
		payOrderNum[i] = rand.Int63n(1000)
	}
	saleOrderNum := make([]int64, rangeLength)
	for i := range saleOrderNum {
		saleOrderNum[i] = rand.Int63n(1000)
	}

	// 随机生成 TalentMsgList 数据
	totalTalents := rand.Int63n(100) // 假设总共有100个达人
	talentMsgList := make([]vo.TalentMsg, 0, param.PageSize)
	start := (param.Page - 1) * param.PageSize
	end := start + param.PageSize
	if end > totalTalents {
		end = totalTalents
	}

	for i := start; i < end; i++ {
		talentMsgList = append(talentMsgList, vo.TalentMsg{
			PhotoUrl: fmt.Sprintf("https://example.com/photo/%d.jpg", i),
			Nickname: fmt.Sprintf("达人%d", i),
			Account:  fmt.Sprintf("account_%d", i),
			OrderNum: rand.Int63n(100),
			GMV:      rand.Float64() * 10000,
		})
	}
	resultVO := vo.ResultVO{
		Page:     int(param.Page),
		PageSize: int(param.PageSize),
		Total:    totalTalents,
		Data:     talentMsgList,
	}

	reSelectionData := vo.ReSelectionData{
		PayData:            payData,
		EstimateData:       estimateData,
		ForecastCommission: rand.Float64() * 100000,
		PayOrderNum:        payOrderNum,
		SaleOrderNum:       saleOrderNum,
		AvgCommissionRate:  rand.Float64(),
		TalentMsgList:      resultVO,
	}

	return reSelectionData, nil
}
