<?php
/**
 * Reports malformed graph shapes collected during normalization.
 *
 * @package SEOvaultSEO\Schema\Validation\Rules\Core
 */

namespace SEOvaultSEO\Schema\Validation\Rules\Core;

use SEOvaultSEO\Schema\Validation\RuleInterface;
use SEOvaultSEO\Schema\Validation\Severity;
use SEOvaultSEO\Schema\Validation\ValidationContext;
use SEOvaultSEO\Schema\Validation\ValidationIssue;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Converts non-mutating normalizer observations into structured issues.
 */
class GraphShapeRule implements RuleInterface {

	/**
	 * Get the stable rule code.
	 *
	 * @return string
	 */
	public function get_code() {
		return 'graph.shape';
	}

	/**
	 * Graph-shape checks apply to every graph.
	 *
	 * @param ValidationContext $context Validation context.
	 * @return bool
	 */
	public function supports( ValidationContext $context ) {
		return true;
	}

	/**
	 * Report normalizer shape problems.
	 *
	 * @param ValidationContext $context Validation context.
	 * @return ValidationIssue[]
	 */
	public function evaluate( ValidationContext $context ) {
		$graph    = $context->get_graph();
		$problems = $graph['shape_problems'] ?? array();
		$issues   = array();

		foreach ( $problems as $problem ) {
			$code     = isset( $problem['code'] ) ? $problem['code'] : 'graph.shape.invalid';
			$path     = isset( $problem['path'] ) ? $problem['path'] : '';
			$issues[] = new ValidationIssue(
				array(
					'code'        => $code,
					'severity'    => Severity::ERROR,
					'category'    => 'core',
					'path'        => $path,
					'source'      => $context->get_source(),
					'title_key'   => $code . '.title',
					'message_key' => $code . '.message',
				)
			);
		}

		return $issues;
	}
}
