package jdenticon

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

func TestJavaScriptReferenceCompatibility(t *testing.T) {
	testCases := []struct {
		input string
		size  int
	}{
		{"test-hash", 64},
		{"example1@gmail.com", 64},
		{"example2@yahoo.com", 64},
	}

	for _, tc := range testCases {
		t.Run(tc.input+"_"+strconv.Itoa(tc.size), func(t *testing.T) {
			// Generate Go SVG with context
			goSvg, err := ToSVG(context.Background(), tc.input, tc.size)
			if err != nil {
				t.Fatalf("Failed to generate Go SVG: %v", err)
			}

			// Read reference JavaScript SVG
			var refFilename string
			if tc.input == "test-hash" {
				refFilename = "test-hash_64.svg"
			} else if tc.input == "example1@gmail.com" {
				refFilename = "example1_at_gmail_com_64.svg"
			} else if tc.input == "example2@yahoo.com" {
				refFilename = "example2_at_yahoo_com_64.svg"
			}

			refPath := filepath.Join("../reference", refFilename)
			refData, err := os.ReadFile(refPath)
			if err != nil {
				t.Skipf("Reference file not found: %s", refPath)
				return
			}
			refSvg := string(refData)

			// Compare
			if goSvg != refSvg {
				t.Errorf("SVG output differs from JavaScript reference")
				t.Logf("Go output:\n%s", goSvg)
				t.Logf("JS reference:\n%s", refSvg)

				// Save Go output for manual inspection
				goPath := filepath.Join("../go-output", refFilename)
				os.MkdirAll(filepath.Dir(goPath), 0755)
				os.WriteFile(goPath, []byte(goSvg), 0644)
				t.Logf("Go output saved to: %s", goPath)
			}
		})
	}
}
