package debpack

import (
	"strings"
	"testing"
)

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

	got := DefaultLayout()
	if got.CLIPath != "/usr/bin/mirage" {
		t.Fatalf("CLIPath = %q, want /usr/bin/mirage", got.CLIPath)
	}
	if got.DaemonPath != "/usr/bin/miraged" {
		t.Fatalf("DaemonPath = %q, want /usr/bin/miraged", got.DaemonPath)
	}
	if got.LauncherPath != "/usr/bin/mirage-qt" {
		t.Fatalf("LauncherPath = %q, want /usr/bin/mirage-qt", got.LauncherPath)
	}
	if got.UIPath != "/usr/lib/mirage/mirage-qt-ui" {
		t.Fatalf("UIPath = %q, want /usr/lib/mirage/mirage-qt-ui", got.UIPath)
	}
	if got.ServicePath != "/lib/systemd/system/miraged.service" {
		t.Fatalf("ServicePath = %q, want /lib/systemd/system/miraged.service", got.ServicePath)
	}
	if got.DesktopPath != "/usr/share/applications/mirage-qt-ui.desktop" {
		t.Fatalf("DesktopPath = %q, want /usr/share/applications/mirage-qt-ui.desktop", got.DesktopPath)
	}
	if got.AppIconPath != "/usr/share/pixmaps/mirage.png" {
		t.Fatalf("AppIconPath = %q, want /usr/share/pixmaps/mirage.png", got.AppIconPath)
	}
	if got.DefaultsPath != "/etc/default/miraged" {
		t.Fatalf("DefaultsPath = %q, want /etc/default/miraged", got.DefaultsPath)
	}
}

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

	control := ControlFile(ControlMetadata{
		Package:      "mirage-qt",
		Version:      "1.92.5",
		Architecture: "amd64",
		Depends:      []string{"libqt6core6", "policykit-1", "xdg-utils"},
	})

	for _, want := range []string{
		"Package: mirage-qt\n",
		"Version: 1.92.5\n",
		"Architecture: amd64\n",
		"Depends: libqt6core6, policykit-1, xdg-utils\n",
		"Description: Mirage Linux tray client\n",
	} {
		if !strings.Contains(control, want) {
			t.Fatalf("control file missing %q:\n%s", want, control)
		}
	}
}

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

	service := ServiceFile()
	for _, want := range []string{
		"ExecStart=/usr/bin/miraged --state=/var/lib/mirage/miraged.state --socket=/run/mirage/miraged.sock --port=${PORT} $FLAGS",
		"ExecStopPost=/usr/bin/miraged --cleanup",
		"RuntimeDirectory=mirage",
		"StateDirectory=mirage",
		"CacheDirectory=mirage",
	} {
		if !strings.Contains(service, want) {
			t.Fatalf("service file missing %q:\n%s", want, service)
		}
	}
}

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

	defaults := DefaultsFile()
	for _, want := range []string{
		`PORT="41641"`,
		`FLAGS=""`,
	} {
		if !strings.Contains(defaults, want) {
			t.Fatalf("defaults file missing %q:\n%s", want, defaults)
		}
	}
}

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

	postinst := PostinstScript()
	for _, want := range []string{
		"systemctl daemon-reload >/dev/null 2>&1 || true",
		"systemctl enable miraged.service >/dev/null 2>&1 || true",
		"systemctl restart miraged.service >/dev/null 2>&1 || systemctl start miraged.service >/dev/null 2>&1 || true",
	} {
		if !strings.Contains(postinst, want) {
			t.Fatalf("postinst missing %q:\n%s", want, postinst)
		}
	}
}
