package codeserver

import (
	"fmt"
	"path/filepath"

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

/*
   Install installs the code-server addon.

   IMPORTANT:
   - Addon-only (not devcontainer-specific)
   - No assumptions about users or workspaces
   - Idempotent via addon marker
*/
func Install(cfg state.Config) error {

	if addons.IsAddonProvisioned("codeserver") {
		return nil
	}

	scriptPath := filepath.Join(
		provision.ScriptsRoot,
		"addons",
		"codeserver",
		"install.sh",
	)

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

	if err := addons.WriteAddonMarker("codeserver"); err != nil {
		return fmt.Errorf("failed to write codeserver marker: %w", err)
	}

	return nil
}
