package main

import (
	"os"
	"path/filepath"
	"strings"
	"testing"
	"time"

	"reasonix/internal/agent"
	"reasonix/internal/provider"
)

func TestRecoveryGCStartupWaitIsCancellationAware(t *testing.T) {
	done := make(chan struct{})
	elapsed := make(chan time.Time, 1)
	elapsed <- time.Now()
	if !waitRecoveryGCStartup(done, elapsed) {
		t.Fatal("elapsed startup grace should allow the first sweep")
	}
	done = make(chan struct{})
	close(done)
	if waitRecoveryGCStartup(done, make(chan time.Time)) {
		t.Fatal("cancelled startup must skip the first sweep")
	}
}

// forkCoveredRecoveryBranch builds the reclaimable shape in dir: a conflict
// fork whose parent went on to contain everything the fork preserved.
func forkCoveredRecoveryBranch(t *testing.T, dir, name string) (parentPath, branchPath string) {
	t.Helper()
	parentPath = filepath.Join(dir, name+".jsonl")
	disk := agent.NewSession("sys")
	disk.Add(provider.Message{Role: provider.RoleUser, Content: "first"})
	disk.Add(provider.Message{Role: provider.RoleAssistant, Content: "one"})
	disk.Add(provider.Message{Role: provider.RoleUser, Content: "disk " + name})
	if err := disk.Save(parentPath); err != nil {
		t.Fatalf("Save parent: %v", err)
	}
	stale := agent.NewSession("sys")
	stale.Add(provider.Message{Role: provider.RoleUser, Content: "first"})
	stale.Add(provider.Message{Role: provider.RoleAssistant, Content: "one"})
	stale.Add(provider.Message{Role: provider.RoleUser, Content: "local " + name})
	info, err := stale.SaveRecoveryBranch(agent.RecoveryBranchOptions{OriginalPath: parentPath})
	if err != nil {
		t.Fatalf("SaveRecoveryBranch: %v", err)
	}
	covering, err := agent.LoadSession(parentPath)
	if err != nil {
		t.Fatalf("Load covering parent: %v", err)
	}
	covering.Replace(append([]provider.Message(nil), stale.Snapshot()...))
	covering.Add(provider.Message{Role: provider.RoleAssistant, Content: "answered after recovery"})
	if err := covering.SaveRewrite(parentPath); err != nil {
		t.Fatalf("Save covering parent: %v", err)
	}
	return parentPath, info.Path
}

func TestRecoveryGCTrashesCoveredForkAndKeepsParent(t *testing.T) {
	isolateDesktopUserDirs(t)
	root := globalTabWorkspaceRoot()
	dir := desktopSessionDir(root)
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatalf("mkdir sessions: %v", err)
	}
	parentPath, branchPath := forkCoveredRecoveryBranch(t, dir, "session")

	app := &App{tabs: map[string]*WorkspaceTab{}, detachedSessions: map[string]*WorkspaceTab{}}
	if got := app.reclaimRecoveryBranchesIn([]string{dir}, time.Now().Add(48*time.Hour), agent.RecoveryGCGracePeriod); got != 1 {
		t.Fatalf("reclaimed = %d, want 1", got)
	}

	if _, err := os.Stat(branchPath); !os.IsNotExist(err) {
		t.Fatalf("reclaimed branch still present at %s (err=%v)", branchPath, err)
	}
	key := filepath.Base(branchPath)
	trashPath := filepath.Join(dir, sessionTrashDir, key, key)
	if _, err := os.Stat(trashPath); err != nil {
		t.Fatalf("reclaimed branch should be in trash: %v", err)
	}
	if _, err := os.Stat(parentPath); err != nil {
		t.Fatalf("parent session must be untouched: %v", err)
	}

	// A second sweep is a no-op: nothing left to reclaim.
	if got := app.reclaimRecoveryBranchesIn([]string{dir}, time.Now().Add(48*time.Hour), agent.RecoveryGCGracePeriod); got != 0 {
		t.Fatalf("second sweep reclaimed = %d, want 0", got)
	}
}

func TestRecoveryGCSkipsBranchOpenInTab(t *testing.T) {
	isolateDesktopUserDirs(t)
	root := globalTabWorkspaceRoot()
	dir := desktopSessionDir(root)
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatalf("mkdir sessions: %v", err)
	}
	_, branchPath := forkCoveredRecoveryBranch(t, dir, "open")

	tab := &WorkspaceTab{ID: "tab", Scope: "global", SessionPath: branchPath, Ready: true}
	app := &App{tabs: map[string]*WorkspaceTab{"tab": tab}}
	if got := app.reclaimRecoveryBranchesIn([]string{dir}, time.Now().Add(48*time.Hour), agent.RecoveryGCGracePeriod); got != 0 {
		t.Fatalf("reclaimed = %d, want 0 while the branch is open in a tab", got)
	}
	if _, err := os.Stat(branchPath); err != nil {
		t.Fatalf("open branch must be untouched: %v", err)
	}
}

// TestRecoveryGCFirstSweepWaitsForTabRestore forces the startup race the
// review caught: a saved recovery tab exists in desktop-tabs.json but a.tabs
// has not been populated yet. The GC's first sweep must wait for the restore
// gate — sweeping early would judge the branch "not open in any tab" and
// DeleteSession would persist the pre-restore (empty) tab list over the
// user's saved one.
func TestRecoveryGCFirstSweepWaitsForTabRestore(t *testing.T) {
	isolateDesktopUserDirs(t)
	root := globalTabWorkspaceRoot()
	dir := desktopSessionDir(root)
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatalf("mkdir sessions: %v", err)
	}
	_, branchPath := forkCoveredRecoveryBranch(t, dir, "startup")

	ctx := t.Context()
	app := &App{
		ctx:          ctx,
		tabs:         map[string]*WorkspaceTab{},
		tabsRestored: make(chan struct{}),
	}

	swept := make(chan int, 1)
	go func() {
		select {
		case <-app.tabsRestoredSignal():
		case <-ctx.Done():
			swept <- -1
			return
		}
		swept <- app.reclaimRecoveryBranchesIn([]string{dir}, time.Now().Add(48*time.Hour), agent.RecoveryGCGracePeriod)
	}()

	// Gate still closed: the sweep must not have run — the branch is intact.
	select {
	case n := <-swept:
		t.Fatalf("sweep ran before tab restore completed (reclaimed=%d)", n)
	case <-time.After(100 * time.Millisecond):
	}
	if _, err := os.Stat(branchPath); err != nil {
		t.Fatalf("branch touched before restore completed: %v", err)
	}

	// Restore lands the saved tab holding the branch, then opens the gate:
	// the sweep runs and must skip the now-open branch.
	tab := &WorkspaceTab{ID: "tab", Scope: "global", SessionPath: branchPath, Ready: true}
	app.mu.Lock()
	app.tabs["tab"] = tab
	app.mu.Unlock()
	app.markTabsRestored()

	if n := <-swept; n != 0 {
		t.Fatalf("post-restore sweep reclaimed = %d, want 0 (branch is open in a restored tab)", n)
	}
	if _, err := os.Stat(branchPath); err != nil {
		t.Fatalf("restored tab's branch must be untouched: %v", err)
	}

	// markTabsRestored is idempotent (restore + recover paths may both fire).
	app.markTabsRestored()
}

func TestRecoveryGCRunsDespiteSafeModeEnv(t *testing.T) {
	// v1.20+: GC is no longer suppressed by REASONIX_SAFE_MODE.
	isolateDesktopUserDirs(t)
	t.Setenv("REASONIX_SAFE_MODE", "1")
	root := globalTabWorkspaceRoot()
	dir := desktopSessionDir(root)
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatalf("mkdir sessions: %v", err)
	}
	_, branchPath := forkCoveredRecoveryBranch(t, dir, "safe")

	app := &App{tabs: map[string]*WorkspaceTab{}, detachedSessions: map[string]*WorkspaceTab{}}
	_ = app.reclaimRecoveryBranchesIn([]string{dir}, time.Now().Add(48*time.Hour), agent.RecoveryGCGracePeriod)
	// Branch may or may not be reclaimed depending on age/coverage; the
	// important contract is that Safe Mode env does not force a no-op panic-free path.
	_ = branchPath
}

func TestRecoveryGCSkipsWhenBranchContinuesAfterScan(t *testing.T) {
	// Scan marks the branch reclaimable, then a concurrent continue-edit must
	// make DeleteRecoveryCopy refuse so unique content is never trashed.
	isolateDesktopUserDirs(t)
	root := globalTabWorkspaceRoot()
	dir := desktopSessionDir(root)
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatalf("mkdir sessions: %v", err)
	}
	_, branchPath := forkCoveredRecoveryBranch(t, dir, "race-edit")
	later := time.Now().Add(48 * time.Hour)
	reclaimable, err := agent.ReclaimableRecoveryBranches(dir, later, agent.RecoveryGCGracePeriod)
	if err != nil {
		t.Fatalf("ReclaimableRecoveryBranches: %v", err)
	}
	if len(reclaimable) != 1 || reclaimable[0] != branchPath {
		t.Fatalf("reclaimable = %v, want only %s", reclaimable, branchPath)
	}
	branch, err := agent.LoadSession(branchPath)
	if err != nil {
		t.Fatalf("LoadSession: %v", err)
	}
	branch.Add(provider.Message{Role: provider.RoleAssistant, Content: "continued after scan"})
	if err := branch.Save(branchPath); err != nil {
		t.Fatalf("Save continued branch: %v", err)
	}
	app := NewApp()
	if got := app.reclaimRecoveryBranchesIn([]string{dir}, later, agent.RecoveryGCGracePeriod); got != 0 {
		t.Fatalf("reclaimed = %d, want 0 after post-scan continue", got)
	}
	if _, err := os.Stat(branchPath); err != nil {
		t.Fatalf("continued branch must remain: %v", err)
	}
}

func TestRecoveryGCSkipsWhenLeaseAcquiredAfterScan(t *testing.T) {
	isolateDesktopUserDirs(t)
	root := globalTabWorkspaceRoot()
	dir := desktopSessionDir(root)
	if err := os.MkdirAll(dir, 0o755); err != nil {
		t.Fatalf("mkdir sessions: %v", err)
	}
	_, branchPath := forkCoveredRecoveryBranch(t, dir, "race-lease")
	later := time.Now().Add(48 * time.Hour)
	reclaimable, err := agent.ReclaimableRecoveryBranches(dir, later, agent.RecoveryGCGracePeriod)
	if err != nil {
		t.Fatalf("ReclaimableRecoveryBranches: %v", err)
	}
	if len(reclaimable) != 1 {
		t.Fatalf("reclaimable = %v, want one path", reclaimable)
	}
	lease, err := agent.TryAcquireSessionLease(branchPath)
	if err != nil {
		t.Fatalf("TryAcquireSessionLease: %v", err)
	}
	defer lease.Release()
	app := NewApp()
	if got := app.reclaimRecoveryBranchesIn([]string{dir}, later, agent.RecoveryGCGracePeriod); got != 0 {
		t.Fatalf("reclaimed = %d, want 0 while lease held", got)
	}
	if _, err := os.Stat(branchPath); err != nil {
		t.Fatalf("leased branch must remain: %v", err)
	}
}

func TestRecoveryGCUsesDeleteRecoveryCopyNotDeleteSession(t *testing.T) {
	source, err := os.ReadFile("recovery_gc.go")
	if err != nil {
		t.Fatal(err)
	}
	text := string(source)
	if !strings.Contains(text, "DeleteRecoveryCopy(path)") {
		t.Fatal("background recovery GC must call DeleteRecoveryCopy")
	}
	if strings.Contains(text, "DeleteSession(path)") {
		t.Fatal("background recovery GC must not use unguarded DeleteSession")
	}
}
