package main

import (
	"cpone/db"
	"cpone/handlers"
	"time"

	"cpone/package/config"
	"cpone/package/database"

	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
	"go.uber.org/zap"
)

// In production, the name of the database
// would be obtained from an .env file

func main() {
	config.Load()
	app := echo.New()
	database.InitDB()

	app.HTTPErrorHandler = handlers.CustomHTTPErrorHandler

	app.Static("/", "assets")
	// app.Use(middleware.Logger())

	logger, _ := zap.NewProduction()
	app.Use(middleware.RequestLoggerWithConfig(
		middleware.RequestLoggerConfig{
			LogURI:    true,
			LogStatus: true,
			LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
				logger.Info("request",
					zap.String("URI", v.URI),
					zap.Int("status", v.Status),
					zap.Duration("Duration", v.Latency),

					zap.Any("date", time.Now().String()),
				)
				return nil
			},
		},
	))
	app.Use(middleware.CORSWithConfig(
		middleware.CORSConfig{
			AllowOrigins: []string{"*"},
			AllowMethods: []string{"*"},
		},
	))
	appStore, err := db.NewAppStore(database.Handle)
	if err != nil {
		app.Logger.Fatalf("failed to create store: %s", err)
	}

	// SETUP ROUT PUBLIC
	handlers.SetupRoutesPublic(app, appStore)
	// SETUP ROUT CORPORATE
	handlers.SetupRoutesCorporate(app, appStore)
	// SETUP ROUT CLIENT
	handlers.SetupRoutesClient(app, appStore)
	// SETUP ROUT DEV
	handlers.SetupRoutesDev(app, appStore)
	app.Logger.Fatal(app.Start(":5000"))
}

/*
https://www.reddit.com/r/golang/comments/17d12wk/using_echo_with_ahtempl/

https://templ.guide/project-structure/project-structure
https://github.com/a-h/templ/tree/main/examples/counter

https://echo.labstack.com/docs

https://www.reddit.com/r/golang/comments/vggekd/whats_the_usage_of_error_in_echo_framework/
https://echo.labstack.com/docs/error-handling
https://medium.com/@emretiryaki3/custom-error-handling-for-echo-web-framework-go-152992ab9cce
https://sorcererxw.com/en/articles/go-echo-error-handing
*/
