package store

import (
	"testing"

	"git.ctrlz.es/mgdelacroix/craban/model"

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

func TestCreateGame(t *testing.T) {
	th, teardown := NewTestHelper(t)
	defer teardown()

	user := th.NewUser()
	gameName := "Awesome Game"

	var game *model.Game
	var gameMember *model.GameMember
	var err error

	t.Run("should correctly create a game", func(t *testing.T) {
		game, err = th.store.Game().Create(gameName, user.ID)
		require.NoError(t, err)
		require.NotZero(t, game.ID)
		require.Equal(t, gameName, game.Name)
		require.Equal(t, user.ID, game.UserID)
	})

	t.Run("should correctly fetch game members", func(t *testing.T) {
		gameMembers, err := th.store.Game().GetMembers(game.ID)
		require.NoError(t, err)
		require.Len(t, gameMembers, 1)
		require.Equal(t, game.ID, gameMembers[0].GameID)
		require.Equal(t, user.ID, gameMembers[0].UserID)
		require.Equal(t, model.RoleGameMaster, gameMembers[0].Role)
	})

	t.Run("should correctly fetch member for user", func(t *testing.T) {
		gameMember, err = th.store.Game().GetMember(game.ID, user.ID)
		require.NoError(t, err)
		require.Equal(t, game.ID, gameMember.GameID)
		require.Equal(t, user.ID, gameMember.UserID)
		require.Equal(t, model.RoleGameMaster, gameMember.Role)
	})

	user2 := th.NewUser()
	t.Run("should correctly add a new member as a player", func(t *testing.T) {
		gameMember2, err := th.store.Game().AddMember(game.ID, user2.ID, model.RolePlayer)
		require.NoError(t, err)
		require.Equal(t, game.ID, gameMember2.GameID)
		require.Equal(t, user2.ID, gameMember2.UserID)
		require.Equal(t, model.RolePlayer, gameMember2.Role)
	})

	user3 := th.NewUser()
	t.Run("should correctly add a new member as a game master", func(t *testing.T) {
		gameMember3, err := th.store.Game().AddMember(game.ID, user3.ID, model.RoleGameMaster)
		require.NoError(t, err)
		require.Equal(t, game.ID, gameMember3.GameID)
		require.Equal(t, user3.ID, gameMember3.UserID)
		require.Equal(t, model.RoleGameMaster, gameMember3.Role)
	})

	t.Run("should correctly fetch all members for game", func(t *testing.T) {
		gameMembers, err := th.store.Game().GetMembers(game.ID)
		require.NoError(t, err)
		require.Len(t, gameMembers, 3)
	})
}
