package java

import (
	"fmt"
	"path/filepath"

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

/*
   Install installs the Java dev container runtime.

   Execution model:
   - Uses local, versioned install scripts from the agent repo
   - Does NOT assume Minecraft semantics
   - Provides a general-purpose Java environment for development

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

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

	scriptPath := filepath.Join(
		provision.ScriptsRoot,
		"devcontainer",
		"java",
		"install.sh",
	)

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

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

	return nil
}
