package web

import (
	"net/http"

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

func (web *Web) ParlamentDispatch(w http.ResponseWriter, r *http.Request) {
	p := r.PathValue("path")
	if p == "rows" {
		web.ParlVotesRowsFragment(w, r)
		return
	}
	web.ParlVoteDetailPage(w, r)
}

func (web *Web) ParlVoteDetailPage(w http.ResponseWriter, r *http.Request) {
	routePath := r.PathValue("path") // e.g. "/XXVIII/I/14"
	if routePath == "" || routePath == "/" {
		http.Error(w, "Bad Request", http.StatusBadRequest)
		return
	}
	path := "/gegenstand/" + routePath

	detail, err := web.App.Parlament.GetDocumentVote(path)
	if err != nil {
		web.App.LogError(r, err)
		http.Error(w, "Internal Server Error", http.StatusInternalServerError)
		return
	}
	if detail == nil {
		http.Error(w, "Not Found", http.StatusNotFound)
		return
	}

	web.render(w, r, http.StatusOK, "parlvotedetail", struct {
		AuthenticatedUser *data.User
		Detail            *parlament.DocumentVoteDetail
		Path              string
	}{
		AuthenticatedUser: common.GetUser(r),
		Detail:            detail,
		Path:              path,
	})
}
