package litigationplanner

import (
	"encoding/json"
	"strings"
	"testing"
)

// TestFristenrechnerType_WireShapeIncludesID is the regression test for
// t-paliad-345: the /api/tools/proceeding-types JSON response must
// include `id` so frontend pickers (Litigation Builder add-proceeding,
// fristenrechner-wizard project prefill) can POST proceeding_type_id
// directly without a code→id round trip. When the id was missing the
// Litigation Builder "+ Verfahren hinzufügen" button silently dropped
// the proceeding_type_id from the POST body (JSON.stringify omits
// undefined keys), the server rejected with 400, and the client
// swallowed the error — user-visible symptom was "nothing happens".
func TestFristenrechnerType_WireShapeIncludesID(t *testing.T) {
	in := FristenrechnerType{
		ID:     42,
		Code:   "upc.inf.cfi",
		Name:   "UPC Verletzungsverfahren",
		NameEN: "UPC Infringement Action",
		Group:  "UPC",
	}
	b, err := json.Marshal(in)
	if err != nil {
		t.Fatalf("marshal: %v", err)
	}
	got := string(b)
	if !strings.Contains(got, `"id":42`) {
		t.Errorf("missing id in wire shape: %s", got)
	}
	for _, want := range []string{`"code":"upc.inf.cfi"`, `"nameEN":"UPC Infringement Action"`} {
		if !strings.Contains(got, want) {
			t.Errorf("missing %q in wire shape: %s", want, got)
		}
	}

	// Round-trip — a client that posts the id back to /api/builder/
	// scenarios/{id}/proceedings should see it preserved as an integer
	// (paliad.scenario_proceedings.proceeding_type_id is INT, not UUID).
	var out FristenrechnerType
	if err := json.Unmarshal(b, &out); err != nil {
		t.Fatalf("unmarshal: %v", err)
	}
	if out.ID != 42 {
		t.Errorf("id lost on round-trip: got %d want 42", out.ID)
	}
}
