package app

import (
	"google.golang.org/grpc"
	healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
	"hyapp/pkg/grpchealth"
	"hyapp/pkg/healthhttp"
	"hyapp/services/activity-service/internal/config"
	mysqlstorage "hyapp/services/activity-service/internal/storage/mysql"
)

func newHealthServers(cfg config.Config, server *grpc.Server, repository *mysqlstorage.Repository) (*grpchealth.ServingChecker, *healthhttp.Server, error) {
	// gRPC health 和 HTTP ready 共用同一个 checker，避免一个入口显示 ready、另一个入口还在 draining。
	health := grpchealth.NewServingChecker("activity-service", grpchealth.Dependency{
		Name:  "mysql",
		Check: repository.Ping,
	})
	healthgrpc.RegisterHealthServer(server, grpchealth.NewServer(health))
	healthHTTP, err := healthhttp.New(cfg.HealthHTTPAddr, cfg.NodeID, health)
	if err != nil {
		return nil, nil, err
	}
	return health, healthHTTP, nil
}
