package web

import (
	"testing"
)

func TestCleanAppVersion(t *testing.T) {
	tests := []struct {
		name     string
		version  string
		expected string
	}{
		{
			name:     "Empty version",
			version:  "",
			expected: "",
		},
		{
			name:     "Version with package name prefix - mpv",
			version:  "mpv 1:0.41.0-2.1",
			expected: "1:0.41.0-2.1",
		},
		{
			name:     "Version with package name prefix - celluloid",
			version:  "celluloid 0.29-1.1",
			expected: "0.29-1.1",
		},
		{
			name:     "Version without package name prefix",
			version:  "1:0.41.0-2.1",
			expected: "1:0.41.0-2.1",
		},
		{
			name:     "Simple version number",
			version:  "1.0.0",
			expected: "1.0.0",
		},
		{
			name:     "Version with multiple spaces",
			version:  "mpv 1:0.41.0-2.1 extra",
			expected: "1:0.41.0-2.1 extra",
		},
		{
			name:     "Single word version",
			version:  "dev",
			expected: "dev",
		},
	}

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

func TestCompareVersions(t *testing.T) {
	tests := []struct {
		name      string
		current   string
		available string
		expected  bool
	}{
		{
			name:      "Both empty",
			current:   "",
			available: "",
			expected:  false,
		},
		{
			name:      "Current empty, available valid",
			current:   "",
			available: "1.0.0",
			expected:  false,
		},
		{
			name:      "Available empty",
			current:   "1.0.0",
			available: "",
			expected:  false,
		},
		{
			name:      "Available is 'Check via Package Manager'",
			current:   "1.0.0",
			available: "Check via Package Manager",
			expected:  false,
		},
		{
			name:      "Different versions",
			current:   "1.0.0",
			available: "1.1.0",
			expected:  true,
		},
		{
			name:      "Same versions",
			current:   "1.0.0",
			available: "1.0.0",
			expected:  false,
		},
		{
			name:      "Package version formats",
			current:   "1:0.41.0-2.1",
			available: "1:0.42.0-1.1",
			expected:  true,
		},
		{
			name:      "Same package version",
			current:   "1:0.41.0-2.1",
			available: "1:0.41.0-2.1",
			expected:  false,
		},
	}

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

func TestGetPackageManagerType(t *testing.T) {
	tests := []struct {
		name     string
		methodID string
		expected string
	}{
		{
			name:     "Flatpak",
			methodID: "mpv-flatpak",
			expected: "Flatpak",
		},
		{
			name:     "Package Manager",
			methodID: "mpv-package",
			expected: "Package Manager",
		},
		{
			name:     "Brew",
			methodID: "mpv-brew",
			expected: "Brew",
		},
		{
			name:     "Celluloid Flatpak",
			methodID: "celluloid-flatpak",
			expected: "Flatpak",
		},
		{
			name:     "Unknown method",
			methodID: "mpv-binary",
			expected: "",
		},
		{
			name:     "Empty method",
			methodID: "",
			expected: "",
		},
	}

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