package monitor

import (
	"testing"

	"gitea.dooplex.hu/admin/felhom-hub/internal/offsite"
	"gitea.dooplex.hu/admin/felhom-hub/internal/offsiteheal"
)

// R-192's guard half, closed by REPLACEMENT (R-204 item 4).
//
// The counting guard inferred the situation from how many of the OLDEST 500 reports after a consume
// carried an offbox target. On demo-hp all 500 predated the rebuild, so the checker confidently
// reported the REGRESSED shape and declined to heal — for 108 reports, while the box sat stranded.
// A declaration needs no window and no count, and it OUTRANKS both inferred shapes.

func TestShapeOf_DeclarationOutranksBothInferredShapes(t *testing.T) {
	cases := []struct {
		name     string
		status   offsite.DeliveryStatus
		declared bool
		want     deliveryShape
	}{
		{"burned, undeclared", offsite.DeliveryStatus{OffsiteReportsSinceConsume: 0, ReportsSinceConsume: 9}, false, shapeBurned},
		{"regressed, undeclared", offsite.DeliveryStatus{OffsiteReportsSinceConsume: 500, ReportsSinceConsume: 500}, false, shapeRegressed},
		// THE demo-hp SHAPE: the counts say "regressed" from a window that predates the rebuild, and
		// the box says it needs a credential. The declaration wins.
		{"regressed counts BUT the box declares", offsite.DeliveryStatus{OffsiteReportsSinceConsume: 500, ReportsSinceConsume: 500}, true, shapeDeclared},
		{"burned counts AND the box declares", offsite.DeliveryStatus{OffsiteReportsSinceConsume: 0, ReportsSinceConsume: 9}, true, shapeDeclared},
	}
	for _, tc := range cases {
		if got := shapeOf(tc.status, tc.declared); got != tc.want {
			t.Errorf("%s: shapeOf = %q, want %q", tc.name, got, tc.want)
		}
	}
}

// The declared-state string is duplicated in three packages (the controller declares it, this checker
// recognises it, the reconciler acts on it). A silent drift between them would make the whole feature
// inert with every test still green — so the two hub-side copies are pinned to each other here. The
// controller's copy is pinned by its own report-shape test and by the live validation.
func TestDeclaredStateStringMatchesTheReconciler(t *testing.T) {
	if declaredNeedsCredential != offsiteheal.StateNeedsCredential {
		t.Fatalf("declared-state drift: monitor has %q, offsiteheal has %q — the checker would never stand down and both mechanisms would heal the same customer",
			declaredNeedsCredential, offsiteheal.StateNeedsCredential)
	}
}
