package handler

import (
	"strconv"

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

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

type CoffeeHandler struct {
	coffeeService *service.CoffeeService
}

func NewCoffeeHandler(coffeeService *service.CoffeeService) *CoffeeHandler {
	return &CoffeeHandler{coffeeService: coffeeService}
}

func (h *CoffeeHandler) GetCoffees(c *gin.Context) {
	coffees, err := h.coffeeService.GetAllCoffees()
	if err != nil {
		Error(c, err)
		return
	}

	Success(c, coffees)
}

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

	coffee, err := h.coffeeService.GetCoffeeByID(id)
	if err != nil {
		Error(c, err)
		return
	}

	Success(c, coffee)
}
