package service

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

func TestValidateExistingRepositoryPathAcceptsRepositoryRoot(t *testing.T) {
	root := t.TempDir()
	if err := os.WriteFile(filepath.Join(root, "config"), []byte("restic"), 0600); err != nil {
		t.Fatal(err)
	}
	if err := validateExistingRepositoryPath(root); err != nil {
		t.Fatalf("repository root rejected: %v", err)
	}
}

func TestValidateExistingRepositoryPathSuggestsDirectChild(t *testing.T) {
	root := t.TempDir()
	repository := filepath.Join(root, "server-backup")
	if err := os.Mkdir(repository, 0700); err != nil {
		t.Fatal(err)
	}
	if err := os.WriteFile(filepath.Join(repository, "config"), []byte("restic"), 0600); err != nil {
		t.Fatal(err)
	}
	err := validateExistingRepositoryPath(root)
	if err == nil || !strings.Contains(err.Error(), repository) {
		t.Fatalf("missing repository suggestion: %v", err)
	}
}

func TestValidateExistingRepositoryPathExplainsMissingConfig(t *testing.T) {
	root := t.TempDir()
	err := validateExistingRepositoryPath(root)
	if err == nil || !strings.Contains(err.Error(), "config, data, index, keys und snapshots") {
		t.Fatalf("missing layout guidance: %v", err)
	}
}
