package taskboard

import (
	"encoding/json"
	"testing"

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

func TestNormalizeTaskBoardViewStatePreservesWorkspaceFilters(t *testing.T) {
	normalized := normalizeTaskBoardViewState(TaskBoardViewState{
		StructuredFilters: map[string][]string{
			"status":  {"todo", "done"},
			"channel": {"chn-1"},
		},
	})

	require.Equal(t, map[string][]string{
		"status":  {"todo", "done"},
		"channel": {"chn-1"},
	}, normalized.StructuredFilters)
}

func TestTaskBoardViewStateRoundTripsDeprecatedCardPropertyToggles(t *testing.T) {
	raw := []byte(`{
		"view":"board",
		"searchQuery":"",
		"quickFilters":[],
		"structuredFilters":{},
		"sortMode":"manual",
		"cardPropertyToggles":["assignee","priority"],
		"doneCollapsed":false
	}`)

	state, err := unmarshalTaskBoardViewStateJSON(raw)
	require.NoError(t, err)
	require.Equal(t, []string{"assignee", "priority"}, state.CardPropertyToggles)

	encoded, err := marshalTaskBoardViewStateJSON(normalizeTaskBoardViewState(state))
	require.NoError(t, err)
	var payload map[string]any
	require.NoError(t, json.Unmarshal(encoded, &payload))
	require.Equal(t, []any{"assignee", "priority"}, payload["cardPropertyToggles"])
}

func TestTaskBoardViewStatePreservesDeprecatedCardPropertiesWhenNewClientOmitsThem(t *testing.T) {
	next := normalizeTaskBoardViewState(TaskBoardViewState{View: "list"})
	existing := TaskBoardViewState{
		CardPropertyToggles: []string{"assignee", "priority"},
	}

	preserved := preserveTaskBoardCardPropertyToggles(next, existing)

	require.Equal(t, []string{"assignee", "priority"}, preserved.CardPropertyToggles)
}
