package main

import (
	"os"
	"log"

	"github.com/pocketbase/pocketbase"
)

// Copied from https://github.com/s-petr/longhabit/blob/main/backend/main.go 

// application holds the core application state and configuration.
type application struct {
	pb     *pocketbase.PocketBase
	config appConfig
}

// appConfig holds the application's configuration settings.
type appConfig struct {
	dbDir              string
}

// newApplication creates and initializes a new application instance.
func newApplication() *application {
	dbDir := getEnvOrDefault("DB_DIR", "pb_data")

	return &application{
		pb: pocketbase.NewWithConfig(pocketbase.Config{DefaultDataDir: dbDir}),
		config: appConfig{
			dbDir:              dbDir,
		},
	}
}

func main() {
	app := newApplication()

	app.SetupCache()
	app.SetupCrons()
	app.SetupRoutes()

	log.Fatal(app.pb.Start())
}

// getEnvOrDefault retrieves the value of an environment variable by key.
// If the environment variable is empty or not set, it returns the defaultValue.
func getEnvOrDefault(key, defaultValue string) string {
	if value := os.Getenv(key); value != "" {
		return value
	}
	return defaultValue
}
