package user

import (
	"encoding/json"
	"net/http"

	"crussell/db"
	"crussell/mw"
)

type LoyaltyResponse struct {
	Stamps       int    `json:"stamps"`
	ReferralCode string `json:"referralCode"`
}

// GET /api/user/loyalty
func GetLoyaltyHandler(w http.ResponseWriter, r *http.Request) {
	userID, _ := mw.GetUserID(r.Context())

	var loyalty LoyaltyResponse
	err := db.Conn.QueryRow(r.Context(), `
		SELECT loyalty_stamps, referral_code
		FROM users
		WHERE id = $1
	`, userID).Scan(&loyalty.Stamps, &loyalty.ReferralCode)

	if err != nil {
		http.Error(w, "failed to get loyalty info", http.StatusInternalServerError)
		return
	}


	_ = json.NewEncoder(w).Encode(loyalty)
}
