package controller_test

import (
	"slices"
	"testing"

	"galaxy/game/internal/model/game"

	"github.com/stretchr/testify/assert"
)

func TestListMoveableGroupIds(t *testing.T) {
	c, g := newCache()

	// 1: idx = 0 / [v] Non-Fleet group
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10))

	// 2: idx = 1 / [v] In-Fleet group
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 1))
	// 3: idx = 2 / [v] In-Fleet group
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10))

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, "Fleet", c.ShipGroup(1).ID))
	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, "Fleet", c.ShipGroup(2).ID))

	// 4: idx = 3 / [v] In_Space
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
	c.ShipGroup(3).StateInSpace = &InSpace

	// 5: idx = 4 / [x] In_Upgrage
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7))
	c.ShipGroup(4).StateUpgrade = &game.InUpgrade{
		UpgradeTech: []game.UpgradePreference{},
	}

	// 6: idx = 5 / [v] Just launched group
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 10))
	assert.NoError(t, g.ShipGroupSend(Race_0.Name, c.ShipGroup(5).ID, R0_Planet_2_num))

	movableGroups := slices.Collect(c.ListMoveableGroupIds())
	assert.Len(t, movableGroups, 5)
	for _, i := range movableGroups {
		sg := c.ShipGroup(i)
		assert.NotEqual(t, game.StateUpgrade, sg.State())
		assert.NotEqual(t, game.StateTransfer, sg.State())
	}
}
