package integrationtests

import (
	"testing"

	"github.com/mattermost/focalboard/server/model"
	"github.com/mattermost/focalboard/server/utils"
	"github.com/stretchr/testify/require"
)

func TestSharing(t *testing.T) {
	th := SetupTestHelper(t).InitBasic()
	defer th.TearDown()

	var boardID string
	token := utils.NewID(utils.IDTypeToken)

	t.Run("an unauthenticated client should not be able to get a sharing", func(t *testing.T) {
		th.Logout(th.Client)

		sharing, resp := th.Client.GetSharing("board-id")
		th.CheckUnauthorized(resp)
		require.Nil(t, sharing)
	})

	t.Run("Check no initial sharing", func(t *testing.T) {
		th.Login1()

		teamID := "0"
		newBoard := &model.Board{
			TeamID: teamID,
			Type:   model.BoardTypeOpen,
		}

		board, err := th.Server.App().CreateBoard(newBoard, th.GetUser1().ID, true)
		require.NoError(t, err)
		require.NotNil(t, board)
		boardID = board.ID

		s, err := th.Server.App().GetSharing(boardID)
		require.NoError(t, err)
		require.Nil(t, s)

		sharing, resp := th.Client.GetSharing(boardID)
		require.NoError(t, resp.Error)
		require.NotNil(t, sharing)
		require.False(t, sharing.Enabled)
		require.Empty(t, sharing.ID)
	})

	t.Run("POST sharing, config = false", func(t *testing.T) {
		sharing := model.Sharing{
			ID:       boardID,
			Token:    token,
			Enabled:  true,
			UpdateAt: 1,
		}

		// it will fail with default config
		success, resp := th.Client.PostSharing(&sharing)
		require.False(t, success)
		require.Error(t, resp.Error)

		t.Run("GET sharing", func(t *testing.T) {
			sharing, resp := th.Client.GetSharing(boardID)
			// Expect empty sharing object
			require.NoError(t, resp.Error)
			require.NotNil(t, sharing)
			require.False(t, sharing.Enabled)
			require.Empty(t, sharing.ID)
			require.Empty(t, sharing.Token)
		})
	})

	t.Run("POST sharing, config = true", func(t *testing.T) {
		th.Server.Config().EnablePublicSharedBoards = true
		sharing := model.Sharing{
			ID:       boardID,
			Token:    token,
			Enabled:  true,
			UpdateAt: 1,
		}

		// it will succeed with updated config
		success, resp := th.Client.PostSharing(&sharing)
		require.True(t, success)
		require.NoError(t, resp.Error)

		t.Run("GET sharing", func(t *testing.T) {
			sharing, resp := th.Client.GetSharing(boardID)
			require.NoError(t, resp.Error)
			require.NotNil(t, sharing)
			require.Equal(t, sharing.ID, boardID)
			require.True(t, sharing.Enabled)
			require.Equal(t, sharing.Token, token)
		})
	})
}
