package main

import (
	"fmt"
	"os"
)

// Mock labels for testing
var testLabels = map[string]map[string]string{
	"redhat-bootc": {
		"com.redhat.bootc": "true",
		"ostree.bootable":  "true",
	},
	"debian-bootc": {
		"com.debian.bootc": "true",
		"ostree.bootable":  "true",
	},
	"both-labels": {
		"com.redhat.bootc": "true",
		"com.debian.bootc": "true",
		"ostree.bootable":  "true",
	},
	"no-bootc": {
		"some.other.label": "value",
	},
	"no-ostree": {
		"com.debian.bootc": "true",
	},
}

func isBootcImage(labels map[string]string) bool {
	// Check for Red Hat bootc label
	if val, exists := labels["com.redhat.bootc"]; exists && val == "true" {
		return true
	}
	
	// Check for Debian bootc label
	if val, exists := labels["com.debian.bootc"]; exists && val == "true" {
		return true
	}
	
	return false
}

func validateBootcImage(labels map[string]string, imageRef string) error {
	if !isBootcImage(labels) {
		return fmt.Errorf("image %s is not a bootc image (missing com.redhat.bootc=true or com.debian.bootc=true label)", imageRef)
	}
	
	// Check for required OSTree labels
	if val, exists := labels["ostree.bootable"]; !exists || val != "true" {
		return fmt.Errorf("image %s is not a bootc image (missing ostree.bootable=true label)", imageRef)
	}
	
	return nil
}

func getBootcType(labels map[string]string) string {
	if val, exists := labels["com.redhat.bootc"]; exists && val == "true" {
		return "redhat"
	}
	
	if val, exists := labels["com.debian.bootc"]; exists && val == "true" {
		return "debian"
	}
	
	return "unknown"
}

func main() {
	fmt.Println("Testing Debian bootc validation logic...")
	fmt.Println("")
	
	tests := []struct {
		name   string
		labels map[string]string
		expect bool
	}{
		{"Red Hat bootc", testLabels["redhat-bootc"], true},
		{"Debian bootc", testLabels["debian-bootc"], true},
		{"Both labels", testLabels["both-labels"], true},
		{"No bootc", testLabels["no-bootc"], false},
		{"No ostree", testLabels["no-ostree"], true}, // Should be true because it has bootc label
	}
	
	passed := 0
	total := len(tests)
	
	for _, test := range tests {
		fmt.Printf("Test: %s\n", test.name)
		fmt.Printf("Labels: %v\n", test.labels)
		
		isBootc := isBootcImage(test.labels)
		bootcType := getBootcType(test.labels)
		err := validateBootcImage(test.labels, "test-image")
		
		fmt.Printf("Is bootc: %t (expected: %t)\n", isBootc, test.expect)
		fmt.Printf("Bootc type: %s\n", bootcType)
		fmt.Printf("Validation error: %v\n", err)
		
		if isBootc == test.expect {
			fmt.Printf("✅ PASS\n")
			passed++
		} else {
			fmt.Printf("❌ FAIL\n")
		}
		fmt.Println("")
	}
	
	fmt.Printf("Test Results: %d/%d passed\n", passed, total)
	
	if passed == total {
		fmt.Println("🎉 All validation logic tests passed!")
		os.Exit(0)
	} else {
		fmt.Println("❌ Some validation logic tests failed")
		os.Exit(1)
	}
}
