package admin

import "testing"

func TestMergeOrgPluginsAddsMissingDefaults(t *testing.T) {
	defaults := defaultOrgPolicy()["plugins"].([]any)
	stored := []any{
		map[string]any{
			"id":          "mail-automation",
			"name":        "Old name",
			"description": "Old description",
			"enabled":     false,
			"version":     "0.9.0",
		},
	}

	merged := mergeOrgPlugins(defaults, stored)
	if len(merged) != len(defaults) {
		t.Fatalf("len(merged) = %d, want %d", len(merged), len(defaults))
	}

	first, ok := merged[0].(map[string]any)
	if !ok {
		t.Fatalf("first plugin is not a map")
	}
	if first["enabled"] != false {
		t.Fatalf("enabled = %v, want false", first["enabled"])
	}
	if first["version"] != "0.9.0" {
		t.Fatalf("version = %v, want stored override", first["version"])
	}

	var hasRichtext bool
	for _, item := range merged {
		plugin, ok := item.(map[string]any)
		if !ok {
			continue
		}
		if plugin["id"] == "richtext-editor" {
			hasRichtext = true
			if plugin["enabled"] != true {
				t.Fatalf("richtext-editor enabled = %v, want true", plugin["enabled"])
			}
		}
	}
	if !hasRichtext {
		t.Fatal("expected richtext-editor from defaults")
	}
}
