package virtualmachine

import (
	"strings"
)

// ErrorCategory represents a categorized error with appropriate condition type and reason
type ErrorCategory struct {
	Type   string
	Reason string
}

// categorizeError categorizes errors into appropriate types for better handling
func categorizeError(errorStr string) ErrorCategory {
	errorStr = strings.ToLower(errorStr)

	// API not implemented errors
	if strings.Contains(errorStr, "501") || 
	   strings.Contains(errorStr, "not implemented") || 
	   strings.Contains(errorStr, "importdisk") {
		return ErrorCategory{
			Type:   "APINotSupported",
			Reason: "ImportDiskAPINotImplemented",
		}
	}

	// Configuration errors (non-retryable without manual intervention)
	if strings.Contains(errorStr, "cannot get provider config") ||
	   strings.Contains(errorStr, "cannot get credentials") ||
	   strings.Contains(errorStr, "cannot find site") ||
	   strings.Contains(errorStr, "cannot create proxmox client") {
		return ErrorCategory{
			Type:   "ConfigurationError",
			Reason: "InvalidConfiguration",
		}
	}

	// Quota errors
	if strings.Contains(errorStr, "quota") ||
	   strings.Contains(errorStr, "exceeded") {
		return ErrorCategory{
			Type:   "QuotaExceeded",
			Reason: "ResourceQuotaExceeded",
		}
	}

	// Node health errors
	if strings.Contains(errorStr, "node") && 
	   (strings.Contains(errorStr, "unhealthy") || 
	    strings.Contains(errorStr, "not reachable") ||
	    strings.Contains(errorStr, "offline")) {
		return ErrorCategory{
			Type:   "NodeUnhealthy",
			Reason: "NodeHealthCheckFailed",
		}
	}

	// Image/Storage errors
	if strings.Contains(errorStr, "image") && 
	   (strings.Contains(errorStr, "not found") ||
	    strings.Contains(errorStr, "cannot find")) {
		return ErrorCategory{
			Type:   "ImageNotFound",
			Reason: "ImageNotFoundInStorage",
		}
	}

	// Lock file errors
	if strings.Contains(errorStr, "lock") ||
	   strings.Contains(errorStr, "timeout") {
		return ErrorCategory{
			Type:   "LockError",
			Reason: "LockFileTimeout",
		}
	}

	// Authentication errors (non-retryable without credential fix)
	if strings.Contains(errorStr, "authentication") ||
	   strings.Contains(errorStr, "unauthorized") ||
	   strings.Contains(errorStr, "401") ||
	   strings.Contains(errorStr, "invalid credentials") ||
	   strings.Contains(errorStr, "forbidden") ||
	   strings.Contains(errorStr, "403") {
		return ErrorCategory{
			Type:   "AuthenticationError",
			Reason: "AuthenticationFailed",
		}
	}

	// Network/Connection errors (retryable)
	if strings.Contains(errorStr, "network") ||
	   strings.Contains(errorStr, "connection") ||
	   strings.Contains(errorStr, "timeout") ||
	   strings.Contains(errorStr, "502") ||
	   strings.Contains(errorStr, "503") ||
	   strings.Contains(errorStr, "connection refused") ||
	   strings.Contains(errorStr, "connection reset") {
		return ErrorCategory{
			Type:   "NetworkError",
			Reason: "TransientNetworkFailure",
		}
	}

	// Generic creation failure
	if strings.Contains(errorStr, "cannot create vm") ||
	   strings.Contains(errorStr, "failed to create") {
		return ErrorCategory{
			Type:   "CreationFailed",
			Reason: "VMCreationFailed",
		}
	}

	// Default: generic failure
	return ErrorCategory{
		Type:   "Failed",
		Reason: "UnknownError",
	}
}

