package apimodel

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

func NewTribeOpponentsDefeated(od domain.OpponentsDefeated) TribeOpponentsDefeated {
	return TribeOpponentsDefeated{
		RankAtt:    od.RankAtt(),
		RankDef:    od.RankDef(),
		RankTotal:  od.RankTotal(),
		ScoreAtt:   od.ScoreAtt(),
		ScoreDef:   od.ScoreDef(),
		ScoreTotal: od.ScoreTotal(),
	}
}

func NewTribe(t domain.Tribe) Tribe {
	converted := Tribe{
		AllPoints:         t.AllPoints(),
		BestRank:          t.BestRank(),
		BestRankAt:        t.BestRankAt(),
		CreatedAt:         t.CreatedAt(),
		Dominance:         t.Dominance(),
		Id:                t.ID(),
		MostPoints:        t.MostPoints(),
		MostPointsAt:      t.MostPointsAt(),
		MostVillages:      t.MostVillages(),
		MostVillagesAt:    t.MostVillagesAt(),
		Name:              t.Name(),
		NumMembers:        t.NumMembers(),
		NumVillages:       t.NumVillages(),
		Points:            t.Points(),
		ProfileUrl:        t.ProfileURL().String(),
		Rank:              t.Rank(),
		OpponentsDefeated: NewTribeOpponentsDefeated(t.OD()),
		Tag:               t.Tag(),
	}

	if deletedAt := t.DeletedAt(); !deletedAt.IsZero() {
		converted.DeletedAt = &deletedAt
	}

	return converted
}

func NewListTribesResponse(res domain.ListTribesResult) ListTribesResponse {
	tribes := res.Tribes()

	resp := ListTribesResponse{
		Data: make([]Tribe, 0, len(tribes)),
		Cursor: Cursor{
			Next: res.Next().Encode(),
			Self: res.Self().Encode(),
		},
	}

	for _, t := range tribes {
		resp.Data = append(resp.Data, NewTribe(t))
	}

	return resp
}

func NewGetTribeResponse(t domain.Tribe) GetTribeResponse {
	return GetTribeResponse{
		Data: NewTribe(t),
	}
}
