package api

import (
	"net/http"
	"sort"

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

func (api *Api) ListMPs(w http.ResponseWriter, r *http.Request) {
	members, err := api.App.Parlament.ListNRMembers("XXVIII")
	if err != nil {
		api.ServerErrorResponse(w, r, err)
		return
	}

	sort.Slice(members, func(i, j int) bool {
		if members[i].FractionName != members[j].FractionName {
			return members[i].FractionName < members[j].FractionName
		}
		return members[i].LastName < members[j].LastName
	})

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