package handler

import (
	"runtime"
	"time"

	"gitee.ltd/lxh/wechat-robot/internal/model"
	"github.com/gofiber/fiber/v2"
)

// HealthCheck 提供容器健康检查接口
func HealthCheck(c *fiber.Ctx) error {
	// 检查数据库连接
	var count int64
	dbErr := model.GetDB().Raw("SELECT 1").Count(&count).Error

	// 获取运行时信息
	var m runtime.MemStats
	runtime.ReadMemStats(&m)

	return c.JSON(fiber.Map{
		"status":    "ok",
		"message":   "service is healthy",
		"timestamp": time.Now().Format(time.RFC3339),
		"database": map[string]any{
			"connected": dbErr == nil,
		},
		"system": map[string]any{
			"goroutines": runtime.NumGoroutine(),
			"memory": map[string]any{
				"alloc":       m.Alloc / 1024 / 1024,
				"total_alloc": m.TotalAlloc / 1024 / 1024,
				"sys":         m.Sys / 1024 / 1024,
				"gc_cycles":   m.NumGC,
			},
		},
	})
}
