package inject

import (
	"fmt"
	"os"
	"path/filepath"
)

// Injector writes node config into a mounted image (boot/root paths supplied by caller).
type Injector struct {
	BootPath string
	RootPath string
}

// FileSpec describes a file to write.
type FileSpec struct {
	Path    string
	Content []byte
	Mode    os.FileMode
	RootFS  bool // if true, write under root path; else boot path
}

// Write materializes the requested files under the boot or root mount because
// the burn flow needs a single place to stage config fragments before sync.
func (i *Injector) Write(files []FileSpec) error {
	for _, f := range files {
		base := i.BootPath
		if f.RootFS {
			base = i.RootPath
		}
		target := filepath.Join(base, f.Path)
		if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
			return fmt.Errorf("mkdir %s: %w", filepath.Dir(target), err)
		}
		if err := os.WriteFile(target, f.Content, f.Mode); err != nil {
			return fmt.Errorf("write %s: %w", target, err)
		}
	}
	return nil
}
