<?php
/**
 * Block-pattern registry.
 *
 * Registers the plugin's "Auto Dealer" pattern category plus two ready-made
 * patterns editors can drop onto a page:
 *
 *   • inventory-page — search form + vehicle grid (full archive layout).
 *   • single-vehicle  — card + specs + dealer info + inquiry form.
 *
 * @package ThemeREX\AutoDealer
 * @version 1.0.0
 */

declare(strict_types=1);

namespace ThemeREX\AutoDealer\Blocks;

defined( 'ABSPATH' ) || exit;
final class PatternRegistry {

	public const CATEGORY = 'trx-auto-dealer';

	public function register(): void {
		add_action( 'init', array( $this, 'registerCategory' ), 9 );
		add_action( 'init', array( $this, 'registerPatterns' ), 11 );
	}

	public function registerCategory(): void {
		if ( ! function_exists( 'register_block_pattern_category' ) ) {
			return;
		}
		register_block_pattern_category(
			self::CATEGORY,
			array( 'label' => __( 'Auto Dealer', 'trx-auto-dealer' ) )
		);
	}

	public function registerPatterns(): void {
		if ( ! function_exists( 'register_block_pattern' ) ) {
			return;
		}

		register_block_pattern(
			'trx-auto-dealer/inventory-page',
			array(
				'title'       => __( 'Inventory Page', 'trx-auto-dealer' ),
				'description' => __( 'Vehicle search form above a 3-column grid of listings.', 'trx-auto-dealer' ),
				'categories'  => array( self::CATEGORY ),
				'keywords'    => array( 'vehicles', 'inventory', 'archive', 'search' ),
				'content'     => $this->inventoryPageContent(),
			)
		);

		register_block_pattern(
			'trx-auto-dealer/single-vehicle',
			array(
				'title'       => __( 'Single Vehicle Page', 'trx-auto-dealer' ),
				'description' => __( 'Vehicle card + specs table on the left, dealer info + inquiry form on the right.', 'trx-auto-dealer' ),
				'categories'  => array( self::CATEGORY ),
				'keywords'    => array( 'vehicle', 'detail', 'single', 'spec sheet' ),
				'content'     => $this->singleVehicleContent(),
			)
		);
	}

	private function inventoryPageContent(): string {
		$heading = esc_html__( 'Browse Our Inventory', 'trx-auto-dealer' );
		$intro   = esc_html__( 'Find your next vehicle from our current selection.', 'trx-auto-dealer' );

		$parts   = array();
		$parts[] = '<!-- wp:group {"layout":{"type":"constrained"},"className":"trx-ad-pattern-inventory"} -->';
		$parts[] = '<div class="wp-block-group trx-ad-pattern-inventory">';
		$parts[] = '<!-- wp:heading {"level":1} -->';
		$parts[] = '<h1 class="wp-block-heading">' . $heading . '</h1>';
		$parts[] = '<!-- /wp:heading -->';
		$parts[] = '<!-- wp:paragraph -->';
		$parts[] = '<p>' . $intro . '</p>';
		$parts[] = '<!-- /wp:paragraph -->';
		$parts[] = '<!-- wp:trx-auto-dealer/search-form /-->';
		$parts[] = '<!-- wp:spacer {"height":"24px"} -->';
		$parts[] = '<div style="height:24px" aria-hidden="true" class="wp-block-spacer"></div>';
		$parts[] = '<!-- /wp:spacer -->';
		$parts[] = '<!-- wp:trx-auto-dealer/vehicle-grid {"perPage":9,"columns":3,"orderby":"date","order":"DESC"} /-->';
		$parts[] = '</div>';
		$parts[] = '<!-- /wp:group -->';

		return implode( "\n", $parts );
	}

	private function singleVehicleContent(): string {
		$inquire = esc_html__( 'Inquire About This Vehicle', 'trx-auto-dealer' );

		$contact_block = sprintf(
			'<!-- wp:trx-auto-dealer/contact-form {"formId":"inquiry","title":"%s"} /-->',
			$inquire
		);

		$parts   = array();
		$parts[] = '<!-- wp:group {"layout":{"type":"constrained"},"className":"trx-ad-pattern-single"} -->';
		$parts[] = '<div class="wp-block-group trx-ad-pattern-single">';
		$parts[] = '<!-- wp:trx-auto-dealer/vehicle-card /-->';
		$parts[] = '<!-- wp:columns {"className":"trx-ad-pattern-single__body"} -->';
		$parts[] = '<div class="wp-block-columns trx-ad-pattern-single__body">';
		$parts[] = '<!-- wp:column {"width":"66.66%"} -->';
		$parts[] = '<div class="wp-block-column" style="flex-basis:66.66%">';
		$parts[] = '<!-- wp:trx-auto-dealer/specs-table /-->';
		$parts[] = '</div>';
		$parts[] = '<!-- /wp:column -->';
		$parts[] = '<!-- wp:column {"width":"33.33%"} -->';
		$parts[] = '<div class="wp-block-column" style="flex-basis:33.33%">';
		$parts[] = '<!-- wp:trx-auto-dealer/dealer-info /-->';
		$parts[] = '<!-- wp:spacer {"height":"24px"} -->';
		$parts[] = '<div style="height:24px" aria-hidden="true" class="wp-block-spacer"></div>';
		$parts[] = '<!-- /wp:spacer -->';
		$parts[] = $contact_block;
		$parts[] = '</div>';
		$parts[] = '<!-- /wp:column -->';
		$parts[] = '</div>';
		$parts[] = '<!-- /wp:columns -->';
		$parts[] = '</div>';
		$parts[] = '<!-- /wp:group -->';

		return implode( "\n", $parts );
	}
}
