package handlers

import (
	"html/template"
	"minecraft-profile/middleware"
	"net/http"
	"os"
)

func IndexHandler(w http.ResponseWriter, r *http.Request) {
	if _, ok := middleware.GetSession(r); ok {
		http.Redirect(w, r, "/profile", http.StatusSeeOther)
		return
	}

	tmpl := template.Must(template.ParseFiles("templates/index.html"))
	tmpl.Execute(w, nil)
}

func RobotsHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "text/plain")
	w.Write([]byte(`User-agent: *
Disallow: /s3cr3t_adm1n_p4th
`))
}

func SecretPathHandler(w http.ResponseWriter, r *http.Request) {
	flag1 := os.Getenv("FLAG1")
	data := map[string]interface{}{
		"Flag": flag1,
	}

	tmpl := template.Must(template.ParseFiles("templates/secret.html"))
	tmpl.Execute(w, data)
}

func AdminHandler(w http.ResponseWriter, r *http.Request) {
	flag2 := os.Getenv("FLAG2")

	data := map[string]interface{}{
		"Flag": flag2,
	}

	tmpl := template.Must(template.ParseFiles("templates/admin.html"))
	tmpl.Execute(w, data)
}
