package sdlc

import (
	"path/filepath"
	"testing"
)

func TestStatePath(t *testing.T) {
	got := StatePath("/project")
	want := filepath.Join("/project", ".sdlc", "state.yaml")
	if got != want {
		t.Errorf("StatePath = %q, want %q", got, want)
	}
}

func TestConfigPath(t *testing.T) {
	got := ConfigPath("/project")
	want := filepath.Join("/project", ".sdlc", "config.yaml")
	if got != want {
		t.Errorf("ConfigPath = %q, want %q", got, want)
	}
}

func TestFeatureDir(t *testing.T) {
	got := FeatureDir("/project", "auth")
	want := filepath.Join("/project", ".sdlc", "features", "auth")
	if got != want {
		t.Errorf("FeatureDir = %q, want %q", got, want)
	}
}

func TestManifestPath(t *testing.T) {
	got := ManifestPath("/project", "auth")
	want := filepath.Join("/project", ".sdlc", "features", "auth", "manifest.yaml")
	if got != want {
		t.Errorf("ManifestPath = %q, want %q", got, want)
	}
}

func TestArtifactPath(t *testing.T) {
	tests := []struct {
		artifact ArtifactType
		wantFile string
	}{
		{ArtifactSpec, "spec.md"},
		{ArtifactDesign, "design.md"},
		{ArtifactTasks, "tasks.md"},
		{ArtifactQAPlan, "qa-plan.md"},
		{ArtifactReview, "review.md"},
		{ArtifactAudit, "audit.md"},
		{ArtifactQAResults, "qa-results.md"},
	}

	for _, tt := range tests {
		got := ArtifactPath("/project", "auth", tt.artifact)
		want := filepath.Join("/project", ".sdlc", "features", "auth", tt.wantFile)
		if got != want {
			t.Errorf("ArtifactPath(%q) = %q, want %q", tt.artifact, got, want)
		}
	}
}

func TestArtifactPathInvalid(t *testing.T) {
	got := ArtifactPath("/project", "auth", ArtifactType("bogus"))
	if got != "" {
		t.Errorf("ArtifactPath(bogus) = %q, want empty", got)
	}
}

func TestValidateSlug(t *testing.T) {
	valid := []string{"auth", "user-auth", "a1", "my-feature-2"}
	for _, s := range valid {
		if err := ValidateSlug(s); err != nil {
			t.Errorf("ValidateSlug(%q) = %v, want nil", s, err)
		}
	}

	invalid := []string{"", "Auth", "UPPER", "123start", "has spaces", "has_underscores", "-leading"}
	for _, s := range invalid {
		if err := ValidateSlug(s); err == nil {
			t.Errorf("ValidateSlug(%q) = nil, want error", s)
		}
	}
}

func TestPhaseIndex(t *testing.T) {
	if i := PhaseIndex(PhaseDraft); i != 0 {
		t.Errorf("PhaseIndex(draft) = %d, want 0", i)
	}
	if i := PhaseIndex(PhaseReleased); i != 9 {
		t.Errorf("PhaseIndex(released) = %d, want 9", i)
	}
	if i := PhaseIndex("bogus"); i != -1 {
		t.Errorf("PhaseIndex(bogus) = %d, want -1", i)
	}
}

func TestIsValidPhase(t *testing.T) {
	if !IsValidPhase(PhaseDraft) {
		t.Error("IsValidPhase(draft) = false, want true")
	}
	if IsValidPhase("bogus") {
		t.Error("IsValidPhase(bogus) = true, want false")
	}
}

func TestIsValidArtifactType(t *testing.T) {
	if !IsValidArtifactType(ArtifactSpec) {
		t.Error("IsValidArtifactType(spec) = false, want true")
	}
	if IsValidArtifactType("bogus") {
		t.Error("IsValidArtifactType(bogus) = true, want false")
	}
}
