package utils

import (
	"os"
	"path/filepath"
	"testing"
)

func TestComputeXXH64_WAVFile(t *testing.T) {
	// Path to test file relative to project root
	wavFile := filepath.Join("..", "audio", "N14-2025-02-25-20241116_054500-685-703.wav")

	hash, err := ComputeXXH64(wavFile)
	if err != nil {
		t.Fatalf("ComputeXXH64() error = %v", err)
	}

	expectedHash := "48dc1684324621de"
	if hash != expectedHash {
		t.Errorf("ComputeXXH64() = %v, want %v", hash, expectedHash)
	}

	t.Logf("Go XXH64 hash:    %s", hash)
	t.Logf("Expected hash:    %s", expectedHash)
}

func TestComputeXXH64_Format(t *testing.T) {
	wavFile := filepath.Join("..", "audio", "N14-2025-02-25-20241116_054500-685-703.wav")

	hash, err := ComputeXXH64(wavFile)
	if err != nil {
		t.Fatalf("ComputeXXH64() error = %v", err)
	}

	// Verify hash is exactly 16 characters
	if len(hash) != 16 {
		t.Errorf("hash length = %d, want 16", len(hash))
	}

	// Verify it's valid lowercase hexadecimal
	for _, c := range hash {
		if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
			t.Errorf("invalid hex character '%c' in hash %s", c, hash)
		}
	}

	t.Logf("Hash format validated: %s", hash)
}

func TestComputeXXH64_FileNotFound(t *testing.T) {
	_, err := ComputeXXH64("nonexistent-file.wav")
	if err == nil {
		t.Error("expected error for nonexistent file, got nil")
	}
}

func TestComputeXXH64_EmptyFile(t *testing.T) {
	// Create a temporary empty file
	tmpDir := t.TempDir()
	emptyFile := filepath.Join(tmpDir, "empty.wav")

	// Create empty file
	if err := createEmptyFile(emptyFile); err != nil {
		t.Fatalf("Failed to create empty file: %v", err)
	}

	hash, err := ComputeXXH64(emptyFile)
	if err != nil {
		t.Fatalf("ComputeXXH64() error = %v", err)
	}

	// XXH64 hash of empty file with seed=0 is a known value
	expectedEmpty := "ef46db3751d8e999"
	if hash != expectedEmpty {
		t.Errorf("ComputeXXH64(empty file) = %v, want %v", hash, expectedEmpty)
	}
}

func TestComputeXXH64_Deterministic(t *testing.T) {
	wavFile := filepath.Join("..", "audio", "N14-2025-02-25-20241116_054500-685-703.wav")

	// Compute hash multiple times
	hash1, err := ComputeXXH64(wavFile)
	if err != nil {
		t.Fatalf("ComputeXXH64() first call error = %v", err)
	}

	hash2, err := ComputeXXH64(wavFile)
	if err != nil {
		t.Fatalf("ComputeXXH64() second call error = %v", err)
	}

	hash3, err := ComputeXXH64(wavFile)
	if err != nil {
		t.Fatalf("ComputeXXH64() third call error = %v", err)
	}

	// All hashes should be identical (deterministic)
	if hash1 != hash2 || hash2 != hash3 {
		t.Errorf("Hashes are not deterministic: %s, %s, %s", hash1, hash2, hash3)
	}

	t.Logf("Hash is deterministic: %s", hash1)
}

func TestComputeXXH64_LeadingZeros(t *testing.T) {
	// Create a small file that might produce a hash with leading zeros
	tmpDir := t.TempDir()
	smallFile := filepath.Join(tmpDir, "small.dat")

	// Write a small amount of data
	if err := createSmallFile(smallFile); err != nil {
		t.Fatalf("Failed to create small file: %v", err)
	}

	hash, err := ComputeXXH64(smallFile)
	if err != nil {
		t.Fatalf("ComputeXXH64() error = %v", err)
	}

	// Verify hash is exactly 16 characters (with zero-padding if needed)
	if len(hash) != 16 {
		t.Errorf("hash length = %d, want 16 (leading zeros should be preserved)", len(hash))
	}
}

// Helper function to create an empty file
func createEmptyFile(path string) error {
	file, err := os.Create(path)
	if err != nil {
		return err
	}
	return file.Close()
}

// Helper function to create a small test file
func createSmallFile(path string) error {
	file, err := os.Create(path)
	if err != nil {
		return err
	}
	defer file.Close()

	// Write a single byte
	_, err = file.Write([]byte{0x42})
	return err
}