package main

import (
	"log"
	"os"

	"mrcc/internal/auth"
	"mrcc/internal/config"
	"mrcc/internal/httpserver"
	"mrcc/internal/logger"
)

func main() {
	cfg := config.Load("auth-service", "8081")
	logger.WithService(cfg.Name).Info("starting service", "port", cfg.Port)

	srv := httpserver.New(cfg)

	// JWT 密钥：默认开发密钥，生产环境通过 AUTH_JWT_SECRET 覆盖
	secret := os.Getenv("AUTH_JWT_SECRET")
	if secret == "" {
		secret = "mrcc-dev-secret-change-me"
	}

	store := auth.NewUserStore()
	jwtMgr := auth.NewJWTManager(secret)
	h := auth.NewHandler(store, jwtMgr)

	// 认证路由: /api/auth/register|login|me
	authGroup := srv.Group("/api/auth")
	h.RegisterAuthRoutes(authGroup)

	// 经济路由: /api/daily-reward|spend|buy|use-item
	apiGroup := srv.Group("/api")
	h.RegisterEconomyRoutes(apiGroup)

	log.Printf("auth-service listening on %s (endpoints: /api/auth/*, /api/daily-reward ...)", cfg.Address())
	if err := srv.Run(cfg.Address()); err != nil {
		log.Fatal(err)
	}
}
