package api

import (
	"net/http"
	"strings"

	"party.at/party/cmd/party/common"
	"party.at/party/internal/data"
)

func (api *Api) GetParlVoteDetail(w http.ResponseWriter, r *http.Request) {
	routePath := r.PathValue("path")
	routePath = strings.TrimPrefix(routePath, "/")
	if routePath == "" {
		api.errorResponse(w, r, data.ErrBadRequest)
		
		return
	}

	path := "/gegenstand/" + routePath

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

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