package confmgr

import (
	"testing"
)

// TestValidateIDRejectsBadChars covers the invalid-character branch of
// validateID, surfaced through the typed getters as ErrNotFound.
func TestValidateIDRejectsBadChars(t *testing.T) {
	s, _ := New(t.TempDir())
	for _, bad := range []string{"", "bad/slash", "has space", "dot.dot"} {
		if _, err := s.GetSet(bad); err == nil {
			t.Errorf("GetSet(%q): want error", bad)
		}
	}
}

// TestNotFoundPaths covers the ErrNotFound branches across the revision API.
func TestNotFoundPaths(t *testing.T) {
	s, _ := New(t.TempDir())

	if _, err := s.GetSet("missing"); err != ErrNotFound {
		t.Errorf("GetSet missing: want ErrNotFound, got %v", err)
	}
	if _, err := s.GetSetVersion("missing", 1); err != ErrNotFound {
		t.Errorf("GetSetVersion missing: want ErrNotFound, got %v", err)
	}
	if _, err := s.Versions(KindSet, "missing"); err != ErrNotFound {
		t.Errorf("Versions missing: want ErrNotFound, got %v", err)
	}
	if err := s.Promote(KindSet, "missing", 1); err != ErrNotFound {
		t.Errorf("Promote missing: want ErrNotFound, got %v", err)
	}
	if _, err := s.Fork(KindSet, "missing", 1); err != ErrNotFound {
		t.Errorf("Fork missing: want ErrNotFound, got %v", err)
	}
	if _, err := s.UpdateSet("missing", sampleSet(), ""); err != ErrNotFound {
		t.Errorf("UpdateSet missing: want ErrNotFound, got %v", err)
	}

	// A valid id that exists but a version that was never written.
	created, _ := s.CreateSet(sampleSet(), "")
	if _, err := s.GetSetVersion(created.ID, 99); err != ErrNotFound {
		t.Errorf("GetSetVersion bogus version: want ErrNotFound, got %v", err)
	}
}

// TestInstancePinnedSetVersion covers the SetVersion>0 branch of setForInstance:
// the instance is validated against a specific (pinned) revision of its set.
func TestInstancePinnedSetVersion(t *testing.T) {
	s, _ := New(t.TempDir())
	set, _ := s.CreateSet(sampleSet(), "")

	// Bump the set to v2 so a distinct earlier revision exists.
	if _, err := s.UpdateSet(set.ID, sampleSet(), "v2"); err != nil {
		t.Fatal(err)
	}

	inst := ConfigInstance{
		Name:       "pinned",
		SetID:      set.ID,
		SetVersion: 1, // pin to the original schema revision
		Values:     map[string]any{"voltage": 24.0},
	}
	out, err := s.CreateInstance(inst, "")
	if err != nil {
		t.Fatalf("CreateInstance pinned: %v", err)
	}
	if out.SetVersion != 1 {
		t.Errorf("SetVersion: want 1, got %d", out.SetVersion)
	}

	// Updating the pinned instance also exercises the pinned UpdateInstance path.
	out.Values["voltage"] = 30.0
	v2, err := s.UpdateInstance(out.ID, out, "bump")
	if err != nil {
		t.Fatalf("UpdateInstance pinned: %v", err)
	}
	if v2.Version != 2 {
		t.Errorf("instance version: want 2, got %d", v2.Version)
	}
}

// TestUpdateInstanceMissingSet covers the load-set error branch of
// UpdateInstance (set referenced by the instance does not exist).
func TestUpdateInstanceMissingSet(t *testing.T) {
	s, _ := New(t.TempDir())
	inst := ConfigInstance{Name: "x", SetID: "nope", Values: map[string]any{}}
	if _, err := s.UpdateInstance("anything", inst, ""); err == nil {
		t.Error("UpdateInstance with missing set: want error")
	}
}

// TestListSkipsVersionedFiles checks List returns only current revisions and
// reflects metadata after updates.
func TestListSkipsVersionedFiles(t *testing.T) {
	s, _ := New(t.TempDir())
	a, _ := s.CreateSet(sampleSet(), "")
	b, _ := s.CreateSet(sampleSet(), "")
	// Create backups for a so the dir holds versioned files too.
	_, _ = s.UpdateSet(a.ID, sampleSet(), "")
	_, _ = s.UpdateSet(a.ID, sampleSet(), "")

	metas, err := s.List(KindSet)
	if err != nil {
		t.Fatal(err)
	}
	if len(metas) != 2 {
		t.Fatalf("List: want 2 current sets, got %d", len(metas))
	}
	ids := map[string]bool{}
	for _, m := range metas {
		ids[m.ID] = true
	}
	if !ids[a.ID] || !ids[b.ID] {
		t.Errorf("List missing expected ids: %v", ids)
	}
}
