package main

import (
	"context"
	"flag"
	"fmt"
	"os"
	"path/filepath"

	"metis/pkg/plan"
)

func imageCmd(args []string) {
	fs := flag.NewFlagSet("image", flag.ExitOnError)
	invPath := fs.String("inventory", "inventory.yaml", "inventory file")
	node := fs.String("node", "", "target node")
	output := fs.String("output", "", "output raw image path")
	cache := fs.String("cache", filepath.Join(os.TempDir(), "metis-cache"), "image cache dir")
	fs.Parse(args)
	if *node == "" {
		fatalf("--node is required")
	}

	inv := loadInventory(*invPath)
	targetOutput := *output
	if targetOutput == "" {
		targetOutput = filepath.Join("artifacts", fmt.Sprintf("%s.img", *node))
	}

	if err := plan.BuildImageFile(context.Background(), inv, *node, *cache, targetOutput); err != nil {
		fatalf("build image: %v", err)
	}

	fmt.Printf("Wrote %s\n", targetOutput)
	fmt.Println("Injected rootfs recovery config and overlays.")
	fmt.Println("Boot-partition NoCloud files are intentionally skipped for this Armbian rpi4 recovery flow.")
}
