package agent

import (
	"embed"
	"net/http"

	"tea.chunkbyte.com/kato/go-worm/lib/helpers"
)

//go:embed web/index.html web/app.js
var webFS embed.FS

func (a *Agent) handleWeb(w http.ResponseWriter, r *http.Request) {
	if r.Method != http.MethodGet {
		helpers.WriteError(w, http.StatusMethodNotAllowed, "method not allowed")
		return
	}
	name := ""
	contentType := ""
	switch r.URL.Path {
	case "/", "/index.html":
		name = "web/index.html"
		contentType = "text/html; charset=utf-8"
	case "/app.js":
		name = "web/app.js"
		contentType = "text/javascript; charset=utf-8"
	default:
		helpers.WriteError(w, http.StatusNotFound, "endpoint not found")
		return
	}
	data, err := webFS.ReadFile(name)
	if err != nil {
		helpers.WriteError(w, http.StatusInternalServerError, "ui asset missing")
		return
	}
	w.Header().Set("Content-Type", contentType)
	w.Header().Set("Cache-Control", "no-cache")
	_, _ = w.Write(data)
}
