package main

import (
	"log/slog"

	"github.com/Theodor-Springmann-Stiftung/lenz-web/app"
	"github.com/Theodor-Springmann-Stiftung/lenz-web/server"
)

func main() {
	cfg, err := GetConfig()
	if err != nil {
		panic(err)
	}

	if cfg.Debug {
		slog.SetLogLoggerLevel(slog.LevelDebug)
	}

	application, err := app.New(cfg)
	if err != nil {
		panic(err)
	}

	err = Serve(application)
	if err != nil {
		panic(err)
	}
}

func Serve(app *app.App) error {
	server, err := server.NewServer(app)
	if err != nil {
		return err
	}
	server.Start()
	return nil
}

func GetConfig() (app.Config, error) {
	cp := app.NewConfigProvider([]string{"config.dev.json", "config.json"})
	if err := cp.Read(); err != nil {
		return app.Config{}, err
	}
	return *cp.Config, nil
}
