package worktree_test

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

	"github.com/aki/amux/internal/config"
	"github.com/aki/amux/internal/idmap"
	"github.com/aki/amux/internal/tests/helpers"
	worktree "github.com/aki/amux/internal/worktree"
)

func TestManager_ListReconciliation(t *testing.T) {
	t.Skip("Skipping test that depends on removed ID/Index system")
	// Create test repository
	repoDir := helpers.CreateTestRepo(t)

	// Initialize Amux
	configManager := config.NewManager(repoDir)
	cfg := config.DefaultConfig()
	err := configManager.Save(cfg)
	if err != nil {
		t.Fatalf("Failed to initialize: %v", err)
	}

	// Create workspace manager
	manager, err := worktree.NewManager(configManager)
	if err != nil {
		t.Fatalf("Failed to create manager: %v", err)
	}

	// Create ID mapper to check indices
	idMapper, err := idmap.NewWorkspaceIDMapper(configManager.GetAmuxDir())
	if err != nil {
		t.Fatalf("Failed to create ID mapper: %v", err)
	}

	// Create some workspaces
	ctx := context.Background()
	ws1, err := manager.Create(ctx, worktree.CreateOptions{
		Name:        "test-ws1",
		Description: "Test workspace 1",
	})
	if err != nil {
		t.Fatalf("Failed to create workspace 1: %v", err)
	}

	ws2, err := manager.Create(ctx, worktree.CreateOptions{
		Name:        "test-ws2",
		Description: "Test workspace 2",
	})
	if err != nil {
		t.Fatalf("Failed to create workspace 2: %v", err)
	}

	ws3, err := manager.Create(ctx, worktree.CreateOptions{
		Name:        "test-ws3",
		Description: "Test workspace 3",
	})
	if err != nil {
		t.Fatalf("Failed to create workspace 3: %v", err)
	}

	// Verify all have indices
	if _, exists := idMapper.GetIndex(idmap.WorkspaceID(ws1.Branch)); !exists {
		t.Error("Expected ws1 to have an index")
	}
	if _, exists := idMapper.GetIndex(idmap.WorkspaceID(ws2.Branch)); !exists {
		t.Error("Expected ws2 to have an index")
	}
	if _, exists := idMapper.GetIndex(idmap.WorkspaceID(ws3.Branch)); !exists {
		t.Error("Expected ws3 to have an index")
	}

	// Manually delete workspace 2's directory (simulating external deletion)
	workspaceDir := filepath.Join(configManager.GetWorkspacesDir(), ws2.Branch)
	err = os.RemoveAll(workspaceDir)
	if err != nil {
		t.Fatalf("Failed to manually delete workspace 2: %v", err)
	}

	// List workspaces - this should trigger reconciliation
	workspaces, err := manager.List(ctx, worktree.ListOptions{})
	if err != nil {
		t.Fatalf("Failed to list workspaces: %v", err)
	}

	// Should only have 2 workspaces now
	if len(workspaces) != 2 {
		t.Errorf("Expected 2 workspaces after deletion, got %d", len(workspaces))
	}

	// Verify ws2's index was cleaned up
	if _, exists := idMapper.GetIndex(idmap.WorkspaceID(ws2.Branch)); exists {
		t.Error("Expected ws2 index to be removed after reconciliation")
	}

	// Verify ws1 and ws3 still have indices
	if _, exists := idMapper.GetIndex(idmap.WorkspaceID(ws1.Branch)); !exists {
		t.Error("Expected ws1 to still have an index")
	}
	if _, exists := idMapper.GetIndex(idmap.WorkspaceID(ws3.Branch)); !exists {
		t.Error("Expected ws3 to still have an index")
	}

	// Clean up
	manager.Remove(ctx, worktree.Identifier(ws1.Branch), worktree.RemoveOptions{})
	manager.Remove(ctx, worktree.Identifier(ws3.Branch), worktree.RemoveOptions{})
}

func TestManager_GetReconciliation(t *testing.T) {
	t.Skip("Skipping test that depends on removed ID/Index system")
	// Create test repository
	repoDir := helpers.CreateTestRepo(t)

	// Initialize Amux
	configManager := config.NewManager(repoDir)
	cfg := config.DefaultConfig()
	err := configManager.Save(cfg)
	if err != nil {
		t.Fatalf("Failed to initialize: %v", err)
	}

	// Create workspace manager
	manager, err := worktree.NewManager(configManager)
	if err != nil {
		t.Fatalf("Failed to create manager: %v", err)
	}

	// Create ID mapper to check indices
	idMapper, err := idmap.NewWorkspaceIDMapper(configManager.GetAmuxDir())
	if err != nil {
		t.Fatalf("Failed to create ID mapper: %v", err)
	}

	// Create a workspace
	ctx := context.Background()
	ws, err := manager.Create(ctx, worktree.CreateOptions{
		Name:        "test-reconcile-get",
		Description: "Test get reconciliation",
	})
	if err != nil {
		t.Fatalf("Failed to create workspace: %v", err)
	}

	// Verify it has an index
	if _, exists := idMapper.GetIndex(idmap.WorkspaceID(ws.Branch)); !exists {
		t.Error("Expected workspace to have an index")
	}

	// Manually delete the workspace directory
	workspaceDir := filepath.Join(configManager.GetWorkspacesDir(), ws.Branch)
	err = os.RemoveAll(workspaceDir)
	if err != nil {
		t.Fatalf("Failed to manually delete workspace: %v", err)
	}

	// Try to get the workspace - should fail but NOT clean up index (reconciliation only happens during List)
	_, err = manager.Get(ctx, ws.Branch)
	if err == nil {
		t.Error("Expected error when getting deleted workspace")
	}

	// Verify the index still exists (Get does not reconcile)
	if _, exists := idMapper.GetIndex(idmap.WorkspaceID(ws.Branch)); !exists {
		t.Error("Expected workspace index to still exist after failed Get (reconciliation only happens during List)")
	}

	// Now list workspaces - this should trigger reconciliation
	_, err = manager.List(ctx, worktree.ListOptions{})
	if err != nil {
		t.Fatalf("Failed to list workspaces: %v", err)
	}

	// Now the index should be cleaned up
	if _, exists := idMapper.GetIndex(idmap.WorkspaceID(ws.Branch)); exists {
		t.Error("Expected workspace index to be removed after List reconciliation")
	}
}
