package handlers import ( "gestion/db" "gestion/utils" "log" "net/http" "github.com/gin-gonic/gin" ) // GetMyReferralBalance — GET /api/v1/referral/balance (client) func GetMyReferralBalance(c *gin.Context) { database := c.MustGet("database").(*db.Database) username, exists := c.Get("username") if !exists { c.JSON(http.StatusUnauthorized, gin.H{"error": "Utilisateur non authentifié"}) return } settings, _ := database.GetSettings() if !settings.ReferralEnabled { c.JSON(http.StatusOK, gin.H{"balance": 0, "referral_enabled": false}) return } balance, err := database.GetClientReferralBalance(username.(string)) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "Solde de parrainage introuvable"}) return } c.JSON(http.StatusOK, gin.H{"balance": balance, "referral_enabled": true}) } // CreditClientReferralAdmin — POST /api/v2/admin/protected/client/:username/referral/credit (admin) func CreditClientReferralAdmin(c *gin.Context) { database := c.MustGet("database").(*db.Database) targetUsername := c.Param("username") var req struct { Amount float64 `json:"amount" binding:"required"` } if err := c.ShouldBindJSON(&req); err != nil || req.Amount <= 0 { c.JSON(http.StatusBadRequest, gin.H{"error": "Montant invalide"}) return } if err := database.CreditClientReferral(targetUsername, req.Amount); err != nil { utils.ServerErr(c, "Impossible de créditer le solde", err) return } balance, _ := database.GetClientReferralBalance(targetUsername) log.Printf("✅ [REFERRAL] +%.2f€ crédité à %s, nouveau solde: %.2f€", req.Amount, targetUsername, balance) c.JSON(http.StatusOK, gin.H{ "message": "Solde parrainage crédité", "balance": balance, }) } // ResetClientReferralAdmin — DELETE /api/v2/admin/protected/client/:username/referral/reset (admin) func ResetClientReferralAdmin(c *gin.Context) { database := c.MustGet("database").(*db.Database) targetUsername := c.Param("username") if err := database.ResetClientReferralBalance(targetUsername); err != nil { utils.ServerErr(c, "Impossible de réinitialiser le solde", err) return } log.Printf("✅ [REFERRAL] Solde parrainage remis à zéro pour %s", targetUsername) c.JSON(http.StatusOK, gin.H{ "message": "Solde parrainage réinitialisé", "balance": 0, }) } // GetClientReferralAdmin — GET /api/v2/admin/protected/client/:username/referral (admin) func GetClientReferralAdmin(c *gin.Context) { database := c.MustGet("database").(*db.Database) targetUsername := c.Param("username") balance, err := database.GetClientReferralBalance(targetUsername) if err != nil { c.JSON(http.StatusNotFound, gin.H{"error": "Client introuvable"}) return } c.JSON(http.StatusOK, gin.H{ "username": targetUsername, "balance": balance, }) }