package vfs

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

func w(t *testing.T, p string, s string) {
	os.MkdirAll(filepath.Dir(p), 0o755)
	if err := os.WriteFile(p, []byte(s), 0o644); err != nil {
		t.Fatal(err)
	}
}

// Overwrite of a directory replaces it entirely (extra dst files deleted).
func TestLocalOverwriteDirDeletesExtra(t *testing.T) {
	d := t.TempDir()
	w(t, filepath.Join(d, "src", "proj", "a.txt"), "new")
	w(t, filepath.Join(d, "dst", "proj", "keep.txt"), "user data")
	_, err := CopyPath(filepath.Join(d, "src", "proj"), filepath.Join(d, "dst"), true)
	_, e := os.Stat(filepath.Join(d, "dst", "proj", "keep.txt"))
	t.Logf("err=%v keep.txt exists=%v", err, e == nil)
	if e != nil {
		t.Errorf("BUG CONFIRMED: overwrite wiped files that only existed in destination dir")
	}
}

// Old destination deleted BEFORE copying; failure leaves nothing.
func TestLocalOverwriteFailureLosesDest(t *testing.T) {
	d := t.TempDir()
	w(t, filepath.Join(d, "src", "f.txt"), "new")
	os.Chmod(filepath.Join(d, "src", "f.txt"), 0o000) // unreadable -> copy fails
	w(t, filepath.Join(d, "dst", "f.txt"), "OLD VALUABLE")
	_, err := CopyPath(filepath.Join(d, "src", "f.txt"), filepath.Join(d, "dst"), true)
	_, e := os.Stat(filepath.Join(d, "dst", "f.txt"))
	t.Logf("err=%v dst exists=%v", err, e == nil)
	if e != nil {
		t.Errorf("BUG CONFIRMED: failed overwrite deleted the old destination")
	}
}

// Read-only source dir cannot be copied (dst dir created 0555 before filling).
func TestLocalCopyReadOnlyDir(t *testing.T) {
	d := t.TempDir()
	w(t, filepath.Join(d, "src", "ro", "a.txt"), "x")
	os.Chmod(filepath.Join(d, "src", "ro"), 0o555)
	defer os.Chmod(filepath.Join(d, "src", "ro"), 0o755)
	_, err := CopyPath(filepath.Join(d, "src", "ro"), filepath.Join(d, "dst"), false)
	t.Logf("err=%v", err)
	if err != nil {
		t.Errorf("BUG CONFIRMED: copying read-only dir fails: %v", err)
	}
	os.Chmod(filepath.Join(d, "dst", "ro"), 0o755)
}

// samePath does not resolve symlinks: dst dir is a symlink to src's dir -> overwrite deletes source.
func TestLocalOverwriteViaSymlinkAlias(t *testing.T) {
	d := t.TempDir()
	w(t, filepath.Join(d, "real", "f.txt"), "ONLY COPY")
	os.Symlink(filepath.Join(d, "real"), filepath.Join(d, "alias"))
	_, err := CopyPath(filepath.Join(d, "real", "f.txt"), filepath.Join(d, "alias"), true)
	_, e := os.Stat(filepath.Join(d, "real", "f.txt"))
	t.Logf("err=%v source exists=%v", err, e == nil)
	if e != nil {
		t.Errorf("BUG CONFIRMED: source destroyed when target dir is a symlink alias")
	}
}

// mtime not preserved locally either.
func TestLocalMtime(t *testing.T) {
	d := t.TempDir()
	p := filepath.Join(d, "src", "f.txt")
	w(t, p, "x")
	old := time.Date(2020, 1, 1, 0, 0, 0, 0, time.UTC)
	os.Chtimes(p, old, old)
	os.MkdirAll(filepath.Join(d, "dst"), 0o755)
	out, _ := CopyPath(p, filepath.Join(d, "dst"), false)
	st, _ := os.Stat(out)
	if !st.ModTime().Equal(old) {
		t.Errorf("BUG CONFIRMED: mtime not preserved: %v", st.ModTime())
	}
}

// Dir copy stats have BytesTotal=0 -> byte progress meaningless.
func TestCopyStatsBytes(t *testing.T) {
	d := t.TempDir()
	w(t, filepath.Join(d, "src", "a"), "1234567890")
	st, _ := CopyStats(filepath.Join(d, "src"))
	t.Logf("stats=%+v", st)
	if st.BytesTotal == 0 {
		t.Errorf("BUG CONFIRMED: CopyStats for dir has BytesTotal=0")
	}
	_ = context.Background
}
