<?php

// Scaffold-owned schema plumbing. Extend request/response behavior in the route handlers instead.
function {{phpPrefix}}_load_schema_from_build_dir( $build_dir, $schema_name ) {
	if ( ! is_string( $build_dir ) || '' === $build_dir ) {
		return null;
	}

	$path = $build_dir . '/api-schemas/' . $schema_name . '.schema.json';
	if ( ! is_readable( $path ) ) {
		return null;
	}

	$contents = file_get_contents( $path );
	if ( false === $contents ) {
		return null;
	}

	$decoded = json_decode( $contents, true );
	return is_array( $decoded ) ? $decoded : null;
}

// Keep schema sanitation aligned with generated JSON Schema unless you are debugging bridge behavior.
function {{phpPrefix}}_sanitize_rest_schema( $schema ) {
	if ( ! is_array( $schema ) ) {
		return $schema;
	}

	unset( $schema['$schema'], $schema['title'] );

	if ( isset( $schema['properties'] ) && is_array( $schema['properties'] ) ) {
		foreach ( $schema['properties'] as $key => $property_schema ) {
			$schema['properties'][ $key ] = {{phpPrefix}}_sanitize_rest_schema( $property_schema );
		}
	}

	if ( isset( $schema['items'] ) && is_array( $schema['items'] ) ) {
		$schema['items'] = {{phpPrefix}}_sanitize_rest_schema( $schema['items'] );
	}

	return $schema;
}

// Route handlers call this bridge before product-specific logic runs.
function {{phpPrefix}}_validate_and_sanitize_request( $value, $build_dir, $schema_name, $param_name ) {
	$schema = {{phpPrefix}}_load_schema_from_build_dir( $build_dir, $schema_name );
	if ( ! is_array( $schema ) ) {
		return new WP_Error( 'missing_schema', 'Missing REST schema.', array( 'status' => 500 ) );
	}

	$rest_schema = {{phpPrefix}}_sanitize_rest_schema( $schema );
	$validation  = rest_validate_value_from_schema( $value, $rest_schema, $param_name );
	if ( is_wp_error( $validation ) ) {
		return $validation;
	}

	return rest_sanitize_value_from_schema( $value, $rest_schema, $param_name );
}
