package main

import (
	// Internal
	"notes2/authentication"
	"notes2/html_template"
	"os"

	// Standard
	"bufio"
	"context"
	"embed"
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"log/slog"
	"net/http"
	"path"
	"regexp"
	"strconv"
	"strings"
	"text/template"
)

const VERSION = "v16"
const CONTEXT_USER = 1
const SYNC_PAGINATION = 200

var (
	FlagGenerate       bool
	FlagLoremIpsum     bool
	FlagDev            bool
	FlagConfig         string
	FlagCreateUser     string
	FlagChangePassword string
	Webengine          HTMLTemplate.Engine
	config             Config
	Log                *slog.Logger
	AuthManager        authentication.Manager
	RxpBearerToken     *regexp.Regexp

	//go:embed views
	ViewFS embed.FS

	//go:embed static
	StaticFS embed.FS

	//go:embed sql
	SqlFS embed.FS
)

func init() { // {{{
	// Configuration filename to use with a somewhat sane default.
	cfgDir, err := os.UserConfigDir()
	if err != nil {
		panic(err)
	}
	cfgFilename := path.Join(cfgDir, "notes2.json")

	flag.StringVar(&FlagConfig, "config", cfgFilename, "Configuration file")
	flag.BoolVar(&FlagDev, "dev", false, "Use local files instead of embedded files")
	flag.BoolVar(&FlagGenerate, "generate", false, "Generate test data")
	flag.BoolVar(&FlagLoremIpsum, "lorem-ipsum", false, "Replace all G- nodes with lorem ipsum paragraphs")
	flag.StringVar(&FlagCreateUser, "create-user", "", "Username for creating a new user")
	flag.StringVar(&FlagChangePassword, "change-password", "", "Change the password for the given username")
	flag.Parse()

	RxpBearerToken = regexp.MustCompile("(?i)^\\s*Bearer\\s+(.*?)\\s*$")
} // }}}
func initLog() { // {{{
	opts := slog.HandlerOptions{}
	opts.Level = slog.LevelDebug
	Log = slog.New(slog.NewJSONHandler(os.Stdout, &opts))
} // }}}
func main() { // {{{
	initLog()
	err := readConfig(FlagConfig)
	if err != nil {
		Log.Error("config", "error", err)
		os.Exit(1)
	}

	// dbschema uses the embedded SQL files to keep the database schema up to date.
	err = initDB()
	if err != nil {
		Log.Error("database", "error", err)
		os.Exit(1)
	}

	// The session manager contains authentication, authorization and session settings.
	AuthManager, err = authentication.NewManager(db, Log, config.JWT.Secret, config.JWT.ExpireDays)

	// Generate test data?
	if FlagGenerate {
		err := TestData()
		if err != nil {
			fmt.Printf("%s\n", err)
			return
		}
		return
	}

	if FlagLoremIpsum {
		err := LoremIpsum()
		if err != nil {
			fmt.Printf("%s\n", err)
			return
		}
		return
	}

	// A new user?
	if FlagCreateUser != "" {
		createNewUser(FlagCreateUser)
		return
	}

	// Forgotten the password?
	if FlagChangePassword != "" {
		changePassword(FlagChangePassword)
		return
	}

	// The webengine takes layouts, pages and components and renders them into HTML.
	Webengine, err = HTMLTemplate.NewEngine(ViewFS, StaticFS, FlagDev)
	if err != nil {
		panic(err)
	}

	http.HandleFunc("/", rootHandler)
	http.HandleFunc("/login", pageLogin)
	http.HandleFunc("/sync", pageSync)
	http.HandleFunc("/offline", pageOffline)

	http.HandleFunc("/user/authenticate", AuthManager.AuthenticationHandler)

	http.HandleFunc("/sync/from_server/count/{sequence}", authenticated(actionSyncFromServerCount))
	http.HandleFunc("/sync/from_server/{sequence}/{offset}", authenticated(actionSyncFromServer))
	http.HandleFunc("/sync/to_server", authenticated(actionSyncToServer))

	http.HandleFunc("/node/retrieve/{uuid}", authenticated(actionNodeRetrieve))
	http.HandleFunc("/node/history/retrieve/{uuid}/{offset}", authenticated(actionNodeHistoryRetrieve))
	http.HandleFunc("/node/history/count/{uuid}", authenticated(actionNodeHistoryCount))

	http.HandleFunc("/service_worker.js", pageServiceWorker)

	listen := fmt.Sprintf("%s:%d", config.Network.Address, config.Network.Port)
	Log.Info("webserver", "listen_address", listen)
	http.ListenAndServe(listen, nil)
} // }}}

func authenticated(fn func(http.ResponseWriter, *http.Request)) func(http.ResponseWriter, *http.Request) { // {{{
	return func(w http.ResponseWriter, r *http.Request) {
		failed := func(err error) {
			j, _ := json.Marshal(struct {
				OK         bool
				Error      string
				AuthFailed bool
			}{false, err.Error(), true})
			w.Write(j)
		}

		// The Bearer token is extracted.
		authHeader := r.Header.Get("Authorization")
		authParts := RxpBearerToken.FindStringSubmatch(authHeader)
		if len(authParts) != 2 {
			failed(fmt.Errorf("Authorization missing or invalid"))
			return
		}
		token := authParts[1]

		// Token signature is verified with the application secret key.
		claims, err := AuthManager.VerifyToken(token)
		if err != nil {
			failed(err)
			return
		}

		// User object is added to the context for the next handler.
		user := NewUser(claims)
		r = r.WithContext(context.WithValue(r.Context(), CONTEXT_USER, user))

		Log.Debug("webserver", "op", "request", "method", r.Method, "url", r.URL.String(), "username", user.Username, "client", user.ClientUUID)
		fn(w, r)
	}
} // }}}

func rootHandler(w http.ResponseWriter, r *http.Request) { // {{{
	// All URLs not specifically handled are routed to this function.
	// Everything going here should be a static resource.
	if r.URL.Path == "/" {
		page := NewPage("notes2")

		err := Webengine.Render(page, w, r)
		if err != nil {
			w.Write([]byte(err.Error()))
			return
		}
		return
	}

	Webengine.StaticResource(w, r)
} // }}}
func httpError(w http.ResponseWriter, err error) { // {{{
	j, _ := json.Marshal(struct {
		OK    bool
		Error string
	}{false, err.Error()})
	w.Write(j)
} // }}}

func pageServiceWorker(w http.ResponseWriter, r *http.Request) { // {{{
	w.Header().Add("Content-Type", "text/javascript; charset=utf-8")

	var tmpl *template.Template
	var err error
	if FlagDev {
		tmpl, err = template.ParseFiles("static/service_worker.js")
	} else {
		tmpl, err = template.ParseFS(StaticFS, "static/service_worker.js")
	}
	if err != nil {
		w.Write([]byte(err.Error()))
		return
	}

	err = tmpl.Execute(w, struct {
		VERSION string
		DevMode bool
	}{VERSION, FlagDev})
	if err != nil {
		w.Write([]byte(err.Error()))
		return
	}
} // }}}
func pageOffline(w http.ResponseWriter, r *http.Request) { // {{{
	page := NewPage("offline")

	err := Webengine.Render(page, w, r)
	if err != nil {
		w.Write([]byte(err.Error()))
		return
	}
} // }}}
func pageLogin(w http.ResponseWriter, r *http.Request) { // {{{
	page := NewPage("login")

	err := Webengine.Render(page, w, r)
	if err != nil {
		w.Write([]byte(err.Error()))
		return
	}
} // }}}
func pageSync(w http.ResponseWriter, r *http.Request) { // {{{
	page := NewPage("sync")

	err := Webengine.Render(page, w, r)
	if err != nil {
		w.Write([]byte(err.Error()))
		return
	}
} // }}}

func actionSyncFromServer(w http.ResponseWriter, r *http.Request) { // {{{
	// The purpose of the Client UUID is to avoid
	// sending nodes back once again to a client that
	// just created or modified it.
	user := getUser(r)
	changedFrom, _ := strconv.Atoi(r.PathValue("sequence"))
	offset, _ := strconv.Atoi(r.PathValue("offset"))

	nodes, maxSeq, moreRowsExist, err := Nodes(user.UserID, offset, uint64(changedFrom), user.ClientUUID)
	if err != nil {
		Log.Error("/sync/from_server", "error", err)
		httpError(w, err)
		return
	}

	/*
		Log.Debug("/sync/from_server", "num_nodes", len(nodes), "maxSeq", maxSeq)
		foo, _ := json.Marshal(nodes)
		os.WriteFile(fmt.Sprintf("/tmp/nodes-%d.json", offset), foo, 0644)
	*/

	j, _ := json.Marshal(struct {
		OK       bool
		Nodes    []Node
		MaxSeq   uint64
		Continue bool
	}{true, nodes, maxSeq, moreRowsExist})
	w.Write(j)
} // }}}
func actionSyncFromServerCount(w http.ResponseWriter, r *http.Request) { // {{{
	// The purpose of the Client UUID is to avoid
	// sending nodes back once again to a client that
	// just created or modified it.
	user := getUser(r)
	changedFrom, _ := strconv.Atoi(r.PathValue("sequence"))

	count, err := NodesCount(user.UserID, uint64(changedFrom), user.ClientUUID)
	if err != nil {
		Log.Error("/sync/from_server/count", "error", err)
		httpError(w, err)
		return
	}

	j, _ := json.Marshal(struct {
		OK    bool
		Count int
	}{
		true,
		count,
	})
	w.Write(j)
} // }}}
func actionNodeRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
	user := getUser(r)
	var err error

	uuid := r.PathValue("uuid")
	node, err := RetrieveNode(user.UserID, uuid)
	if err != nil {
		responseError(w, err)
		return
	}

	responseData(w, map[string]interface{}{
		"OK":   true,
		"Node": node,
	})
} // }}}
func actionNodeHistoryRetrieve(w http.ResponseWriter, r *http.Request) { // {{{
	user := getUser(r)
	var err error

	uuid := r.PathValue("uuid")
	offset, err := strconv.Atoi(r.PathValue("offset"))
	if err != nil {
		responseError(w, err)
		return
	}

	nodes, hasMore, err := RetrieveNodeHistory(user.UserID, uuid, offset)
	if err != nil {
		responseError(w, err)
		return
	}

	responseData(w, map[string]any{
		"OK":      true,
		"Nodes":   nodes,
		"HasMore": hasMore,
	})
} // }}}
func actionNodeHistoryCount(w http.ResponseWriter, r *http.Request) { // {{{
	user := getUser(r)
	var err error

	uuid := r.PathValue("uuid")

	count, err := RetrieveNodeHistoryCount(user.UserID, uuid)
	if err != nil {
		responseError(w, err)
		return
	}

	responseData(w, map[string]any{
		"OK":      true,
		"Count":   count,
	})
} // }}}
func actionSyncToServer(w http.ResponseWriter, r *http.Request) { // {{{
	user := getUser(r)

	body, _ := io.ReadAll(r.Body)
	var request struct {
		NodeData string
	}
	err := json.Unmarshal(body, &request)
	if err != nil {
		httpError(w, err)
		return
	}

	_, err = db.Exec(`CALL add_nodes($1, $2, $3::jsonb)`, user.UserID, user.ClientUUID, request.NodeData)
	if err != nil {
		Log.Error("sync", "error", err)
		httpError(w, err)
		return
	}

	responseData(w, map[string]any{
		"OK": true,
	})
} // }}}

func createNewUser(username string) { // {{{
	reader := bufio.NewReader(os.Stdin)

	fmt.Print("\nPassword: ")
	pwd, _ := reader.ReadString('\n')
	pwd = strings.Trim(pwd, "\r\n")

	fmt.Print("Name: ")
	name, _ := reader.ReadString('\n')
	name = strings.Trim(name, "\r\n")

	alreadyExists, err := AuthManager.CreateUser(username, pwd, name)
	if alreadyExists {
		fmt.Printf("\nUser '%s' already exists\n", username)
		return
	}
	if err != nil {
		Log.Error("create_user", "error", err)
		return
	}

	fmt.Printf("\nUser '%s' with username '%s' is created.\n", name, username)
} // }}}
func changePassword(username string) { // {{{
	reader := bufio.NewReader(os.Stdin)

	fmt.Print("\nPassword: ")
	newPwd, _ := reader.ReadString('\n')
	newPwd = strings.Trim(newPwd, "\r\n")

	hasChanged, err := AuthManager.ChangePassword(username, "", newPwd, true)
	if !hasChanged {
		fmt.Printf("Invalid user\n")
		return
	}
	if err != nil {
		Log.Error("change_password", "error", err)
		return
	}

	fmt.Printf("\nPassword changed\n")
} // }}}
func getUser(r *http.Request) UserSession { // {{{
	user, _ := r.Context().Value(CONTEXT_USER).(UserSession)
	return user
} // }}}
