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()
	configController   controllers.ConfigController   = controllers.NewConfigController()
	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.RequireAdmin, shopItemController.EditItemView)
		viewRoutes.POST("/shopitems/:id/edit", authValidator.RequireAdmin, shopItemController.EditItemHandler)
		viewRoutes.GET("/shopitems/:id/delete", authValidator.RequireAdmin, shopItemController.DeleteItemView)
		viewRoutes.POST("/shopitems/:id/delete", authValidator.RequireAdmin, shopItemController.DeleteItemHandler)
		viewRoutes.GET("/variant/:id/print", authValidator.RequireAdmin, printController.PrintVariantView)
		viewRoutes.GET("/cart/print", authValidator.RequireAdmin, printController.PrintCartView)
		viewRoutes.POST("/print", authValidator.RequireAdmin, printController.PrintHandler)

		viewRoutes.GET("/config", authValidator.RequireAdmin, configController.ConfigView)
		viewRoutes.POST("/config/:id", authValidator.RequireAdmin, configController.ConfigHandler)
		viewRoutes.POST("/config", authValidator.RequireAdmin, configController.AddConfigHandler)

		viewRoutes.GET("/tags", authValidator.RequireAdmin, configController.TagView)
		viewRoutes.POST("/tags/:id", authValidator.RequireAdmin, configController.TagHandler)
		viewRoutes.GET("/tags/:id", userController.TagView)
		viewRoutes.POST("/tags", authValidator.RequireAdmin, configController.AddTagHandler)

		viewRoutes.GET("/paper", authValidator.RequireAdmin, configController.PaperView)
		viewRoutes.POST("/paper/:id", authValidator.RequireAdmin, configController.PaperHandler)
		viewRoutes.GET("/paper/:id", userController.TagView)
		viewRoutes.POST("/paper", authValidator.RequireAdmin, configController.AddPaperHandler)

		viewRoutes.GET("/invoice", authValidator.RequireAdmin, configController.InvoiceView)
		viewRoutes.POST("/invoice/:id", authValidator.RequireAdmin, configController.InvoiceHandler)

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

		viewRoutes.GET("/orders", authValidator.RequireAdmin, cartItemController.OrdersView)
		viewRoutes.POST("/order/:token/edit", authValidator.RequireAdmin, 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", userController.InitAdmin)
		viewRoutes.GET("/register/:token", userController.RegisterView)
		viewRoutes.GET("/invites", authValidator.RequireAdmin, userController.InviteView)
		viewRoutes.POST("/invites", authValidator.RequireAdmin, userController.InviteHandler)
		viewRoutes.GET("/passwordreset", authValidator.RequireAuth, userController.ResetView)
		viewRoutes.GET("/additem", authValidator.RequireAdmin, shopItemController.AddItemView)
		viewRoutes.GET("/batchupload", authValidator.RequireAdmin, shopItemController.AddItemsView)
		viewRoutes.POST("/login", userController.LoginHandler)
		viewRoutes.POST("/register", userController.RegisterHandler)
		viewRoutes.POST("/additem", authValidator.RequireAdmin, shopItemController.AddItemHandler)
		viewRoutes.POST("/batchupload", authValidator.RequireAdmin, shopItemController.AddItemsHandler)
		viewRoutes.POST("/passwordreset", authValidator.RequireAuth, userController.ResetHandler)
	}

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