package sdlc

import (
	"os"
	"testing"
)

func TestStateRoundTrip(t *testing.T) {
	root := t.TempDir()

	// Create .sdlc directory
	if err := os.MkdirAll(SDLCRoot(root), 0o755); err != nil {
		t.Fatal(err)
	}

	original := DefaultState("test-project")
	original.RecordAction("test-action", "auth", "tester", "success")
	original.AddActiveFeature("auth", PhaseDraft)

	if err := original.Save(root); err != nil {
		t.Fatalf("Save: %v", err)
	}

	loaded, err := LoadState(root)
	if err != nil {
		t.Fatalf("LoadState: %v", err)
	}

	if loaded.Version != 1 {
		t.Errorf("Version = %d, want 1", loaded.Version)
	}
	if loaded.Project.Name != "test-project" {
		t.Errorf("Project.Name = %q, want %q", loaded.Project.Name, "test-project")
	}
	if len(loaded.History) != 1 {
		t.Fatalf("History len = %d, want 1", len(loaded.History))
	}
	if loaded.History[0].Action != "test-action" {
		t.Errorf("History[0].Action = %q, want %q", loaded.History[0].Action, "test-action")
	}
	if loaded.LastAction != "test-action" {
		t.Errorf("LastAction = %q, want %q", loaded.LastAction, "test-action")
	}
	if loaded.LastActor != "tester" {
		t.Errorf("LastActor = %q, want %q", loaded.LastActor, "tester")
	}
	if len(loaded.ActiveWork.Features) != 1 {
		t.Fatalf("ActiveWork.Features len = %d, want 1", len(loaded.ActiveWork.Features))
	}
	if loaded.ActiveWork.Features[0].Slug != "auth" {
		t.Errorf("ActiveWork.Features[0].Slug = %q, want %q", loaded.ActiveWork.Features[0].Slug, "auth")
	}
}

func TestLoadStateNotInitialized(t *testing.T) {
	root := t.TempDir()
	_, err := LoadState(root)
	if err != ErrNotInitialized {
		t.Errorf("LoadState = %v, want ErrNotInitialized", err)
	}
}

func TestRecordAction(t *testing.T) {
	s := DefaultState("test")
	s.RecordAction("CREATE_SPEC", "auth", "claude", "success")
	s.RecordAction("TRANSITION", "auth", "classifier", "success")

	if len(s.History) != 2 {
		t.Fatalf("History len = %d, want 2", len(s.History))
	}
	if s.LastAction != "TRANSITION" {
		t.Errorf("LastAction = %q, want TRANSITION", s.LastAction)
	}
}

func TestAddActiveFeatureDeduplicate(t *testing.T) {
	s := DefaultState("test")
	s.AddActiveFeature("auth", PhaseDraft)
	s.AddActiveFeature("auth", PhaseDraft) // duplicate

	if len(s.ActiveWork.Features) != 1 {
		t.Errorf("Features len = %d, want 1", len(s.ActiveWork.Features))
	}
}

func TestUpdateActiveFeature(t *testing.T) {
	s := DefaultState("test")
	s.AddActiveFeature("auth", PhaseDraft)
	s.UpdateActiveFeature("auth", PhaseSpecified, "feature/auth")

	if s.ActiveWork.Features[0].Phase != PhaseSpecified {
		t.Errorf("Phase = %q, want specified", s.ActiveWork.Features[0].Phase)
	}
	if s.ActiveWork.Features[0].Branch != "feature/auth" {
		t.Errorf("Branch = %q, want feature/auth", s.ActiveWork.Features[0].Branch)
	}
}

func TestRemoveActiveFeature(t *testing.T) {
	s := DefaultState("test")
	s.AddActiveFeature("auth", PhaseDraft)
	s.AddActiveFeature("payments", PhaseDraft)
	s.RemoveActiveFeature("auth")

	if len(s.ActiveWork.Features) != 1 {
		t.Fatalf("Features len = %d, want 1", len(s.ActiveWork.Features))
	}
	if s.ActiveWork.Features[0].Slug != "payments" {
		t.Errorf("Features[0].Slug = %q, want payments", s.ActiveWork.Features[0].Slug)
	}
}
