package models

import (
	"server/conf"
	mongodbmgr "server/db/mongodb"

	// redismgr "server/db/redis"
	"time"

	"gopkg.in/mgo.v2/bson"
)

// 定义用户集合操作
var userCollection = mongodbmgr.NewCollection(conf.MongoDBName, "users")

// 用户模型
type User struct {
	UserID    int64     `bson:"user_id"`
	Name      string    `bson:"name"`
	Password  string    `bson:"password"`
	LastLogin time.Time `bson:"last_login"`
}

// func AddGameToList(userId string) {
// 	// 推入 Redis 列表
// 	err := redismgr.ListPush("user:"+userId+":games", "game1")
// 	if err != nil {
// 		log.Println("Error pushing to Redis:", err)
// 	}
// }

// 获取最近三天有登录的用户，支持分页
func GetThreeDayUsersWithPagination(skip, limit int) ([]User, error) {
	var users []User
	threeDaysAgo := time.Now().AddDate(0, 0, -3)
	err := userCollection.FindMany(
		bson.M{"last_login": bson.M{"$gte": threeDaysAgo}},
		skip,
		limit,
		&users,
	)
	if err != nil {
		return nil, err
	}
	return users, nil
}

// 根据用户ID获取用户信息
func GetUserByID(userID int64) (*User, error) {
	var user User
	err := userCollection.FindOne(bson.M{"user_id": userID}, &user)
	if err != nil {
		return nil, err
	}
	return &user, nil
}
