package main

import (
	"fmt"
	"io"
	"net/http"
	"os"

	"github.com/gin-gonic/gin"
	"github.com/joho/godotenv"

	"git.dynamicdiscord.de/kalipso/zineshop/controllers"
	"git.dynamicdiscord.de/kalipso/zineshop/middlewares"
	"git.dynamicdiscord.de/kalipso/zineshop/repositories"
)

var (
	shopItemController controllers.ShopItemController = controllers.NewShopItemController()
	userController     controllers.UserController     = controllers.UserController{}
	cartItemController controllers.CartItemController = controllers.NewCartItemController()
	printController    controllers.PrintController    = controllers.NewPrintController()
	authValidator      middlewares.AuthValidator      = middlewares.AuthValidator{}
)

func LoadEnvVariables() {
	err := godotenv.Load(".env")

	if err != nil {
		fmt.Println("Error loading .env file")
	}
}

func setupLogOutput() {
	f, _ := os.Create("gin.log")
	gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
}

func SetReply(ctx *gin.Context, err error, message any) {
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
	} else {
		ctx.JSON(http.StatusOK, message)
	}
}

func main() {
	LoadEnvVariables()

	repositories.InitRepositories()

	server := gin.New()
	server.Use(gin.Recovery())
	server.Use(gin.Logger())

	server.Static("/static", os.Getenv("STATIC"))
	server.LoadHTMLGlob(fmt.Sprintf("%s/*.html", os.Getenv("VIEWS")))

	viewRoutes := server.Group("/", authValidator.OptionalAuth)
	{
		viewRoutes.GET("/", userController.MainView)
		viewRoutes.GET("/shopitems/:id", shopItemController.ShopItemView)
		viewRoutes.GET("/shopitems/:id/edit", authValidator.RequireAuth, shopItemController.EditItemView)
		viewRoutes.POST("/shopitems/:id/edit", authValidator.RequireAuth, shopItemController.EditItemHandler)
		viewRoutes.GET("/shopitems/:id/delete", authValidator.RequireAuth, shopItemController.DeleteItemView)
		viewRoutes.POST("/shopitems/:id/delete", authValidator.RequireAuth, shopItemController.DeleteItemHandler)
		viewRoutes.GET("/variant/:id/print", authValidator.RequireAuth, printController.PrintVariantView)
		viewRoutes.GET("/cart/print", authValidator.RequireAuth, printController.PrintCartView)
		viewRoutes.POST("/print", authValidator.RequireAuth, printController.PrintHandler)

		viewRoutes.GET("/tags", authValidator.RequireAuth, shopItemController.TagView)
		viewRoutes.POST("/tags/:id", authValidator.RequireAuth, shopItemController.TagHandler)
		viewRoutes.GET("/tags/:id", userController.TagView)
		viewRoutes.POST("/tags", authValidator.RequireAuth, shopItemController.AddTagHandler)
		viewRoutes.GET("/cart", cartItemController.CartItemView)
		viewRoutes.POST("/cart", cartItemController.AddItemHandler)
		viewRoutes.POST("/cart/delete", cartItemController.DeleteItemHandler)
		viewRoutes.POST("/cart/edit", cartItemController.EditItemHandler)
		viewRoutes.GET("/checkout", cartItemController.CheckoutView)
		viewRoutes.POST("/checkout", cartItemController.CheckoutHandler)
		viewRoutes.POST("/order", cartItemController.OrderHandler)
		viewRoutes.GET("/order/:token", cartItemController.OrderView)
		viewRoutes.GET("/order/:token/print", authValidator.RequireAuth, printController.PrintOrderView)

		viewRoutes.GET("/orders", authValidator.RequireAuth, cartItemController.OrdersView)
		viewRoutes.POST("/order/:token/edit", authValidator.RequireAuth, cartItemController.OrdersHandler)

		//write middleware that redirects to homescreen on register/login/reset for logged in users
		viewRoutes.GET("/login", userController.LoginView)
		viewRoutes.GET("/logout", userController.Logout)
		viewRoutes.GET("/register/:token", userController.RegisterView)
		viewRoutes.GET("/passwordreset", userController.ResetView)
		viewRoutes.GET("/additem", authValidator.RequireAuth, shopItemController.AddItemView)
		viewRoutes.GET("/batchupload", authValidator.RequireAuth, shopItemController.AddItemsView)
		viewRoutes.POST("/login", userController.LoginHandler)
		viewRoutes.POST("/register", userController.RegisterHandler)
		viewRoutes.POST("/additem", authValidator.RequireAuth, shopItemController.AddItemHandler)
		viewRoutes.POST("/batchupload", authValidator.RequireAuth, shopItemController.AddItemsHandler)
		viewRoutes.POST("/passwordreset", userController.ResetHandler)
	}

	server.Run(":" + os.Getenv("PORT"))
}
