package GCP_CLOUD_FUNCTIONS

import (
	"net/http"
	"os"
)

// Mains is the Cloud Function entry point
func Mains(w http.ResponseWriter, r *http.Request) {
	// Validate token in header
	token := r.Header.Get("Authorization")
	if token == "" {
		http.Error(w, "Missing Authorization header", http.StatusUnauthorized)
		return
	}

	queryType := r.Header.Get("Querytype")
	if queryType == "" {
		http.Error(w, "Missing Querytype header", http.StatusBadRequest)
		return
	}

	// Get expected token from environment variable or use default
	expectedToken := os.Getenv("ACCESS_TOKEN")
	if expectedToken == "" {
		http.Error(w, "Missing access token", http.StatusUnauthorized)
		return
	}

	// Check if the token is valid
	if token != expectedToken {
		http.Error(w, "Invalid token", http.StatusUnauthorized)
		return
	}

	switch queryType {
	case "threat":
		ApiQuery_THREAT(w, r)
	case "toxic":
		ApiQuery_TOXIC_COMBINATION(w, r)
	case "riskbulletin":
		ApiQuery_RISK_BULLETIN(w, r)
	case "osvul":
		ApiQuery_OS_VULNERABILITY(w, r)
	default:
		http.Error(w, "Invalid query-type", http.StatusBadRequest)
	}
}
