package node

import (
	"fmt"
	"path/filepath"

	"zlh-agent/internal/provision"
	"zlh-agent/internal/provision/devcontainer"
	"zlh-agent/internal/state"
)

/*
   Install installs the Node.js dev container runtime.

   Execution model:
   - Uses local, versioned install scripts from the agent repo
   - Scripts are responsible for fetching artifacts
   - Artifact server is NOT an execution source

   IMPORTANT:
   - This function ONLY installs
   - No verification here
*/
func Install(cfg state.Config) error {

	// Idempotency guard
	if devcontainer.IsProvisioned() {
		return nil
	}

	// Local script path (inside agent repo / container)
	scriptPath := filepath.Join(
		provision.ScriptsRoot,
		"devcontainer",
		"node",
		"install.sh",
	)

	if err := provision.RunScript(scriptPath); err != nil {
		return fmt.Errorf("node devcontainer install failed: %w", err)
	}

	// Mark devcontainer ready
	if err := devcontainer.WriteReadyMarker("node"); err != nil {
		return fmt.Errorf("failed to write devcontainer ready marker: %w", err)
	}

	return nil
}
