<?php
/**
 * Link-health status classifier.
 *
 * @package Affilun\Application\LinkHealth
 */

declare(strict_types=1);

namespace Affilun\Application\LinkHealth;

/**
 * Normalizes Cloud link-health result fields into UI buckets.
 */
final class LinkHealthStatusClassifier {

	/**
	 * Classify a Cloud status token and HTTP status code.
	 *
	 * @param string $status     Cloud status token.
	 * @param int    $httpStatus HTTP status code, or 0 when absent.
	 *
	 * @return 'ok'|'warning'|'broken'|'unknown'
	 */
	public static function classify( string $status, int $httpStatus ): string {
		$normalized = strtolower( trim( $status ) );
		if ( in_array( $normalized, array( 'ok', 'healthy', 'success' ), true ) && $httpStatus < 400 ) {
			return 'ok';
		}
		if ( in_array( $normalized, array( 'broken', 'failed', 'error' ), true ) || $httpStatus >= 400 ) {
			return 'broken';
		}
		if (
			in_array( $normalized, array( 'redirected', 'warning', 'timeout' ), true )
			|| $httpStatus >= 300
		) {
			return 'warning';
		}

		return 'unknown';
	}
}
