package server

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

	"github.com/gin-gonic/gin"
	domain "github.com/haydenhargreaves/Potion/internal/domain/user"
)

func (s *Server) EngagementViewRecipeHandlerV2(ctx *gin.Context) {
	recipeId, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":  http.StatusBadRequest,
			"message": fmt.Sprintf("[ERROR] Failed to parse ID parameter. %s", err.Error()),
		})
		return
	}

	userId := getUserId(ctx)
	if userId == nil {
		if engagement, err := s.deps.EngagementService.ViewRecipe(recipeId); err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"status":  http.StatusBadRequest,
				"message": fmt.Sprintf("[ERROR] Failed to create engagement. %s", err.Error()),
			})
		} else {
			ctx.JSON(http.StatusOK, gin.H{
				"status":     http.StatusOK,
				"message":    "[OK] Successfully created engagement.",
				"engagement": engagement,
			})
		}
		return
	}
	if engagement, err := s.deps.EngagementService.UserViewRecipe(*userId, recipeId); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":  http.StatusBadRequest,
			"message": fmt.Sprintf("[ERROR] Failed to create engagement. %s", err.Error()),
		})
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"status":     http.StatusOK,
			"message":    "[OK] Successfully created engagement.",
			"engagement": engagement,
		})
	}
}

func (s *Server) EngagementShareRecipeHandlerV2(ctx *gin.Context) {
	recipeId, err := strconv.Atoi(ctx.Param("id"))
	if err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":  http.StatusBadRequest,
			"message": fmt.Sprintf("[ERROR] Failed to parse ID parameter. %s", err.Error()),
		})
		return
	}

	userId := getUserId(ctx)
	if userId == nil {
		if engagement, err := s.deps.EngagementService.ShareRecipe(recipeId); err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"status":  http.StatusBadRequest,
				"message": fmt.Sprintf("[ERROR] Failed to create engagement. %s", err.Error()),
			})
		} else {
			ctx.JSON(http.StatusOK, gin.H{
				"status":     http.StatusOK,
				"message":    "[OK] Successfully created engagement.",
				"engagement": engagement,
			})
		}
		return
	}

	if engagement, err := s.deps.EngagementService.UserShareRecipe(*userId, recipeId); err != nil {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"status":  http.StatusBadRequest,
			"message": fmt.Sprintf("[ERROR] Failed to create engagement. %s", err.Error()),
		})
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"status":     http.StatusOK,
			"message":    "[OK] Successfully created engagement.",
			"engagement": engagement,
		})
	}
}

func (s *Server) EngagementFavoriteRecipeHandlerV2(ctx *gin.Context) {
	s.withAuthenticatedUser(ctx, func(ctx *gin.Context, user *domain.User) {
		recipeId, err := strconv.Atoi(ctx.Param("id"))
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"status":  http.StatusBadRequest,
				"message": fmt.Sprintf("[ERROR] Failed to parse ID parameter. %s", err.Error()),
			})
			return
		}

		if engagement, err := s.deps.EngagementService.UserFavoriteRecipe(user.Id, recipeId); err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"status":  http.StatusBadRequest,
				"message": fmt.Sprintf("[ERROR] Failed to create engagement. %s", err.Error()),
			})
		} else {
			ctx.JSON(http.StatusOK, gin.H{
				"status":     http.StatusOK,
				"message":    "[OK] Successfully created engagement.",
				"engagement": engagement,
			})
		}
	})
}

func (s *Server) EngagementMakeRecipeHandlerV2(ctx *gin.Context) {
	s.withAuthenticatedUser(ctx, func(ctx *gin.Context, user *domain.User) {
		recipeId, err := strconv.Atoi(ctx.Param("id"))
		if err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"status":  http.StatusBadRequest,
				"message": fmt.Sprintf("[ERROR] Failed to parse ID parameter. %s", err.Error()),
			})
			return
		}

		if engagement, err := s.deps.EngagementService.UserMakeRecipe(user.Id, recipeId); err != nil {
			ctx.JSON(http.StatusBadRequest, gin.H{
				"status":  http.StatusBadRequest,
				"message": fmt.Sprintf("[ERROR] Failed to create engagement. %s", err.Error()),
			})
		} else {
			ctx.JSON(http.StatusOK, gin.H{
				"status":     http.StatusOK,
				"message":    "[OK] Successfully created engagement.",
				"engagement": engagement,
			})
		}
	})
}
