package apimodel

import (
	"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
)

func NewEnnoblement(withRelations domain.EnnoblementWithRelations) Ennoblement {
	e := withRelations.Ennoblement()
	return Ennoblement{
		CreatedAt: e.CreatedAt(),
		Id:        e.ID(),
		Points:    e.Points(),
		Village: NewVillageMeta(
			withRelations.Village().WithRelations(withRelations.OldOwner().WithRelations(withRelations.OldTribe())),
		),
		NewOwner: NewPlayerMeta(withRelations.NewOwner().WithRelations(withRelations.NewTribe())),
	}
}

func NewListEnnoblementsResponse(res domain.ListEnnoblementsWithRelationsResult) ListEnnoblementsResponse {
	ennoblements := res.Ennoblements()

	resp := ListEnnoblementsResponse{
		Data: make([]Ennoblement, 0, len(ennoblements)),
		Cursor: Cursor{
			Next: res.Next().Encode(),
			Self: res.Self().Encode(),
		},
	}

	for _, e := range ennoblements {
		resp.Data = append(resp.Data, NewEnnoblement(e))
	}

	return resp
}
