package http_server

import (
	"log"
	"net/http"

	"github.com/Vomitblood/cspj-application/server/internal/db"
	"github.com/Vomitblood/cspj-application/server/internal/sql_injection"
)

func healthCheck(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	w.Write([]byte(`{"status": "ok"}`))
}

// setup the http server
func ServeApi() {
	http.HandleFunc("/health", healthCheck)
	http.HandleFunc("/health-db", db.DbHealthCheck)
	http.HandleFunc("/setup-demo-db", db.SetupDemoDb)
	http.HandleFunc("/nuke-db", db.NukeDb)
	http.HandleFunc("/fetch-all-users", db.FetchAllUsers)
	http.HandleFunc("/execute-sql", sql_injection.ExecuteSql)
	http.HandleFunc("/login-sql", sql_injection.LoginSql)
	http.HandleFunc("/secure-execute-sql", sql_injection.SecureExecuteSql)
	http.HandleFunc("/secure-login-sql", sql_injection.SecureLoginSql)
	http.HandleFunc("/secure-get-user", sql_injection.SecureGetUser)
	log.Println("Server is running on http://localhost:5000")
	if err := http.ListenAndServe(":5000", nil); err != nil {
		log.Fatalf("Failed to start server: %v", err)
	}
}
