package api

import (
	"net/http"
	"strconv"
	"github.com/gorilla/mux"
)

func (h *Handler) ListBlocks(w http.ResponseWriter, r *http.Request) {
	blocks, err := h.Store.ListBlocks()
	if err != nil {
		WriteError(w, http.StatusInternalServerError, "Erro ao listar blocos")
		return
	}
	WriteJSON(w, blocks)
}

func (h *Handler) GetBlockByHeight(w http.ResponseWriter, r *http.Request) {
	heightStr := mux.Vars(r)["height"]
	height, err := strconv.Atoi(heightStr)
	if err != nil {
		WriteError(w, http.StatusBadRequest, "Altura inválida")
		return
	}
	block, err := h.Store.LoadBlock(height)
	if err != nil {
		WriteError(w, http.StatusNotFound, "Bloco não encontrado")
		return
	}
	WriteJSON(w, block)
}