package sftp

import (
	"testing"
)

func TestNewSFTPService(t *testing.T) {
	svc := NewSFTPService()
	if svc == nil {
		t.Fatal("nil")
	}
}

func TestListWithoutClient(t *testing.T) {
	svc := NewSFTPService()
	_, err := svc.List("nonexistent", "/")
	if err == nil {
		t.Error("should error without client")
	}
}

func TestReadFileWithoutClient(t *testing.T) {
	svc := NewSFTPService()
	_, err := svc.ReadFile("nonexistent", "/etc/hosts")
	if err == nil {
		t.Error("should error without client")
	}
}

func TestWriteFileWithoutClient(t *testing.T) {
	svc := NewSFTPService()
	err := svc.WriteFile("nonexistent", "/tmp/test", "data")
	if err == nil {
		t.Error("should error without client")
	}
}

func TestMkdirWithoutClient(t *testing.T) {
	svc := NewSFTPService()
	err := svc.Mkdir("nonexistent", "/tmp/newdir")
	if err == nil {
		t.Error("should error without client")
	}
}

func TestDeleteWithoutClient(t *testing.T) {
	svc := NewSFTPService()
	err := svc.Delete("nonexistent", "/tmp/file")
	if err == nil {
		t.Error("should error without client")
	}
}

func TestRenameWithoutClient(t *testing.T) {
	svc := NewSFTPService()
	err := svc.Rename("nonexistent", "/old", "/new")
	if err == nil {
		t.Error("should error without client")
	}
}

func TestStatWithoutClient(t *testing.T) {
	svc := NewSFTPService()
	_, err := svc.Stat("nonexistent", "/tmp")
	if err == nil {
		t.Error("should error without client")
	}
}

func TestFileEntrySorting(t *testing.T) {
	// Test that SortEntries puts dirs first, then alpha
	entries := []FileEntry{
		{Name: "zebra.txt", IsDir: false},
		{Name: "alpha", IsDir: true},
		{Name: "beta.conf", IsDir: false},
		{Name: "omega", IsDir: true},
	}
	SortEntries(entries)
	if entries[0].Name != "alpha" {
		t.Errorf("[0] = %s, want alpha", entries[0].Name)
	}
	if entries[1].Name != "omega" {
		t.Errorf("[1] = %s, want omega", entries[1].Name)
	}
	if entries[2].Name != "beta.conf" {
		t.Errorf("[2] = %s, want beta.conf", entries[2].Name)
	}
	if entries[3].Name != "zebra.txt" {
		t.Errorf("[3] = %s, want zebra.txt", entries[3].Name)
	}
}

func TestSortEntriesEmpty(t *testing.T) {
	entries := []FileEntry{}
	SortEntries(entries)
	if len(entries) != 0 {
		t.Errorf("expected empty slice, got %d entries", len(entries))
	}
}

func TestSortEntriesCaseInsensitive(t *testing.T) {
	entries := []FileEntry{
		{Name: "Zebra", IsDir: false},
		{Name: "alpha", IsDir: false},
	}
	SortEntries(entries)
	if entries[0].Name != "alpha" {
		t.Errorf("[0] = %s, want alpha", entries[0].Name)
	}
	if entries[1].Name != "Zebra" {
		t.Errorf("[1] = %s, want Zebra", entries[1].Name)
	}
}

func TestMaxEditFileSize(t *testing.T) {
	if MaxEditFileSize != 5*1024*1024 {
		t.Errorf("MaxEditFileSize = %d, want %d", MaxEditFileSize, 5*1024*1024)
	}
}
