package api

import (
	"errors"
	"net/http"
	"strings"

	"github.com/julienschmidt/httprouter"
)

func (api *Api) GetParlVoteDetail(w http.ResponseWriter, r *http.Request) {
	ps := httprouter.ParamsFromContext(r.Context())
	routePath := ps.ByName("path") // e.g. "/XXVIII/I/14"
	routePath = strings.TrimPrefix(routePath, "/")
	if routePath == "" {
		api.BadRequestResponse(w, r, errors.New("path is required"))
		return
	}

	path := "/gegenstand/" + routePath

	detail, err := api.App.Parlament.GetDocumentVote(path)
	if err != nil {
		api.ServerErrorResponse(w, r, err)
		return
	}
	if detail == nil {
		api.NotFoundResponse(w, r)
		return
	}

	if err := api.writeJSON(w, http.StatusOK, envelope{"vote": detail}, nil); err != nil {
		api.ServerErrorResponse(w, r, err)
	}
}
