<?php
declare(strict_types=1);

namespace Wohno\Plugin\Block;

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

/**
 * Ready-to-use block patterns that drop the `wohno/listings` block with sensible
 * preset attributes — so users can insert a complete listings section in one
 * click instead of configuring the block from scratch.
 */
final class Patterns {

	private const CATEGORY = 'wohno';

	/**
	 * Hook pattern registration into `init` (after the block is registered).
	 *
	 * @return void
	 */
	public function register(): void {
		add_action( 'init', array( $this, 'register_patterns' ), 11 );
	}

	/**
	 * Register the Wohno pattern category and patterns.
	 *
	 * @return void
	 */
	public function register_patterns(): void {
		if ( ! function_exists( 'register_block_pattern' ) || ! function_exists( 'register_block_pattern_category' ) ) {
			return;
		}

		register_block_pattern_category(
			self::CATEGORY,
			array( 'label' => __( 'Wohno', 'wohno' ) )
		);

		$patterns = array(
			array(
				'title'       => __( 'Wohno: Inserate-Raster', 'wohno' ),
				'description' => __( 'Raster der Inserate in drei Spalten.', 'wohno' ),
				'attrs'       => array(
					'layout'  => 'grid',
					'columns' => 3,
					'limit'   => 9,
				),
			),
			array(
				'title'       => __( 'Wohno: Große Karten', 'wohno' ),
				'description' => __( 'Bildbetonte, große Inserate-Karten.', 'wohno' ),
				'attrs'       => array(
					'layout' => 'cards',
					'limit'  => 6,
				),
			),
			array(
				'title'       => __( 'Wohno: Suche & Liste', 'wohno' ),
				'description' => __( 'Filterleiste mit Inseraten als Liste — die komplette Suche für deine Besucher.', 'wohno' ),
				'attrs'       => array(
					'layout' => 'list',
					'search' => true,
					'limit'  => 12,
				),
			),
			array(
				'title'       => __( 'Wohno: Karte', 'wohno' ),
				'description' => __( 'Interaktive Karte mit allen Inseraten.', 'wohno' ),
				'attrs'       => array( 'layout' => 'map' ),
			),
		);

		foreach ( $patterns as $pattern ) {
			register_block_pattern(
				'wohno/' . sanitize_key( (string) $pattern['attrs']['layout'] ) . '-' . ( $pattern['attrs']['search'] ?? false ? 'search' : 'plain' ),
				array(
					'title'       => $pattern['title'],
					'description' => $pattern['description'],
					'categories'  => array( self::CATEGORY ),
					'content'     => '<!-- wp:wohno/listings ' . (string) wp_json_encode( $pattern['attrs'] ) . ' /-->',
				)
			);
		}
	}
}
