package handlers

import (
	"context"
	"net/http"
	"time"

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

	"yh_web/server/config"

	"github.com/gin-gonic/gin"
)

func GetStats(c *gin.Context) {
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	db := config.GetDB(config.DBName)

	users, _ := db.Collection("users").CountDocuments(ctx, bson.M{})
	workspaces, _ := db.Collection("workspaces").CountDocuments(ctx, bson.M{})
	conversations, _ := db.Collection("conversations").CountDocuments(ctx, bson.M{})
	messages, _ := db.Collection("messages").CountDocuments(ctx, bson.M{})
	files, _ := db.Collection("files").CountDocuments(ctx, bson.M{})

	c.JSON(http.StatusOK, gin.H{
		"users":         users,
		"workspaces":    workspaces,
		"conversations": conversations,
		"messages":      messages,
		"files":         files,
	})
}
