package handler

import (
	"strconv"

	"awesome-back/internal/service"
	"awesome-back/pkg/errors"

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

type PastryHandler struct {
	pastryService *service.PastryService
}

func NewPastryHandler(pastryService *service.PastryService) *PastryHandler {
	return &PastryHandler{pastryService: pastryService}
}

func (h *PastryHandler) GetPastries(c *gin.Context) {
	pastries, err := h.pastryService.GetAllPastries()
	if err != nil {
		Error(c, err)
		return
	}

	Success(c, pastries)
}

func (h *PastryHandler) GetPastryByID(c *gin.Context) {
	idStr := c.Param("id")
	id, err := strconv.Atoi(idStr)
	if err != nil {
		Error(c, errors.NewInvalidError("invalid pastry ID format"))
		return
	}

	pastry, err := h.pastryService.GetPastryByID(id)
	if err != nil {
		Error(c, err)
		return
	}

	Success(c, pastry)
}

func (h *PastryHandler) GetPastriesByCategory(c *gin.Context) {
	category := c.Param("category")
	if category == "" {
		Error(c, errors.NewInvalidError("category parameter is required"))
		return
	}

	pastries, err := h.pastryService.GetPastriesByCategory(category)
	if err != nil {
		Error(c, err)
		return
	}

	Success(c, pastries)
}
