package apimodel

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

func NewTribeSnapshot(withRelations domain.TribeSnapshotWithRelations) TribeSnapshot {
	ts := withRelations.TribeSnapshot()
	return TribeSnapshot{
		AllPoints:         ts.AllPoints(),
		Date:              oapitypes.Date{Time: ts.Date()},
		Dominance:         ts.Dominance(),
		Id:                ts.ID(),
		NumMembers:        ts.NumMembers(),
		NumVillages:       ts.NumVillages(),
		OpponentsDefeated: NewTribeOpponentsDefeated(ts.OD()),
		Points:            ts.Points(),
		Rank:              ts.Rank(),
		Tribe:             NewTribeMeta(withRelations.Tribe()),
	}
}

func NewListTribeSnapshotsResponse(res domain.ListTribeSnapshotsWithRelationsResult) ListTribeSnapshotsResponse {
	tss := res.TribeSnapshots()

	resp := ListTribeSnapshotsResponse{
		Data: make([]TribeSnapshot, 0, len(tss)),
		Cursor: Cursor{
			Next: res.Next().Encode(),
			Self: res.Self().Encode(),
		},
	}

	for _, ts := range tss {
		resp.Data = append(resp.Data, NewTribeSnapshot(ts))
	}

	return resp
}
