package apimodel

import (
	"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
	oapitypes "github.com/oapi-codegen/runtime/types"
)

func NewPlayerSnapshot(withRelations domain.PlayerSnapshotWithRelations) PlayerSnapshot {
	ps := withRelations.PlayerSnapshot()
	return PlayerSnapshot{
		Date:              oapitypes.Date{Time: ps.Date()},
		Id:                ps.ID(),
		NumVillages:       ps.NumVillages(),
		OpponentsDefeated: NewPlayerOpponentsDefeated(ps.OD()),
		Player:            NewPlayerMeta(withRelations.Player().WithRelations(withRelations.Tribe())),
		Points:            ps.Points(),
		Rank:              ps.Rank(),
	}
}

func NewListPlayerSnapshotsResponse(res domain.ListPlayerSnapshotsWithRelationsResult) ListPlayerSnapshotsResponse {
	pss := res.PlayerSnapshots()

	resp := ListPlayerSnapshotsResponse{
		Data: make([]PlayerSnapshot, 0, len(pss)),
		Cursor: Cursor{
			Next: res.Next().Encode(),
			Self: res.Self().Encode(),
		},
	}

	for _, ps := range pss {
		resp.Data = append(resp.Data, NewPlayerSnapshot(ps))
	}

	return resp
}
