// Installer contract for version reporting and launch paths.
//
// Scope: keep client/server install scripts aligned with Cargo package
// versions and the paths used by desktop/systemd launchers.
// Targets: install scripts plus the Linux desktop entry.
// Why: users should be able to rerun installers idempotently and see the same
// version that the installed binary was built from.

const CLIENT_INSTALL: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/install/client.sh"
));
const SERVER_INSTALL: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/scripts/install/server.sh"
));
const CLIENT_DESKTOP: &str = include_str!(concat!(
    env!("CARGO_MANIFEST_DIR"),
    "/client/assets/linux/lesavka.desktop"
));
const CLIENT_CARGO: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/client/Cargo.toml"));
const SERVER_CARGO: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/server/Cargo.toml"));
const COMMON_CARGO: &str = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/common/Cargo.toml"));

fn package_version(manifest: &str) -> &str {
    manifest
        .lines()
        .find_map(|line| {
            let (key, value) = line.trim().split_once('=')?;
            (key.trim() == "version").then(|| value.trim().trim_matches('"'))
        })
        .expect("package version")
}

#[test]
fn workspace_runtime_crates_share_one_release_version() {
    let client_version = package_version(CLIENT_CARGO);
    assert_eq!(client_version, package_version(SERVER_CARGO));
    assert_eq!(client_version, package_version(COMMON_CARGO));
}

#[test]
fn client_install_reports_the_manifest_version_and_current_revision() {
    for marker in [
        "manifest_package_version \"$SRC/client/Cargo.toml\"",
        "INSTALLED_SHA=$(source_revision \"$SRC\")",
        "Installed: lesavka-client",
        "Installed version: lesavka-client",
        "Build source: $SRC/target/release/lesavka-client",
    ] {
        assert!(
            CLIENT_INSTALL.contains(marker),
            "client installer should preserve version/reporting marker {marker}"
        );
    }
}

#[test]
fn server_install_reports_the_manifest_version_and_current_revision() {
    for marker in [
        "manifest_package_version \"$SRC_DIR/server/Cargo.toml\"",
        "INSTALLED_SHA=$(git -C \"$SCRIPT_REPO_ROOT\" rev-parse --short HEAD",
        "Installed: lesavka-server",
        "Installed version: lesavka-server",
        "Client install can use:",
    ] {
        assert!(
            SERVER_INSTALL.contains(marker),
            "server installer should preserve version/reporting marker {marker}"
        );
    }
}

#[test]
fn desktop_and_terminal_launch_paths_point_at_the_installed_client_binary() {
    for marker in [
        "install_verified_executable \"$SRC/target/release/lesavka-client\" /usr/local/bin/lesavka-client",
        "ln -sf /usr/local/bin/lesavka-client /usr/local/bin/lesavka",
        "ln -sf /usr/local/bin/lesavka-client \"$USER_HOME/.local/bin/lesavka-client\"",
        "Desktop entry: /usr/share/applications/lesavka.desktop",
    ] {
        assert!(
            CLIENT_INSTALL.contains(marker),
            "client installer should preserve launch path marker {marker}"
        );
    }

    assert!(CLIENT_DESKTOP.contains("Exec=env GSK_RENDERER=gl /usr/local/bin/lesavka"));
    assert!(CLIENT_DESKTOP.contains("Icon=lesavka"));
}

#[test]
fn server_installs_all_systemd_entrypoint_binaries_from_the_same_build() {
    for marker in [
        "install_verified_executable \"$SRC_DIR/target/release/lesavka-server\" /usr/local/bin/lesavka-server",
        "install_verified_executable \"$SRC_DIR/target/release/lesavka-uvc\" /usr/local/bin/lesavka-uvc",
        "install_verified_executable \"$SRC_DIR/scripts/daemon/lesavka-core.sh\" /usr/local/bin/lesavka-core.sh",
        "install_verified_executable \"$SRC_DIR/scripts/daemon/lesavka-uvc.sh\" /usr/local/bin/lesavka-uvc.sh",
        "install_verified_executable \"$SRC_DIR/scripts/daemon/lesavka-recovery-ladder.sh\" /usr/local/bin/lesavka-recovery-ladder",
        "/usr/local/bin/lesavka-server",
        "/usr/local/bin/lesavka-uvc",
        "/usr/local/bin/lesavka-core.sh",
        "/usr/local/bin/lesavka-uvc.sh",
        "/usr/local/bin/lesavka-recovery-ladder",
    ] {
        assert!(
            SERVER_INSTALL.contains(marker),
            "server installer should preserve binary path marker {marker}"
        );
    }
}
