package web

import (
	"bytes"
	"strings"
	"testing"
)

// C8 — template smoke for the redesigned NAS page: it renders through the PRODUCTION template tree
// and uses the canonical form classes; the nonexistent classes (`form-row`/`form-input`) and the
// summary-styled-as-button hack — the root causes of the unstyled look this task fixed — must never
// come back. Companion red-proof: reintroduce `class="form-input"` on an input → FAIL.
func TestStorageNetworkTemplate_CanonicalClasses(t *testing.T) {
	s := testServer(t)
	s.loadTemplates()
	data := map[string]interface{}{
		"Page": "storage-network", "Title": "Hálózati tárhely",
		"NetAddSupport": "yes",
		"NetworkStoragePaths": []networkStorageItem{
			{Name: "media", Label: "NAS media", Protocol: "nfs", Server: "10.0.0.5", Export: "/srv/media", Path: "/mnt/felhom-drives/media", Health: "ok"},
			{Name: "ghost", Label: "Árva megosztás: ghost", Protocol: "nfs", Server: "10.0.0.5", Export: "/srv/ghost", Path: "/mnt/felhom-drives/ghost", Health: "idle", Orphan: true},
		},
	}
	var buf bytes.Buffer
	if err := s.tmpl.ExecuteTemplate(&buf, "storage_network", data); err != nil {
		t.Fatalf("render storage_network: %v", err)
	}
	html := buf.String()

	for _, want := range []string{
		`class="form-control"`,               // the canonical input class
		`class="form-group"`,                 // the canonical field wrapper
		"SMB (Synology, QNAP",                // protocol-honest ordering: SMB listed first
		"NFS (TrueNAS, Linux szerver)",       // no bare "ajánlott" claim
		"Árva",                               // the orphan badge renders
		"minden felhasználó leképezése",      // the Route-A guidance block
		"ns-hostid",                          // the live computed host-id span
		"/api/storage/netstorage/add/status", // the poll-driven progress wiring
	} {
		if !strings.Contains(html, want) {
			t.Errorf("rendered page missing %q", want)
		}
	}
	for _, banned := range []string{
		"form-row",            // nonexistent class — the old unstyled-look root cause
		"form-input",          // nonexistent class
		`<summary class="btn`, // the summary-styled-as-button hack
	} {
		if strings.Contains(html, banned) {
			t.Errorf("rendered page still contains the banned pattern %q", banned)
		}
	}
	// The orphan row renders with ONLY the remove action (no attach/migrate verbs anywhere near it).
	if !strings.Contains(html, "Eltávolítás") {
		t.Error("remove action missing")
	}
}

// --- T5 (Scenario D): capability banner replaces the add form on an old agent -------------------
// Companion red-proof: drop the {{if eq .NetAddSupport "no"}} wrapper (always render the form) →
// the banner-present + form-absent assertions on "no" fail.
func TestStorageNetworkTemplate_AgentOutdatedBanner(t *testing.T) {
	s := testServer(t)
	s.loadTemplates()
	render := func(support string) string {
		t.Helper()
		var buf bytes.Buffer
		err := s.tmpl.ExecuteTemplate(&buf, "storage_network", map[string]interface{}{
			"Page": "storage-network", "Title": "Hálózati tárhely",
			"NetAddSupport": support,
			"NetworkStoragePaths": []networkStorageItem{
				{Name: "media", Label: "NAS media", Protocol: "nfs", Server: "10.0.0.5", Export: "/srv/media", Path: "/mnt/felhom-drives/media", Health: "ok"},
			},
		})
		if err != nil {
			t.Fatalf("render (support=%s): %v", support, err)
		}
		return buf.String()
	}

	const banner = "A rendszer háttérszolgáltatása (ügynök) régebbi verziójú"
	t.Run("no → banner instead of form; list + remove intact", func(t *testing.T) {
		html := render("no")
		if !strings.Contains(html, banner) {
			t.Error("the §Part-3 banner is missing on an old agent")
		}
		if strings.Contains(html, `id="ns-add-form"`) {
			t.Error("the add form must NOT render on an old agent")
		}
		// The share list + remove action stay fully usable in every state.
		if !strings.Contains(html, "NAS media") || !strings.Contains(html, "Eltávolítás") {
			t.Error("share list/remove must still render under the banner")
		}
	})
	for _, support := range []string{"yes", "unknown"} {
		t.Run(support+" → form as today", func(t *testing.T) {
			html := render(support)
			if !strings.Contains(html, `id="ns-add-form"`) {
				t.Errorf("the add form must render on support=%s", support)
			}
			if strings.Contains(html, banner) {
				t.Errorf("the banner must NOT render on support=%s", support)
			}
		})
	}
}
