package server

import (
	"fmt"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	domain "github.com/haydenhargreaves/Potion/internal/domain/server"
	"github.com/haydenhargreaves/Potion/internal/templates/components"
)

func (s *Server) EngagementViewRecipeHandler(ctx *gin.Context) {
	recipeId, _ := strconv.Atoi(ctx.Param("id"))

	// Ensure user is logged in with a valid account
	user := s.deps.UserService.GetAuthenicatedUser(ctx)
	if user == nil {
		// Log (stale) user out
		s.SetCookie(ctx, "jwt_token", "", -1)
		s.SetCookie(ctx, "search-filters", "", -1)
	}

	if !domain.IsLoggedIn(ctx) || user == nil {
		if _, err := s.deps.EngagementService.ViewRecipe(recipeId); err != nil {
			components.RenderErrorBanner(ctx, err.Error())
			ctx.JSON(http.StatusInternalServerError, gin.H{
				"status":  http.StatusInternalServerError,
				"message": err.Error(),
			})
		} else {
			ctx.Header("HX-Redirect", fmt.Sprintf(domain.WEB_RECIPE, recipeId))
			ctx.Status(http.StatusOK)
		}
		return
	}

	// We caught nil already, we can assume the user exists
	if _, err := s.deps.EngagementService.UserViewRecipe(user.Id, recipeId); err != nil {
		components.RenderErrorBanner(ctx, err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status":  http.StatusInternalServerError,
			"message": err.Error(),
		})
	} else {
		ctx.Header("HX-Redirect", fmt.Sprintf(domain.WEB_RECIPE, recipeId))
		ctx.Status(http.StatusOK)
	}
}

func (s *Server) EngagementShareRecipeHandler(ctx *gin.Context) {
	recipeId, _ := strconv.Atoi(ctx.Param("id"))

	// Ensure user is logged in with a valid account
	user := s.deps.UserService.GetAuthenicatedUser(ctx)
	if user == nil {
		// Log (stale) user out
		s.SetCookie(ctx, "jwt_token", "", -1)
		s.SetCookie(ctx, "search-filters", "", -1)
	}

	if !domain.IsLoggedIn(ctx) || user == nil {
		if _, err := s.deps.EngagementService.ShareRecipe(recipeId); err != nil {
			components.RenderErrorBanner(ctx, err.Error())
			ctx.JSON(http.StatusInternalServerError, gin.H{
				"status":  http.StatusInternalServerError,
				"message": err.Error(),
			})
		} else {
			ctx.Status(http.StatusNoContent)
		}
		return
	}

	if _, err := s.deps.EngagementService.UserShareRecipe(user.Id, recipeId); err != nil {
		components.RenderErrorBanner(ctx, err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status":  http.StatusInternalServerError,
			"message": err.Error(),
		})
	} else {
		ctx.Status(http.StatusNoContent)
	}
}

func (s *Server) EngagementFavoriteRecipeHandler(ctx *gin.Context) {
	// Ensure user is logged in with a valid account
	user := s.deps.UserService.GetAuthenicatedUser(ctx)
	if user == nil {
		// Log (stale) user out
		s.SetCookie(ctx, "jwt_token", "", -1)
		s.SetCookie(ctx, "search-filters", "", -1)
	}

	if !domain.IsLoggedIn(ctx) || user == nil {
		ctx.Header("HX-Redirect", domain.WEB_LOGIN)
		ctx.Status(http.StatusOK)
		return
	}

	id := ctx.Param("id")
	recipeId, _ := strconv.Atoi(id)

	if _, err := s.deps.EngagementService.UserFavoriteRecipe(user.Id, recipeId); err != nil {
		components.RenderErrorBanner(ctx, fmt.Sprintf("Something went wrong. %s.", err.Error()))
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status":  http.StatusInternalServerError,
			"message": err.Error(),
		})
	} else {
		ctx.Status(http.StatusNoContent)
	}
}

func (s *Server) EngagementMakeRecipeHandler(ctx *gin.Context) {
	// Ensure user is logged in with a valid account
	user := s.deps.UserService.GetAuthenicatedUser(ctx)
	if user == nil {
		// Log (stale) user out
		s.SetCookie(ctx, "jwt_token", "", -1)
		s.SetCookie(ctx, "search-filters", "", -1)
	}

	if !domain.IsLoggedIn(ctx) || user == nil {
		ctx.Header("HX-Redirect", domain.WEB_LOGIN)
		ctx.Status(http.StatusOK)
		return
	}

	id := ctx.Param("id")
	recipeId, _ := strconv.Atoi(id)

	if _, err := s.deps.EngagementService.UserMakeRecipe(user.Id, recipeId); err != nil {
		components.RenderErrorBanner(ctx, err.Error())
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status":  http.StatusInternalServerError,
			"message": err.Error(),
		})
	} else {
		ctx.Status(http.StatusNoContent)
	}
}
