package controller

import (
	"narasi-ahli-be/app/module/ebooks/service"
	usersRepository "narasi-ahli-be/app/module/users/repository"
	"narasi-ahli-be/utils/paginator"
	"strconv"

	"github.com/gofiber/fiber/v2"
	"github.com/rs/zerolog"

	utilRes "narasi-ahli-be/utils/response"
)

type ebookWishlistsController struct {
	wishlistService service.EbookWishlistsService
	usersRepo       usersRepository.UsersRepository
	Log             zerolog.Logger
}

type EbookWishlistsController interface {
	GetByUserId(c *fiber.Ctx) error
	AddToWishlist(c *fiber.Ctx) error
	RemoveFromWishlist(c *fiber.Ctx) error
	IsInWishlist(c *fiber.Ctx) error
}

func NewEbookWishlistsController(wishlistService service.EbookWishlistsService, usersRepo usersRepository.UsersRepository, log zerolog.Logger) EbookWishlistsController {
	return &ebookWishlistsController{
		wishlistService: wishlistService,
		usersRepo:       usersRepo,
		Log:             log,
	}
}

// GetByUserId Wishlist
// @Summary      Get user wishlist
// @Description  API for getting user wishlist
// @Tags         Ebook Wishlist
// @Security     Bearer
// @Param        X-Client-Key header string true "Insert the X-Client-Key"
// @Param 	     req query paginator.Pagination false "pagination parameters"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ebooks/wishlist [get]
func (_i *ebookWishlistsController) GetByUserId(c *fiber.Ctx) error {
	paginate, err := paginator.Paginate(c)
	if err != nil {
		return err
	}

	authToken := c.Get("Authorization")

	wishlistsData, paging, err := _i.wishlistService.GetByUserId(authToken, paginate)
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Wishlist successfully retrieved"},
		Data:     wishlistsData,
		Meta:     paging,
	})
}

// AddToWishlist Wishlist
// @Summary      Add ebook to wishlist
// @Description  API for adding ebook to wishlist
// @Tags         Ebook Wishlist
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        ebookId path int true "Ebook ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ebooks/wishlist/{ebookId} [post]
func (_i *ebookWishlistsController) AddToWishlist(c *fiber.Ctx) error {
	ebookId, err := strconv.ParseUint(c.Params("ebookId"), 10, 0)
	if err != nil {
		return err
	}

	authToken := c.Get("Authorization")

	err = _i.wishlistService.AddToWishlist(authToken, uint(ebookId))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Ebook successfully added to wishlist"},
	})
}

// RemoveFromWishlist Wishlist
// @Summary      Remove ebook from wishlist
// @Description  API for removing ebook from wishlist
// @Tags         Ebook Wishlist
// @Security     Bearer
// @Param        X-Csrf-Token header string true "Insert the X-Csrf-Token"
// @Param        ebookId path int true "Ebook ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ebooks/wishlist/{ebookId} [delete]
func (_i *ebookWishlistsController) RemoveFromWishlist(c *fiber.Ctx) error {
	ebookId, err := strconv.ParseUint(c.Params("ebookId"), 10, 0)
	if err != nil {
		return err
	}

	authToken := c.Get("Authorization")

	err = _i.wishlistService.RemoveFromWishlist(authToken, uint(ebookId))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Ebook successfully removed from wishlist"},
	})
}

// IsInWishlist Wishlist
// @Summary      Check if ebook is in wishlist
// @Description  API for checking if ebook is in wishlist
// @Tags         Ebook Wishlist
// @Security     Bearer
// @Param        ebookId path int true "Ebook ID"
// @Success      200  {object}  response.Response
// @Failure      400  {object}  response.BadRequestError
// @Failure      401  {object}  response.UnauthorizedError
// @Failure      500  {object}  response.InternalServerError
// @Router       /ebooks/wishlist/check/{ebookId} [get]
func (_i *ebookWishlistsController) IsInWishlist(c *fiber.Ctx) error {
	ebookId, err := strconv.ParseUint(c.Params("ebookId"), 10, 0)
	if err != nil {
		return err
	}

	authToken := c.Get("Authorization")

	isInWishlist, err := _i.wishlistService.IsInWishlist(authToken, uint(ebookId))
	if err != nil {
		return err
	}

	return utilRes.Resp(c, utilRes.Response{
		Success:  true,
		Messages: utilRes.Messages{"Wishlist status successfully retrieved"},
		Data:     map[string]bool{"isInWishlist": isInWishlist},
	})
}
