package main

import (
	"embed"
	"html/template"
	"io"
	"io/fs"
	"log"
	"net/http"
	"os"
	"strings"
)

//go:embed templates/*.tmpl
var tmplFS embed.FS

//go:embed *.svg *.css
var static embed.FS

var templates *template.Template
var devMode bool

type PageData struct {
	Page      string
	Title     string
	Desc      string
	ActiveNav string
}

func loadTemplates() {
	if devMode {
		templates = template.Must(template.ParseGlob("templates/*.tmpl"))
	} else {
		sub, _ := fs.Sub(tmplFS, "templates")
		templates = template.Must(template.ParseFS(sub, "*.tmpl"))
	}
}

func render(w http.ResponseWriter, data PageData) {
	if devMode {
		loadTemplates()
	}
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	if err := templates.ExecuteTemplate(w, "base.tmpl", data); err != nil {
		log.Printf("template error: %v", err)
		http.Error(w, "Internal error", 500)
	}
}

func geoHandler(w http.ResponseWriter, r *http.Request) {
	ip := r.Header.Get("X-Forwarded-For")
	if ip == "" {
		ip = r.RemoteAddr
	}
	if i := strings.LastIndex(ip, ":"); i >= 0 {
		ip = ip[:i]
	}
	ip = strings.Trim(ip, "[]")

	resp, err := http.Get("https://ipapi.co/" + ip + "/json/")
	if err != nil {
		http.Error(w, `{"error":"geo failed"}`, 502)
		return
	}
	defer resp.Body.Close()
	w.Header().Set("Content-Type", "application/json")
	io.Copy(w, resp.Body)
}

func main() {
	if _, err := os.Stat("templates"); err == nil {
		devMode = true
		log.Println("dev mode: templates loaded from disk")
	}
	loadTemplates()

	port := os.Getenv("PORT")
	if port == "" {
		port = "8099"
	}

	http.HandleFunc("/geo", geoHandler)

	http.HandleFunc("/hosted", func(w http.ResponseWriter, r *http.Request) {
		render(w, PageData{Page: "hosted", Title: "vault1984 — Hosted", ActiveNav: "hosted"})
	})
	http.HandleFunc("/install", func(w http.ResponseWriter, r *http.Request) {
		render(w, PageData{Page: "install", Title: "Self-host — vault1984", Desc: "Self-host vault1984 in 30 seconds. One binary, no dependencies.", ActiveNav: "install"})
	})
	http.HandleFunc("/pricing", func(w http.ResponseWriter, r *http.Request) {
		render(w, PageData{Page: "pricing", Title: "Pricing — vault1984", Desc: "Free self-hosted or $12/year hosted (launch price). No tiers, no per-seat, no contact sales.", ActiveNav: "pricing"})
	})
	http.HandleFunc("/privacy", func(w http.ResponseWriter, r *http.Request) {
		render(w, PageData{Page: "privacy", Title: "Privacy Policy — vault1984"})
	})
	http.HandleFunc("/terms", func(w http.ResponseWriter, r *http.Request) {
		render(w, PageData{Page: "terms", Title: "Terms of Service — vault1984"})
	})
	http.HandleFunc("/sources", func(w http.ResponseWriter, r *http.Request) {
		render(w, PageData{Page: "sources", Title: "Sources — vault1984"})
	})
	http.HandleFunc("/styleguide", func(w http.ResponseWriter, r *http.Request) {
		render(w, PageData{Page: "styleguide", Title: "vault1984 — Styleguide"})
	})

	// Catch-all: index page at "/" or static files or .html redirects
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		if r.URL.Path == "/" {
			render(w, PageData{Page: "index", Title: "vault1984 — AI-native password manager", Desc: "Field-level encryption for password managers that live alongside AI assistants. Your AI gets what it needs. Your secrets stay yours."})
			return
		}
		// Redirect old .html URLs to clean paths
		if strings.HasSuffix(r.URL.Path, ".html") {
			clean := strings.TrimSuffix(r.URL.Path, ".html")
			if clean == "/index" {
				clean = "/"
			}
			http.Redirect(w, r, clean, http.StatusMovedPermanently)
			return
		}
		http.FileServer(http.FS(static)).ServeHTTP(w, r)
	})

	log.Printf("vault1984-web starting on :%s", port)
	if err := http.ListenAndServe(":"+port, nil); err != nil {
		log.Fatal(err)
	}
}
