package api

import (
	"net/http"
	"sort"
)

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 := api.writeJSON(w, http.StatusOK, envelope{
		"members": members,
		"total":   len(members),
	}, nil); err != nil {
		api.ServerErrorResponse(w, r, err)
	}
}
