package handlers

import (
	"net/http"
	"net/http/httptest"
	"testing"
)

// U4 (m/paliad#151) — both legacy URLs hard-cut to /tools/procedures
// with HTTP 301. Query strings carry through so bookmarks like
// /tools/fristenrechner?event=upc.inf.cfi.soc&trigger_date=2026-04-01
// resolve to /tools/procedures?event=…&trigger_date=… without losing
// the user's intent.
func TestLegacyToolsPagesRedirect(t *testing.T) {
	cases := []struct {
		name    string
		path    string
		handler func(http.ResponseWriter, *http.Request)
		wantLoc string
	}{
		{
			name:    "fristenrechner naked",
			path:    "/tools/fristenrechner",
			handler: handleFristenrechnerPage,
			wantLoc: "/tools/procedures",
		},
		{
			name:    "fristenrechner with query",
			path:    "/tools/fristenrechner?event=upc.inf.cfi.soc&trigger_date=2026-04-01",
			handler: handleFristenrechnerPage,
			wantLoc: "/tools/procedures?event=upc.inf.cfi.soc&trigger_date=2026-04-01",
		},
		{
			name:    "verfahrensablauf naked",
			path:    "/tools/verfahrensablauf",
			handler: handleVerfahrensablaufPage,
			wantLoc: "/tools/procedures",
		},
		{
			name:    "verfahrensablauf with proceeding",
			path:    "/tools/verfahrensablauf?proceeding=upc.inf.cfi&side=claimant",
			handler: handleVerfahrensablaufPage,
			wantLoc: "/tools/procedures?proceeding=upc.inf.cfi&side=claimant",
		},
	}
	for _, tc := range cases {
		t.Run(tc.name, func(t *testing.T) {
			req := httptest.NewRequest(http.MethodGet, tc.path, nil)
			w := httptest.NewRecorder()
			tc.handler(w, req)
			if w.Code != http.StatusMovedPermanently {
				t.Fatalf("status = %d, want %d", w.Code, http.StatusMovedPermanently)
			}
			if got := w.Header().Get("Location"); got != tc.wantLoc {
				t.Fatalf("Location = %q, want %q", got, tc.wantLoc)
			}
		})
	}
}
