<?php
/**
 * Validates date-shaped Schema.org values.
 *
 * @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;
use SEOvaultSEO\Schema\Validation\ValueInspector;

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

/**
 * Reports invalid date and date-time strings without coercing values.
 */
class DateValueRule implements RuleInterface {

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

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

	/**
	 * Validate known date properties.
	 *
	 * @param ValidationContext $context Validation context.
	 * @return ValidationIssue[]
	 */
	public function evaluate( ValidationContext $context ) {
		$issues = array();
		foreach ( $context->get_entities() as $entity ) {
			if ( empty( $entity['is_top_level'] ) ) {
				continue;
			}

			ValueInspector::walk(
				$entity['data'] ?? array(),
				$entity['path'] ?? '',
				function ( $property, $value, $path ) use ( &$issues, $entity, $context ) {
					if ( ! ValueInspector::is_date_property( $property ) || ! is_string( $value ) || ValueInspector::is_empty( $value ) || ValueInspector::is_valid_date( $value ) ) {
						return;
					}

					$issues[] = new ValidationIssue(
						array(
							'code'        => 'schema.value.invalid_date',
							'severity'    => Severity::WARNING,
							'category'    => 'core',
							'entity_type' => implode( ', ', $entity['types'] ?? array() ),
							'entity_id'   => $entity['id'] ?? '',
							'path'        => $path,
							'source'      => $context->get_source(),
							'title_key'   => 'schema.value_invalid_date.title',
							'message_key' => 'schema.value_invalid_date.message',
						)
					);
				}
			);
		}

		return $issues;
	}
}
