package debianpatch

import (
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
)

type ContainerInspect struct {
	Labels map[string]string `json:"Labels"`
}

func main() {
	if len(os.Args) != 2 {
		fmt.Println("Usage: go run test_validation.go <image-tag>")
		os.Exit(1)
	}
	
	imageTag := os.Args[1]
	
	// Inspect the container image
	cmd := exec.Command("podman", "inspect", imageTag)
	output, err := cmd.Output()
	if err != nil {
		fmt.Printf("Error inspecting image %s: %v\n", imageTag, err)
		os.Exit(1)
	}
	
	// Parse the JSON output
	var containers []ContainerInspect
	if err := json.Unmarshal(output, &containers); err != nil {
		fmt.Printf("Error parsing JSON: %v\n", err)
		os.Exit(1)
	}
	
	if len(containers) == 0 {
		fmt.Printf("No container information found for %s\n", imageTag)
		os.Exit(1)
	}
	
	labels := containers[0].Labels
	fmt.Printf("Image: %s\n", imageTag)
	fmt.Printf("Labels: %v\n", labels)
	
	// Test our validation logic
	isBootc := false
	bootcType := "unknown"
	
	if val, exists := labels["com.redhat.bootc"]; exists && val == "true" {
		isBootc = true
		bootcType = "redhat"
	}
	
	if val, exists := labels["com.debian.bootc"]; exists && val == "true" {
		isBootc = true
		bootcType = "debian"
	}
	
	hasOstreeBootable := false
	if val, exists := labels["ostree.bootable"]; exists && val == "true" {
		hasOstreeBootable = true
	}
	
	fmt.Printf("Is bootc image: %t\n", isBootc)
	fmt.Printf("Bootc type: %s\n", bootcType)
	fmt.Printf("Has ostree.bootable: %t\n", hasOstreeBootable)
	
	if isBootc && hasOstreeBootable {
		fmt.Printf("✅ Image %s is a valid bootc image\n", imageTag)
		if bootcType == "debian" {
			fmt.Printf("✅ Image %s is specifically a Debian bootc image\n", imageTag)
		}
	} else {
		fmt.Printf("❌ Image %s is not a valid bootc image\n", imageTag)
		os.Exit(1)
	}
}
