package tools

import (
	"testing"

	"github.com/google/uuid"
)

func TestDecodeBase64AcceptsWhitespaceAndMultipleVariants(t *testing.T) {
	tests := []struct {
		name  string
		input string
		want  string
	}{
		{name: "standard with whitespace", input: "aG V s\nbG8=", want: "hello"},
		{name: "raw standard", input: "aGVsbG8", want: "hello"},
		{name: "standard with extra padding", input: "aGVsbG8==", want: "hello"},
		{name: "standard url-safe payload", input: "--8=", want: string([]byte{0xfb, 0xef})},
		{name: "raw url-safe payload", input: "--8", want: string([]byte{0xfb, 0xef})},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			got, err := decodeBase64(tc.input)
			if err != nil {
				t.Fatalf("decodeBase64(%q) error = %v", tc.input, err)
			}
			if string(got) != tc.want {
				t.Fatalf("decodeBase64(%q) = %q, want %q", tc.input, string(got), tc.want)
			}
		})
	}
}

func TestParseStoredFileIDAcceptsUUIDAndURI(t *testing.T) {
	id := uuid.New()

	tests := []struct {
		name  string
		input string
		want  uuid.UUID
	}{
		{name: "bare uuid", input: id.String(), want: id},
		{name: "resource uri", input: fileURIPrefix + id.String(), want: id},
		{name: "resource uri with surrounding whitespace", input: "  " + fileURIPrefix + id.String() + "  ", want: id},
	}

	for _, tc := range tests {
		t.Run(tc.name, func(t *testing.T) {
			got, err := parseStoredFileID(tc.input)
			if err != nil {
				t.Fatalf("parseStoredFileID(%q) error = %v", tc.input, err)
			}
			if got != tc.want {
				t.Fatalf("parseStoredFileID(%q) = %v, want %v", tc.input, got, tc.want)
			}
		})
	}
}

func TestParseStoredFileIDRejectsInvalidValues(t *testing.T) {
	tests := []string{
		"",
		"not-a-uuid",
		fileURIPrefix + "not-a-uuid",
	}

	for _, input := range tests {
		t.Run(input, func(t *testing.T) {
			if _, err := parseStoredFileID(input); err == nil {
				t.Fatalf("parseStoredFileID(%q) = nil error, want error", input)
			}
		})
	}
}
