package main

import (
	"net/http"

	"brainminder.speedtech.it/internal/version"
	"brainminder.speedtech.it/models"
	"github.com/justinas/nosurf"
)

func (app *application) newTemplateData(r *http.Request) map[string]any {
	data := map[string]any{
		"AuthenticatedUser": contextGetAuthenticatedUser(r),
		"CSRFToken":         nosurf.Token(r),
		"Version":           version.Get(),
	}

	typeModel := &models.TypeModel{DB: app.db}

	currentNotebookId := app.getCurrentNotebookId(r)
	criteria := map[string]any{
		"notebook_id": currentNotebookId,
	}

	types, _, _ := typeModel.Find(criteria)
	data["typesList"] = types

	notebookModel := &models.NotebookModel{DB: app.db}
	data["notebooksList"] = notebookModel.AllAsOptions(true)
	data["current_notebook_id"] = currentNotebookId

	return data
}

func (app *application) newEmailData() map[string]any {
	data := map[string]any{
		"BaseURL": app.config.baseURL,
	}

	return data
}

/*
func (app *application) backgroundTask(r *http.Request, fn func() error) {
	app.wg.Add(1)

	go func() {
		defer app.wg.Done()

		defer func() {
			err := recover()
			if err != nil {
				app.reportServerError(r, fmt.Errorf("%s", err))
			}
		}()

		err := fn()
		if err != nil {
			app.reportServerError(r, err)
		}
	}()
}
*/
