package main

import (
	"net/http"

	"github.com/emarifer/go-templ-project-structure/db"
	"github.com/emarifer/go-templ-project-structure/handlers"
	"github.com/emarifer/go-templ-project-structure/services"
	"github.com/labstack/echo/v4"
	"github.com/labstack/echo/v4/middleware"
)

// In production, the name of the database
// would be obtained from an .env file
const dbName = "user_data.db"

func main() {
	app := echo.New()

	app.HTTPErrorHandler = handlers.CustomHTTPErrorHandler

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

	// We redirect the root route to the "/user" route
	app.GET("/", func(c echo.Context) error {
		return c.Redirect(http.StatusMovedPermanently, "/landing_page/")
	})

	app.GET("/pie_chart", func(c echo.Context) error {
		return c.Redirect(http.StatusMovedPermanently, "/pie_chart/")
	})

	uStore, err := db.NewUserStore(dbName)
	if err != nil {
		app.Logger.Fatalf("failed to create store: %s", err)
	}

	us := services.NewServicesUser(services.User{}, uStore)

	h := handlers.New(us)

	handlers.SetupRoutes(app, h)

	LStore, err := db.NewLoginStore(dbName)
	if err != nil {
		app.Logger.Fatalf("failed to create store: %s", err)
	}
	l := services.NewServicesLogin(services.Login{}, LStore)
	lh := handlers.NewLoginHandler(l)
	handlers.SetupRoutesLogin(app, lh)

	xStore, err := db.NewXsampleStore(dbName)
	if err != nil {
		app.Logger.Fatalf("failed to create store: %s", err)
	}
	xs := services.NewServicesXsample(services.Xsample{}, xStore)
	xh := handlers.NewXsampleHandler(xs)
	handlers.SetupRoutesXsample(app, xh)

	LpStore, err := db.NewLandingPageStore(dbName)
	if err != nil {
		app.Logger.Fatalf("failed to create store: %s", err)
	}
	lps := services.NewServicesLandingPage(services.LandingPage{}, LpStore)
	lphs := handlers.NewLandingPageHandler(lps)
	// handlers.SetupRoutesLandingPage(app, lphs)

	Lpmastermenustore, err := db.NewMasterMenuUserGroupStore(dbName)
	if err != nil {
		app.Logger.Fatalf("failed to create store: %s", err)
	}
	mastermenuservices := services.NewServicesMasterMenuUserGroup(services.MasterMenu{}, Lpmastermenustore)
	mastermenuhandler := handlers.NewMasterMenuUserGroupHandler(mastermenuservices)
	handlers.SetupRoutesLandingPage(app, lphs, mastermenuhandler)

	LpchartStore, err := db.NewPieChartStore(dbName)
	if err != nil {
		app.Logger.Fatalf("failed to create store: %s", err)
	}
	bchart, err := db.NewBarChartStore(dbName)
	if err != nil {
		app.Logger.Fatalf("failed to create store: %s", err)
	}

	lpchart := services.NewServicesPiechart(services.Piechart{}, services.Barchart{}, LpchartStore, bchart)
	lphsx := handlers.NewPiechartHandler(lpchart)
	handlers.SetupRoutesPieChart(app, lphsx)

	eStore, err := db.NewEmployeeStore(dbName)
	if err != nil {
		app.Logger.Fatalf("failed to create store: %s", err)
	}
	el := services.NewServicesEmployee(services.Employee{}, eStore)
	ehs := handlers.NewEmployeeHandler(el)
	handlers.SetupRoutesEmployee(app, ehs)

	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
*/
