package tests

import (
	"context"
	"server/common"
	"server/config"
	"server/modules/user/vo"
	"server/modules/yx/service"
	"testing"
	"time"
)

// TestRedisOperation 测试 Redis 读写
func TestRedisOperation(t *testing.T) {
	ctx := context.Background()
	key := "test:hello"
	value := "world_" + time.Now().Format("2006-01-02 15:04:05")

	// 写 Redis
	err := config.RDB.Set(ctx, key, value, time.Minute).Err()
	if err != nil {
		t.Fatalf("Redis 写入失败: %v", err)
	}
	t.Logf("Redis 写入成功: %s -> %s", key, value)

	// 读 Redis
	val, err := config.RDB.Get(ctx, key).Result()
	if err != nil {
		t.Fatalf("Redis 读取失败: %v", err)
	}
	t.Logf("Redis 读取成功: %s", val)

	if val != value {
		t.Errorf("Redis 值不匹配: 期望 %s, 实际 %s", value, val)
	}
}

// TestSchoolMajorQuery 测试院校专业复杂查询 Mapper
func TestSchoolMajorQuery(t *testing.T) {
	cs := service.NewYxCalculationMajorService()
	userScoreVO := vo.UserScoreVO{
		ProfessionalCategory: "美术与设计类",
		CognitioPolyclinic:   "文科",
		CulturalScore:        500,
		ProfessionalScore:    250,
	}
	start := time.Now()
	items, err := cs.ListByUserQueryType("美术与设计类", "文科", []string{})
	if err != nil {
		t.Fatalf("查询失败: %v", err)
	}

	duration := time.Now().UnixMilli() - start.UnixMilli()
	common.LogInfo("CheckEnrollProbability elapsed: %v", duration)
	t.Logf("查询成功，共找到 %d 条记录", len(items))

	startTime := time.Now()
	cs.CheckEnrollProbability(&items, userScoreVO)
	elapsed := time.Now().UnixMilli() - startTime.UnixMilli()
	common.LogInfo("CheckEnrollProbability elapsed: %v", elapsed)

	// 插入到数据库
	// err = cs.BatchCreateBySchoolMajorDTO(items)
	if err != nil {
		t.Fatalf("插入数据库失败: %v", err)
	}
	t.Logf("成功插入 %d 条记录到数据库", len(items))

	// 打印前几条结果
	// for i, item := range items {
	// 	if i >= 3 {
	// 		break
	// 	}
	// 	jsonBytes, _ := json.Marshal(item)
	// 	t.Logf("记录 #%d: %s", i+1, string(jsonBytes))
	// }
}

// TestIDGenerator 测试 ID 生成器
func TestIDGenerator(t *testing.T) {
	idLong := common.GenerateLongID()
	t.Logf("生成的 Long ID: %d", idLong)

	idStr := common.GenerateStringID()
	t.Logf("生成的 String ID: %s", idStr)

	// idTimestamp := common.GetIDGenerator().GenerateTimestampLongID()
	// t.Logf("生成的 Timestamp Long ID: %d", idTimestamp)
}
