package storage

import (
	"strings"
	"testing"
)

func TestAuthorityCatalogResolvesSourceScopedS3Config(t *testing.T) {
	primary := Config{
		Provider: ProviderS3, Bucket: "customer-files", Region: "us-west-2",
		KeyPrefix: "customer/v1", Endpoint: "https://s3.example.invalid",
	}
	catalog, err := NewAuthorityCatalog(primary, `[{"name":"history","source_type":"archive","provider":"s3","bucket":"history-files","region":"us-east-1","key_prefix":"history/v2"}]`)
	if err != nil {
		t.Fatal(err)
	}
	authority, ok := catalog.Resolve(ProviderS3, "history-files", "ARCHIVE")
	if !ok {
		t.Fatal("source-scoped authority was not resolved")
	}
	if authority.Primary || authority.Name != "history" || authority.Config.Region != "us-east-1" ||
		authority.Config.KeyPrefix != "history/v2" || authority.Config.Endpoint != primary.Endpoint {
		t.Fatalf("resolved authority = %#v", authority)
	}
	if _, ok := catalog.Resolve(ProviderS3, "history-files", "native"); ok {
		t.Fatal("source-scoped authority admitted the wrong source type")
	}
	if !catalog.Governs(ProviderS3, "history-files") {
		t.Fatal("catalog did not recognize the governed historical location")
	}
}

func TestAuthorityCatalogPrefersSourceScopedConfigAtPrimaryLocation(t *testing.T) {
	primary := Config{Provider: ProviderS3, Bucket: "shared", Region: "us-west-2", KeyPrefix: "native"}
	catalog, err := NewAuthorityCatalog(primary, `[{"name":"history","source_type":"archive","provider":"s3","bucket":"shared","region":"us-west-2","key_prefix":"history"}]`)
	if err != nil {
		t.Fatal(err)
	}
	history, ok := catalog.Resolve(ProviderS3, "shared", "archive")
	if !ok || history.Primary || history.Config.KeyPrefix != "history" {
		t.Fatalf("history authority = %#v, %v", history, ok)
	}
	native, ok := catalog.Resolve(ProviderS3, "shared", "native")
	if !ok || !native.Primary || native.Config.KeyPrefix != "native" {
		t.Fatalf("primary authority = %#v, %v", native, ok)
	}
}

func TestAuthorityCatalogResolvesS3HistoryFromFilesystemPrimary(t *testing.T) {
	primary := Config{Provider: ProviderFilesystem, FilesystemRoot: "/runtime/files"}
	catalog, err := NewAuthorityCatalog(primary, `[{"name":"history","source_type":"archive","bucket":"history-files","region":"us-west-2","key_prefix":"history"}]`)
	if err != nil {
		t.Fatal(err)
	}
	history, ok := catalog.Resolve(ProviderS3, "history-files", "archive")
	if !ok || history.Primary || history.Config.Provider != ProviderS3 ||
		history.Config.Bucket != "history-files" || history.Config.KeyPrefix != "history" {
		t.Fatalf("history authority = %#v, %v", history, ok)
	}
}

func TestAuthorityCatalogRejectsAmbiguousAndUnsafeDocuments(t *testing.T) {
	primary := Config{Provider: ProviderS3, Bucket: "primary", Region: "us-west-2", KeyPrefix: "native"}
	tests := []struct {
		name string
		raw  string
		want string
	}{
		{name: "invalid json", raw: `[`, want: "valid JSON"},
		{name: "missing", raw: `[{"name":"history"}]`, want: "missing required"},
		{name: "provider", raw: `[{"name":"history","source_type":"archive","provider":"filesystem","bucket":"history","region":"us-west-2","key_prefix":"history"}]`, want: "provider must be s3"},
		{name: "unsafe prefix", raw: `[{"name":"history","source_type":"archive","provider":"s3","bucket":"history","region":"us-west-2","key_prefix":"history/../escape"}]`, want: "invalid key_prefix"},
		{name: "duplicate binding", raw: `[{"name":"one","source_type":"archive","provider":"s3","bucket":"history","region":"us-west-2","key_prefix":"one"},{"name":"two","source_type":"archive","provider":"s3","bucket":"history","region":"us-east-1","key_prefix":"two"}]`, want: "ambiguous"},
	}
	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			_, err := NewAuthorityCatalog(primary, test.raw)
			if err == nil || !strings.Contains(err.Error(), test.want) {
				t.Fatalf("error = %v, want containing %q", err, test.want)
			}
		})
	}
}
