package utils

import (
	"testing"
)

func TestGenerateID(t *testing.T) {
	t.Run("returns non-empty string", func(t *testing.T) {
		id := GenerateID()
		if id == "" {
			t.Error("GenerateID() returned empty string")
		}
	})

	t.Run("returns valid UUID format", func(t *testing.T) {
		id := GenerateID()
		if !IsValidUUID(id) {
			t.Errorf("GenerateID() returned invalid UUID: %q", id)
		}
	})

	t.Run("generates unique IDs", func(t *testing.T) {
		ids := make(map[string]bool)
		for i := 0; i < 100; i++ {
			id := GenerateID()
			if ids[id] {
				t.Errorf("GenerateID() produced duplicate ID: %q", id)
			}
			ids[id] = true
		}
	})

	t.Run("UUID has correct length", func(t *testing.T) {
		id := GenerateID()
		// UUID v4 format: 8-4-4-4-12 = 36 characters
		if len(id) != 36 {
			t.Errorf("GenerateID() returned string of length %d, want 36", len(id))
		}
	})
}

func TestIsValidUUID(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected bool
	}{
		{"valid UUID v4", "550e8400-e29b-41d4-a716-446655440000", true},
		{"valid UUID lowercase", "6ba7b810-9dad-11d1-80b4-00c04fd430c8", true},
		{"valid UUID uppercase", "6BA7B810-9DAD-11D1-80B4-00C04FD430C8", true},
		{"empty string", "", false},
		{"too short", "550e8400-e29b-41d4-a716", false},
		{"no dashes", "550e8400e29b41d4a716446655440000", true}, // google/uuid accepts this
		{"random string", "not-a-uuid", false},
		{"partial UUID", "550e8400-e29b-41d4-xxxx-446655440000", false},
		{"spaces", "  ", false},
		{"all zeros", "00000000-0000-0000-0000-000000000000", true},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := IsValidUUID(tt.input)
			if result != tt.expected {
				t.Errorf("IsValidUUID(%q) = %v, want %v", tt.input, result, tt.expected)
			}
		})
	}
}
