package apimodel

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

func NewTribeChange(withRelations domain.TribeChangeWithRelations) TribeChange {
	tc := withRelations.TribeChange()
	return TribeChange{
		CreatedAt: tc.CreatedAt(),
		Id:        tc.ID(),
		NewTribe:  NewNullTribeMeta(withRelations.NewTribe()),
		Player:    NewPlayerMeta(withRelations.Player().WithRelations(withRelations.OldTribe())),
	}
}

func NewListTribeChangesResponse(res domain.ListTribeChangesWithRelationsResult) ListTribeChangesResponse {
	tcs := res.TribeChanges()

	resp := ListTribeChangesResponse{
		Data: make([]TribeChange, 0, len(tcs)),
		Cursor: Cursor{
			Next: res.Next().Encode(),
			Self: res.Self().Encode(),
		},
	}

	for _, tc := range tcs {
		resp.Data = append(resp.Data, NewTribeChange(tc))
	}

	return resp
}
