package handlers

import (
	"log/slog"
	"net/http"

	"github.com/CubeCraft-Creations/Extrudex/backend/internal/dtos"
	"github.com/CubeCraft-Creations/Extrudex/backend/internal/services"
)

// PrinterHandler handles HTTP requests for printer listings.
type PrinterHandler struct {
	service *services.PrinterService
}

// NewPrinterHandler creates a PrinterHandler with the given service.
func NewPrinterHandler(service *services.PrinterService) *PrinterHandler {
	return &PrinterHandler{service: service}
}

// List handles GET /api/printers — returns all printers with printer_type info.
func (h *PrinterHandler) List(w http.ResponseWriter, r *http.Request) {
	printers, err := h.service.List(r.Context())
	if err != nil {
		slog.Error("failed to list printers", "error", err)
		writeJSON(w, http.StatusInternalServerError, dtos.ErrorResponse{
			Error: "internal server error",
			Code:  http.StatusInternalServerError,
		})
		return
	}

	writeJSON(w, http.StatusOK, dtos.SingleResponse{Data: printers})
}
