package controller_test

import (
	"math"
	"slices"
	"testing"

	e "galaxy/error"

	"galaxy/game/internal/model/game"

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

func TestShipGroupJoinFleet(t *testing.T) {
	c, g := newCache()
	groupIndex := uuid.Nil

	fleetOne := "R0_Fleet_one"
	fleetTwo := "R0_Fleet_two"

	assert.ErrorContains(t,
		g.ShipGroupJoinFleet(Race_0.Name, BadEntityName, groupIndex),
		e.GenericErrorText(e.ErrInputEntityTypeNameInvalid))

	assert.ErrorContains(t,
		g.ShipGroupJoinFleet(Race_0.Name, "Unnamed", groupIndex),
		e.GenericErrorText(e.ErrInputEntityNotExists))

	// creating ShipGroup
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 5))
	groupIndex = c.ShipGroup(0).ID

	assert.ErrorContains(t,
		g.ShipGroupJoinFleet(UnknownRace, fleetOne, groupIndex),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ShipGroupJoinFleet(Race_Extinct.Name, fleetOne, groupIndex),
		e.GenericErrorText(e.ErrRaceExinct))

	// ensure race has no Fleets
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 0)

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetOne, groupIndex))
	fleets := slices.Collect(c.ListFleets(Race_0_idx))
	groups := slices.Collect(c.RaceShipGroups(Race_0_idx))
	assert.Len(t, groups, 1)
	gi := 0
	assert.Len(t, fleets, 1)
	assert.Equal(t, fleets[0].Name, fleetOne)
	fleetState := c.FleetState(fleets[0].ID)
	assert.Equal(t, game.StateInOrbit, fleetState.State)

	assert.NotNil(t, groups[gi].FleetID)
	assert.Equal(t, fleets[0].ID, *groups[gi].FleetID)

	// create another ShipGroup
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3))
	groupIndex = c.ShipGroup(1).ID
	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetTwo, groupIndex))
	fleets = slices.Collect(c.ListFleets(Race_0_idx))
	groups = slices.Collect(c.RaceShipGroups(Race_0_idx))
	assert.Len(t, groups, 2)
	assert.Len(t, fleets, 2)
	assert.Equal(t, fleets[1].Name, fleetTwo)
	fleetState = c.FleetState(fleets[1].ID)
	assert.Equal(t, game.StateInOrbit, fleetState.State)

	gi = 1
	assert.Len(t, groups, 2)
	assert.NotNil(t, groups[gi].FleetID)
	assert.Equal(t, fleets[1].ID, *groups[gi].FleetID)
	assert.Equal(t, uint(3), groups[gi].Number)

	groupIndex = groups[gi].ID
	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetOne, groupIndex))
	fleets = slices.Collect(c.ListFleets(Race_0_idx))
	assert.Len(t, fleets, 1)
	groups = slices.Collect(c.RaceShipGroups(Race_0_idx))
	assert.NotNil(t, groups[gi].FleetID)
	assert.Equal(t, fleets[0].ID, *groups[gi].FleetID)
	fleetState = c.FleetState(fleets[0].ID)
	assert.Equal(t, game.StateInOrbit, fleetState.State)

	// group not In_Orbit
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 7))
	gi = 2
	c.ShipGroup(gi).StateInSpace = &InSpace
	assert.ErrorContains(t,
		g.ShipGroupJoinFleet(Race_0.Name, fleetOne, c.ShipGroup(gi).ID),
		e.GenericErrorText(e.ErrShipsBusy))
	c.ShipGroup(gi).StateInSpace = nil

	// existing fleet not on the same planet or in_orbit
	c.ShipGroup(0).StateInSpace = &InSpace
	c.ShipGroup(1).StateInSpace = c.ShipGroup(0).StateInSpace
	assert.ErrorContains(t,
		g.ShipGroupJoinFleet(Race_0.Name, fleetOne, c.ShipGroup(gi).ID),
		e.GenericErrorText(e.ErrShipsNotOnSamePlanet))
}

func TestFleetMerge(t *testing.T) {
	c, g := newCache()
	// creating ShipGroup #1 at Planet_0
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 1)) // group #1
	// creating ShipGroup #2 at Planet_2
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_2_num, 2)) // group #2
	// creating ShipGroup #3 at Planet_0
	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3)) // group #3

	// ensure race has no Fleets
	assert.Len(t, slices.Collect(c.ListFleets(Race_0_idx)), 0)

	fleetOnPlanet2 := "R0_Fleet_On_Planet_2"
	fleetSourceOne := "R0_Fleet_one"
	fleetTargetTwo := "R0_Fleet_two"

	assert.ErrorContains(t,
		g.FleetMerge(Race_0.Name, fleetSourceOne, fleetTargetTwo),
		e.GenericErrorText(e.ErrInputEntityNotExists))
	assert.ErrorContains(t,
		g.FleetMerge(UnknownRace, fleetSourceOne, fleetTargetTwo),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.FleetMerge(Race_Extinct.Name, fleetSourceOne, fleetTargetTwo),
		e.GenericErrorText(e.ErrRaceExinct))

	assert.ErrorContains(t,
		g.ShipGroupJoinFleet(UnknownRace, fleetSourceOne, c.ShipGroup(0).ID),
		e.GenericErrorText(e.ErrInputUnknownRace))
	assert.ErrorContains(t,
		g.ShipGroupJoinFleet(Race_Extinct.Name, fleetSourceOne, c.ShipGroup(0).ID),
		e.GenericErrorText(e.ErrRaceExinct))

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetSourceOne, c.ShipGroup(0).ID))

	assert.ErrorContains(t,
		g.FleetMerge(Race_0.Name, fleetSourceOne, fleetTargetTwo),
		e.GenericErrorText(e.ErrInputEntityNotExists))

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetTargetTwo, c.ShipGroup(2).ID))
	assert.NoError(t, g.FleetMerge(Race_0.Name, fleetSourceOne, fleetTargetTwo))
	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleetOnPlanet2, c.ShipGroup(1).ID))

	assert.ErrorContains(t,
		g.FleetMerge(Race_0.Name, fleetOnPlanet2, fleetTargetTwo),
		e.GenericErrorText(e.ErrShipsNotOnSamePlanet))
}

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

	c.MustPlanet(R0_Planet_0_num).Material = 100.
	c.MustPlanet(R0_Planet_0_num).Capital = 100.
	fleet := "Fleet"
	var speed, mass float64

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Gunship, R0_Planet_0_num, 3)) // 1

	s := c.ShipGroup(0).Speed(c.MustShipClass(Race_0_idx, Race_0_Gunship))
	m := c.ShipGroup(0).FullMass(c.MustShipClass(Race_0_idx, Race_0_Gunship))

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 5)) // 2
	assert.NoError(t, g.ShipGroupLoad(Race_0.Name, c.ShipGroup(1).ID, "MAT", 10.))

	assert.NoError(t, c.CreateShips(Race_0_idx, Race_0_Freighter, R0_Planet_0_num, 7)) // 3
	assert.NoError(t, g.ShipGroupLoad(Race_0.Name, c.ShipGroup(2).ID, "CAP", 10.))

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleet, c.ShipGroup(0).ID))
	fleetIndex := 0
	speed, mass = c.FleetSpeedAndMass(fleetIndex)
	assert.Equal(t, s, speed)
	assert.Equal(t, m, mass)

	s = math.Min(s, c.ShipGroup(1).Speed(c.MustShipClass(Race_0_idx, Race_0_Freighter)))
	m += c.ShipGroup(1).FullMass(c.MustShipClass(Race_0_idx, Race_0_Freighter))

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleet, c.ShipGroup(1).ID))
	speed, mass = c.FleetSpeedAndMass(fleetIndex)
	assert.Equal(t, s, speed)
	assert.Equal(t, m, mass)

	s = math.Min(s, c.ShipGroup(2).Speed(c.MustShipClass(Race_0_idx, Race_0_Freighter)))
	m += c.ShipGroup(2).FullMass(c.MustShipClass(Race_0_idx, Race_0_Freighter))

	assert.NoError(t, g.ShipGroupJoinFleet(Race_0.Name, fleet, c.ShipGroup(2).ID))
	speed, mass = c.FleetSpeedAndMass(fleetIndex)
	assert.Equal(t, s, speed)
	assert.Equal(t, m, mass)
}
