package engineer

import (
	"encoding/json"
	"testing"
	"time"

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

func TestEngineerJSONNestsDispatchState(t *testing.T) {
	t.Parallel()

	dispatchedAt := time.Date(2026, 7, 10, 6, 0, 0, 0, time.UTC)
	payload, err := json.Marshal(Engineer{
		Email: "alice@example.com",
		DispatchState: DispatchState{
			DispatchEnabled:     true,
			LastDispatchedAt:    &dispatchedAt,
			LastDispatchedIssue: "DM-123",
		},
	})
	require.NoError(t, err)

	var body map[string]any
	require.NoError(t, json.Unmarshal(payload, &body))
	_, ok := body["dispatch_enabled"]
	require.False(t, ok)
	_, ok = body["last_assigned_at"]
	require.False(t, ok)
	dispatchState, ok := body["dispatch_state"].(map[string]any)
	require.True(t, ok)
	require.Equal(t, true, dispatchState["dispatch_enabled"])
	require.Equal(t, "DM-123", dispatchState["last_dispatched_issue"])
}
