package security

import (
	"encoding/base64"
	"testing"

	"github.com/btcsuite/btcutil/base58"
)

func TestGenerateRandomKeyBase64(t *testing.T) {
	tests := []struct {
		name      string
		n         int
		wantError bool
	}{
		{
			name:      "valid 16 bytes",
			n:         16,
			wantError: false,
		},
		{
			name:      "valid 32 bytes",
			n:         32,
			wantError: false,
		},
		{
			name:      "valid 64 bytes",
			n:         64,
			wantError: false,
		},
		{
			name:      "invalid zero bytes",
			n:         0,
			wantError: true,
		},
		{
			name:      "invalid negative bytes",
			n:         -1,
			wantError: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result, err := GenerateRandomKeyBase64(tt.n)

			if tt.wantError {
				if err == nil {
					t.Error("expected error but got none")
				}
				return
			}

			if err != nil {
				t.Errorf("unexpected error: %v", err)
				return
			}

			// Verify result is not empty
			if result == "" {
				t.Error("expected non-empty result")
			}

			// Verify result is valid base64
			decoded, err := base64.StdEncoding.DecodeString(result)
			if err != nil {
				t.Errorf("result is not valid base64: %v", err)
			}

			// Verify decoded length matches input
			if len(decoded) != tt.n {
				t.Errorf("expected decoded length %d, got %d", tt.n, len(decoded))
			}
		})
	}

	// Test randomness - two calls should produce different results
	t.Run("randomness check", func(t *testing.T) {
		result1, err1 := GenerateRandomKeyBase64(32)
		result2, err2 := GenerateRandomKeyBase64(32)

		if err1 != nil || err2 != nil {
			t.Fatalf("unexpected errors: %v, %v", err1, err2)
		}

		if result1 == result2 {
			t.Error("expected different random keys, got identical results")
		}
	})
}

func TestGenerateRandomKeyBase58(t *testing.T) {
	tests := []struct {
		name      string
		n         int
		wantError bool
	}{
		{
			name:      "valid 16 bytes",
			n:         16,
			wantError: false,
		},
		{
			name:      "valid 32 bytes",
			n:         32,
			wantError: false,
		},
		{
			name:      "valid 64 bytes",
			n:         64,
			wantError: false,
		},
		{
			name:      "invalid zero bytes",
			n:         0,
			wantError: true,
		},
		{
			name:      "invalid negative bytes",
			n:         -1,
			wantError: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result, err := GenerateRandomKeyBase58(tt.n)

			if tt.wantError {
				if err == nil {
					t.Error("expected error but got none")
				}
				return
			}

			if err != nil {
				t.Errorf("unexpected error: %v", err)
				return
			}

			// Verify result is not empty
			if result == "" {
				t.Error("expected non-empty result")
			}

			// Verify result is valid base58 by decoding
			decoded := base58.Decode(result)
			if len(decoded) == 0 {
				t.Error("result is not valid base58")
			}

			// Verify decoded length matches input
			if len(decoded) != tt.n {
				t.Errorf("expected decoded length %d, got %d", tt.n, len(decoded))
			}
		})
	}

	// Test randomness - two calls should produce different results
	t.Run("randomness check", func(t *testing.T) {
		result1, err1 := GenerateRandomKeyBase58(32)
		result2, err2 := GenerateRandomKeyBase58(32)

		if err1 != nil || err2 != nil {
			t.Fatalf("unexpected errors: %v, %v", err1, err2)
		}

		if result1 == result2 {
			t.Error("expected different random keys, got identical results")
		}
	})
}
