package main

import (
	"archive/zip"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

func TestWintunZipMemberForArch(t *testing.T) {
	t.Parallel()

	tests := []struct {
		arch string
		want string
	}{
		{arch: "amd64", want: "wintun/bin/amd64/wintun.dll"},
		{arch: "386", want: "wintun/bin/x86/wintun.dll"},
		{arch: "arm64", want: "wintun/bin/arm64/wintun.dll"},
		{arch: "arm", want: "wintun/bin/arm/wintun.dll"},
	}

	for _, tt := range tests {
		tt := tt
		t.Run(tt.arch, func(t *testing.T) {
			t.Parallel()

			got, err := wintunZipMemberForArch(tt.arch)
			if err != nil {
				t.Fatalf("wintunZipMemberForArch(%q): %v", tt.arch, err)
			}
			if got != tt.want {
				t.Fatalf("wintunZipMemberForArch(%q) = %q, want %q", tt.arch, got, tt.want)
			}
		})
	}
}

func TestWintunZipMemberForArchRejectsUnsupportedArch(t *testing.T) {
	t.Parallel()

	_, err := wintunZipMemberForArch("riscv64")
	if err == nil {
		t.Fatal("wintunZipMemberForArch unexpectedly succeeded for unsupported arch")
	}
	if !strings.Contains(err.Error(), "unsupported arch") {
		t.Fatalf("wintunZipMemberForArch returned unexpected error: %v", err)
	}
}

func TestStageWintunAssets(t *testing.T) {
	t.Parallel()

	tempDir := t.TempDir()
	zipPath := filepath.Join(tempDir, "wintun.zip")
	stageDir := filepath.Join(tempDir, "stage")

	if err := writeZipArchive(zipPath, map[string]string{
		"wintun/bin/amd64/wintun.dll": "dll-bytes",
		"wintun/LICENSE.txt":          "license-text",
	}); err != nil {
		t.Fatalf("writeZipArchive: %v", err)
	}

	if err := stageWintunAssets(zipPath, "amd64", stageDir); err != nil {
		t.Fatalf("stageWintunAssets: %v", err)
	}

	dllBytes, err := os.ReadFile(filepath.Join(stageDir, "wintun.dll"))
	if err != nil {
		t.Fatalf("ReadFile(wintun.dll): %v", err)
	}
	if string(dllBytes) != "dll-bytes" {
		t.Fatalf("unexpected wintun.dll contents %q", string(dllBytes))
	}

	licenseBytes, err := os.ReadFile(filepath.Join(stageDir, "LICENSE.wintun.txt"))
	if err != nil {
		t.Fatalf("ReadFile(LICENSE.wintun.txt): %v", err)
	}
	if string(licenseBytes) != "license-text" {
		t.Fatalf("unexpected LICENSE.wintun.txt contents %q", string(licenseBytes))
	}
}

func TestStageWintunAssetsFailsWhenDLLMissing(t *testing.T) {
	t.Parallel()

	tempDir := t.TempDir()
	zipPath := filepath.Join(tempDir, "wintun.zip")
	stageDir := filepath.Join(tempDir, "stage")

	if err := writeZipArchive(zipPath, map[string]string{
		"wintun/LICENSE.txt": "license-text",
	}); err != nil {
		t.Fatalf("writeZipArchive: %v", err)
	}

	err := stageWintunAssets(zipPath, "amd64", stageDir)
	if err == nil {
		t.Fatal("stageWintunAssets unexpectedly succeeded without wintun.dll")
	}
	if !strings.Contains(err.Error(), "wintun/bin/amd64/wintun.dll") {
		t.Fatalf("stageWintunAssets returned unexpected error: %v", err)
	}
}

func writeZipArchive(zipPath string, entries map[string]string) error {
	f, err := os.Create(zipPath)
	if err != nil {
		return err
	}
	defer f.Close()

	zw := zip.NewWriter(f)
	defer zw.Close()

	for name, contents := range entries {
		w, err := zw.Create(name)
		if err != nil {
			return err
		}
		if _, err := w.Write([]byte(contents)); err != nil {
			return err
		}
	}
	return nil
}
