package User

import (
	"net/http"

	storedto "astrology-api/dto/store"
	"astrology-api/services"

	"github.com/gin-gonic/gin"
)

type StoreProductController struct {
	service *services.StoreProductService
}

func NewStoreProductController() *StoreProductController {
	return &StoreProductController{
		service: services.NewStoreProductService(),
	}
}

func (c *StoreProductController) GetProducts(ctx *gin.Context) {

	var req storedto.ProductListRequest

	if err := ctx.ShouldBindJSON(&req); err != nil {

		ctx.JSON(http.StatusBadRequest, gin.H{
			"status": 400,
			"message": err.Error(),
		})
		return
	}

	response, err := c.service.GetProducts(req)

	if err != nil {

		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status": 500,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, response)
}

func (c *StoreProductController) GetProductDetail(ctx *gin.Context) {

	var req storedto.ProductDetailRequest

	if err := ctx.ShouldBindJSON(&req); err != nil {

		ctx.JSON(http.StatusBadRequest, gin.H{
			"status": 400,
			"message": err.Error(),
		})
		return
	}

	response, err := c.service.GetProductDetail(req)

	if err != nil {

		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status": 500,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, response)
}


func (c *StoreProductController) SearchProducts(ctx *gin.Context) {

	var req storedto.SearchProductRequest

	if err := ctx.ShouldBindJSON(&req); err != nil {

		ctx.JSON(http.StatusBadRequest, gin.H{
			"status": 400,
			"message": err.Error(),
		})
		return
	}

	response, err := c.service.SearchProducts(req)

	if err != nil {

		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status": 500,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, response)
}

func (c *StoreProductController) GetSimilarProducts(ctx *gin.Context) {

	var req storedto.SimilarProductRequest

	if err := ctx.ShouldBindJSON(&req); err != nil {

		ctx.JSON(http.StatusBadRequest, gin.H{
			"status": 400,
			"message": err.Error(),
		})
		return
	}

	response, err := c.service.GetSimilarProducts(req)

	if err != nil {

		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status": 500,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, response)
}

func (c *StoreProductController) GetProductReviews(ctx *gin.Context) {

	var req storedto.ProductReviewRequest

	if err := ctx.ShouldBindJSON(&req); err != nil {

		ctx.JSON(http.StatusBadRequest, gin.H{
			"status": 400,
			"message": err.Error(),
		})
		return
	}

	response, err := c.service.GetProductReviews(req)

	if err != nil {

		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status": 500,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, response)
}


func (c *StoreProductController) GetProductFAQs(ctx *gin.Context) {

	var req storedto.ProductFAQRequest

	if err := ctx.ShouldBindJSON(&req); err != nil {

		ctx.JSON(http.StatusBadRequest, gin.H{
			"status": 400,
			"message": err.Error(),
		})
		return
	}

	response, err := c.service.GetProductFAQs(req)

	if err != nil {

		ctx.JSON(http.StatusInternalServerError, gin.H{
			"status": 500,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(http.StatusOK, response)
}
