package controller

import (
	"strings"
	"testing"
)

func TestDerivedNames(t *testing.T) {
	if got := bucketName("prod", "data"); got != "autobk-prod-data" {
		t.Errorf("bucketName = %q", got)
	}
	if got := userName("data"); got != "autobk-data-owner" {
		t.Errorf("userName = %q", got)
	}
	if got := accessName("data"); got != "autobk-data-rw" {
		t.Errorf("accessName = %q", got)
	}
	if got := credSecretName("data"); got != "data-autobackup-rgw" {
		t.Errorf("credSecretName = %q", got)
	}
	if got := resticSecretName("data"); got != "data-autobackup-restic" {
		t.Errorf("resticSecretName = %q", got)
	}
	if got := scheduleName("data"); got != "data-autobackup" {
		t.Errorf("scheduleName = %q", got)
	}
}

func TestBucketNameSanitisesAndClamps(t *testing.T) {
	// Upper-case and invalid characters are lowercased/replaced.
	if got := bucketName("Prod_NS", "My.Data"); strings.ToLower(got) != got {
		t.Errorf("bucketName not lowercased: %q", got)
	}

	longNS := strings.Repeat("a", 40)
	longObj := strings.Repeat("b", 40)
	got := bucketName(longNS, longObj)
	if len(got) > maxBucketName {
		t.Errorf("bucketName %q exceeds %d chars (%d)", got, maxBucketName, len(got))
	}
	// Deterministic: same inputs -> same hashed name.
	if got2 := bucketName(longNS, longObj); got != got2 {
		t.Errorf("bucketName not deterministic: %q vs %q", got, got2)
	}
	// Distinct long inputs -> distinct names (hash disambiguates).
	if other := bucketName(longNS, strings.Repeat("c", 40)); other == got {
		t.Errorf("bucketName collision for distinct inputs: %q", got)
	}
}

func TestObjectNameClamps(t *testing.T) {
	longObj := strings.Repeat("x", 300)
	got := scheduleName(longObj)
	if len(got) > maxObjectName {
		t.Errorf("scheduleName %q exceeds %d chars (%d)", got, maxObjectName, len(got))
	}
	if got2 := scheduleName(longObj); got != got2 {
		t.Errorf("scheduleName not deterministic")
	}
}
