package controllers

import (
	"net/http"

	"git.kirsle.net/apps/gophertype/pkg/responses"
	"git.kirsle.net/apps/gophertype/pkg/session"
)

// AgeVerify handles the age gate prompt page for NSFW sites.
func AgeVerify(w http.ResponseWriter, r *http.Request) {
	var (
		v       = responses.NewTemplateVars(w, r)
		next    = r.FormValue("next")
		confirm = r.FormValue("confirm")
	)

	if next == "" {
		next = "/"
	}
	v.V["Next"] = next

	if r.Method == http.MethodPost {
		if confirm == "true" {
			session := session.Get(r)
			session.AgeOK = true
			session.Save(w)
			responses.Redirect(w, r, next)
			return
		}
	}

	responses.RenderTemplate(w, r, "_builtin/age-gate.gohtml", v)
}
