package sdlc

import (
	"os"
	"path/filepath"
	"testing"
)

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

	if err := Init(root, "test-project"); err != nil {
		t.Fatalf("Init: %v", err)
	}

	// Verify .sdlc directory exists
	sdlcRoot := SDLCRoot(root)
	info, err := os.Stat(sdlcRoot)
	if err != nil {
		t.Fatalf("stat .sdlc: %v", err)
	}
	if !info.IsDir() {
		t.Fatal(".sdlc is not a directory")
	}

	// Verify all subdirectories exist
	for _, dir := range SubDirs() {
		dirPath := filepath.Join(sdlcRoot, dir)
		info, err := os.Stat(dirPath)
		if err != nil {
			t.Errorf("stat %s: %v", dir, err)
			continue
		}
		if !info.IsDir() {
			t.Errorf("%s is not a directory", dir)
		}
	}

	// Verify state.yaml
	state, err := LoadState(root)
	if err != nil {
		t.Fatalf("LoadState: %v", err)
	}
	if state.Version != 1 {
		t.Errorf("state.Version = %d, want 1", state.Version)
	}
	if state.Project.Name != "test-project" {
		t.Errorf("state.Project.Name = %q, want test-project", state.Project.Name)
	}

	// Verify config.yaml
	config, err := LoadConfig(root)
	if err != nil {
		t.Fatalf("LoadConfig: %v", err)
	}
	if config.Project.Name != "test-project" {
		t.Errorf("config.Project.Name = %q, want test-project", config.Project.Name)
	}
}

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

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

	err := Init(root, "test")
	if err == nil {
		t.Fatal("Init should fail when already initialized")
	}
}

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

	if IsInitialized(root) {
		t.Error("IsInitialized = true before init")
	}

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

	if !IsInitialized(root) {
		t.Error("IsInitialized = false after init")
	}
}
