package bunmodel

import (
	"fmt"
	"time"

	"gitea.dwysokinski.me/twhelp/corev3/internal/domain"
	"github.com/uptrace/bun"
)

type TribeChange struct {
	bun.BaseModel `bun:"table:tribe_changes,alias:tc"`

	ID         int       `bun:"id,pk,autoincrement,identity"`
	ServerKey  string    `bun:"server_key,nullzero"`
	PlayerID   int       `bun:"player_id,nullzero"`
	Player     Player    `bun:"player,rel:belongs-to,join:player_id=id,join:server_key=server_key"`
	NewTribeID int       `bun:"new_tribe_id,nullzero"`
	NewTribe   Tribe     `bun:"new_tribe,rel:belongs-to,join:new_tribe_id=id,join:server_key=server_key"`
	OldTribeID int       `bun:"old_tribe_id,nullzero"`
	OldTribe   Tribe     `bun:"old_tribe,rel:belongs-to,join:old_tribe_id=id,join:server_key=server_key"`
	CreatedAt  time.Time `bun:"created_at,nullzero"`
}

func (tc TribeChange) ToDomain() (domain.TribeChange, error) {
	converted, err := domain.UnmarshalTribeChangeFromDatabase(
		tc.ID,
		tc.ServerKey,
		tc.PlayerID,
		tc.OldTribeID,
		tc.NewTribeID,
		tc.CreatedAt,
	)
	if err != nil {
		return domain.TribeChange{}, fmt.Errorf(
			"couldn't construct domain.TribeChange (id=%d): %w",
			tc.ID,
			err,
		)
	}

	return converted, nil
}

type TribeChanges []TribeChange

func (tcs TribeChanges) ToDomain() (domain.TribeChanges, error) {
	res := make(domain.TribeChanges, 0, len(tcs))

	for _, tc := range tcs {
		converted, err := tc.ToDomain()
		if err != nil {
			return nil, err
		}

		res = append(res, converted)
	}

	return res, nil
}
