package devcontainer

import (
	"fmt"
	"strings"

	"zlh-agent/internal/state"

	devgo "zlh-agent/internal/provision/devcontainer/go"
	"zlh-agent/internal/provision/devcontainer/java"
	"zlh-agent/internal/provision/devcontainer/node"
	"zlh-agent/internal/provision/devcontainer/python"
)

/*
   Provision — entrypoint for dev container provisioning.

   IMPORTANT:
   - This function ONLY performs installation.
   - Verification happens in ensureProvisioned().
   - state.Config is treated as immutable desired state.
   - Routing is based strictly on cfg.Runtime.
*/
func Provision(cfg state.Config) error {

	runtime := strings.ToLower(cfg.Runtime)

	switch runtime {

	case "node":
		if err := node.Install(cfg); err != nil {
			return fmt.Errorf("node devcontainer install failed: %w", err)
		}

	case "python":
		if err := python.Install(cfg); err != nil {
			return fmt.Errorf("python devcontainer install failed: %w", err)
		}

	case "go":
		if err := devgo.Install(cfg); err != nil {
			return fmt.Errorf("go devcontainer install failed: %w", err)
		}

	case "java":
		if err := java.Install(cfg); err != nil {
			return fmt.Errorf("java devcontainer install failed: %w", err)
		}

	default:
		return fmt.Errorf("unsupported dev container runtime: %s", runtime)
	}

	// DO NOT VERIFY HERE.
	// Verification happens in ensureProvisioned().
	return nil
}
