package main

import (
	"os"

	config "astrology-api/configs"
	"astrology-api/notifications"
	"astrology-api/routes"
	"astrology-api/services"

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

func main() {
	config.LoadEnv()
	config.ConnectDB()
	//config.ConnectRedis()
	config.LoadPhonePeConfig()

	// Agora credentials for the consultation channels. Loaded before the
	// routes, because each route file constructs the token service at wiring
	// time. With none set, consultations still run end to end and the apps
	// simply get no credentials back.
	config.LoadAgoraConfig()

	// Says at boot whether consultations can run at all. Without this, an
	// unset credential surfaces as a live 400 from /consultation/start that
	// reads as a code fault rather than as configuration.
	config.LogAgoraConfig()

	// Say at boot whether social login is configured. The failure mode otherwise
	// is a live 400 from /social/*-login, which reads as a code fault rather than
	// a .env the running service never read.
	services.LogFirebaseConfig()

	// Push notifications. Says at boot whether FCM is configured, for the same
	// reason: a silently disabled notifier looks like a code fault later.
	notifications.Init(config.DB)

	gin.SetMode(gin.ReleaseMode)
	r := gin.Default()
	r.Static("/uploads", "./uploads")
	err := r.SetTrustedProxies(nil)
	if err != nil {
		panic(err)
	}
	routes.SetupRoutes(r)

	// APP_PORT was already in .env but ignored; default keeps the old :8080.
	port := os.Getenv("APP_PORT")

	if port == "" {
		port = "8080"
	}

	if err := r.Run(":" + port); err != nil {
		panic(err)
	}
}
