package mdb

import (
	"context"
	"kurumibot/database"
	"kurumibot/laniakea"
	"time"

	"go.mongodb.org/mongo-driver/v2/bson"
)

type CodeEntry struct {
	Code        string    `bson:"code"`
	Money       uint64    `bson:"money"`
	Exp         uint64    `bson:"exp"`
	AutoId      uint16    `bson:"autoId"`
	MaidId      uint16    `bson:"maidId"`
	MinerId     uint16    `bson:"minerId"`
	BusinessId  uint16    `bson:"businessId"`
	ValidUntil  time.Time `bson:"validUntil"`
	UsageCount  int       `bson:"usageCount"`
	TotalUsages uint      `bson:"totalUsages"`
}

func FindCode(db *laniakea.DatabaseContext, code string) (*CodeEntry, error) {
	ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
	defer cancel()
	collection := database.GetMongoCollection(db, "codes")
	res := collection.FindOne(ctx, bson.M{"code": code})
	entry := new(CodeEntry)
	err := res.Decode(entry)
	return entry, err
}
