package handlers

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

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

// MaterialHandler handles requests for material lookup data.
type MaterialHandler struct {
	repo *repositories.MaterialRepository
}

// NewMaterialHandler creates a MaterialHandler with the given repository.
func NewMaterialHandler(repo *repositories.MaterialRepository) *MaterialHandler {
	return &MaterialHandler{repo: repo}
}

// List handles GET /api/materials — returns all material bases.
func (h *MaterialHandler) List(w http.ResponseWriter, r *http.Request) {
	materials, err := h.repo.GetAll(r.Context())
	if err != nil {
		slog.Error("failed to list materials", "error", err)
		writeJSON(w, http.StatusInternalServerError, dtos.ErrorResponse{
			Error: "internal server error",
			Code:  http.StatusInternalServerError,
		})
		return
	}

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