<?php
/**
 * DESCRIPTION: Registers block patterns and pattern categories for event layouts
 * used with the Nemtly Events query loop variation and single event templates.
 *
 * @package NemtlyBooking\Core
 */

namespace NemtlyBooking\Core;

/**
 * Class to manage block patterns.
 *
 * @since PBOOKING_SINCE
 */
class Pattern_Manager {

	/**
	 * Initialize patterns.
	 *
	 * @since PBOOKING_SINCE
	 */
	public function init() {
		add_action( 'init', [ $this, 'register_pattern_category' ] );
		add_action( 'init', [ $this, 'register_patterns' ] );
	}

	/**
	 * Register pattern category.
	 *
	 * @since PBOOKING_SINCE
	 */
	public function register_pattern_category() {
		register_block_pattern_category(
			'nemtly-events',
			[ 'label' => __( 'Nemtly Events', 'nemtly-booking' ) ]
		);
	}

	/**
	 * Register patterns.
	 *
	 * @since PBOOKING_SINCE
	 */
	public function register_patterns() {
		$this->register_event_card_grid();
		$this->register_event_overlay_grid();
		$this->register_sidebar_event_info();
	}

	/**
	 * Register Event Card Grid pattern (3-column grid with image, date, title, excerpt, price).
	 *
	 * @since PBOOKING_SINCE
	 */
	private function register_event_card_grid() {
		register_block_pattern(
			'nemtly-booking/event-card-grid',
			[
				'title'       => __( 'Event Card Grid', 'nemtly-booking' ),
				'description' => __( 'A 3-column card grid showing event image, date, title, excerpt, and price.', 'nemtly-booking' ),
				'categories'  => [ 'nemtly-events' ],
				'blockTypes'  => [ 'core/query' ],
				'content'     => '<!-- wp:query {"query":{"postType":"nemtly_event","perPage":6,"offset":0,"order":"asc","orderBy":"date","inherit":false},"namespace":"nemtly/events","displayLayout":{"type":"flex","columns":3}} -->
<div class="wp-block-query"><!-- wp:post-template {"layout":{"type":"grid","columnCount":3}} -->
<!-- wp:group {"style":{"border":{"width":"1px","color":"#e5e7eb","radius":"8px"},"spacing":{"padding":{"top":"0","right":"0","bottom":"0","left":"0"}}}} -->
<div class="wp-block-group" style="border-width:1px;border-color:#e5e7eb;border-radius:8px;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0"><!-- wp:post-featured-image {"isLink":true,"height":"200px","style":{"border":{"radius":{"topLeft":"8px","topRight":"8px"}}}} /-->

<!-- wp:group {"style":{"spacing":{"padding":{"top":"1.25rem","right":"1.25rem","bottom":"1.25rem","left":"1.25rem"},"blockGap":"0.5rem"}}} -->
<div class="wp-block-group" style="padding-top:1.25rem;padding-right:1.25rem;padding-bottom:1.25rem;padding-left:1.25rem"><!-- wp:nemtly/event-date {"showTime":false,"showIcon":true,"style":{"typography":{"fontSize":"0.875rem"},"color":{"text":"#6b7280"}}} /-->

<!-- wp:post-title {"isLink":true,"level":3,"style":{"spacing":{"margin":{"top":"0.25rem","bottom":"0.5rem"}}}} /-->

<!-- wp:post-excerpt {"moreText":"","excerptLength":20} /-->

<!-- wp:nemtly/price /--></div>
<!-- /wp:group --></div>
<!-- /wp:group -->
<!-- /wp:post-template -->

<!-- wp:query-pagination {"layout":{"type":"flex","justifyContent":"center"}} -->
<!-- wp:query-pagination-previous /-->

<!-- wp:query-pagination-numbers /-->

<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination -->

<!-- wp:query-no-results -->
<!-- wp:paragraph {"placeholder":"Add text or blocks that will display when a query returns no results."} -->
<p>' . esc_html__( 'No events found.', 'nemtly-booking' ) . '</p>
<!-- /wp:paragraph -->
<!-- /wp:query-no-results --></div>
<!-- /wp:query -->',
			]
		);
	}

	/**
	 * Register Event Overlay Grid pattern (image background with title overlay and meta row below).
	 *
	 * @since PBOOKING_SINCE
	 */
	private function register_event_overlay_grid() {
		register_block_pattern(
			'nemtly-booking/event-overlay-grid',
			[
				'title'       => __( 'Event Overlay Grid', 'nemtly-booking' ),
				'description' => __( 'A 3-column grid with featured image as background, title overlay, and event details below.', 'nemtly-booking' ),
				'categories'  => [ 'nemtly-events' ],
				'blockTypes'  => [ 'core/query' ],
				'content'     => '<!-- wp:query {"query":{"postType":"nemtly_event","perPage":9,"offset":0,"order":"asc","orderBy":"date","inherit":false},"namespace":"nemtly/events","displayLayout":{"type":"flex","columns":3}} -->
<div class="wp-block-query"><!-- wp:post-template {"layout":{"type":"grid","columnCount":3}} -->
<!-- wp:group {"style":{"border":{"radius":"12px"},"spacing":{"padding":{"top":"0","right":"0","bottom":"0","left":"0"},"blockGap":"0"},"shadow":"0 1px 3px rgba(0,0,0,0.12)"}} -->
<div class="wp-block-group" style="border-radius:12px;padding-top:0;padding-right:0;padding-bottom:0;padding-left:0;box-shadow:0 1px 3px rgba(0,0,0,0.12)"><!-- wp:cover {"useFeaturedImage":true,"dimRatio":40,"overlayColor":"black","isUserOverlayColor":true,"minHeight":220,"minHeightUnit":"px","contentPosition":"bottom left","isDark":true,"style":{"border":{"radius":{"topLeft":"12px","topRight":"12px"}},"spacing":{"padding":{"top":"1rem","right":"1rem","bottom":"1rem","left":"1rem"}}}} -->
<div class="wp-block-cover is-light has-custom-content-position is-position-bottom-left" style="border-top-left-radius:12px;border-top-right-radius:12px;padding-top:1rem;padding-right:1rem;padding-bottom:1rem;padding-left:1rem;min-height:220px"><span aria-hidden="true" class="wp-block-cover__background has-black-background-color has-background-dim-40 has-background-dim"></span><div class="wp-block-cover__inner-container"><!-- wp:post-title {"isLink":true,"level":3,"style":{"typography":{"fontSize":"1.125rem","fontWeight":"600"},"elements":{"link":{"color":{"text":"#ffffff"}}},"color":{"text":"#ffffff"},"spacing":{"margin":{"top":"0","bottom":"0"}}}} /-->

<!-- wp:nemtly/event-location {"showIcon":true,"style":{"typography":{"fontSize":"0.8125rem"},"color":{"text":"#d1d5db"},"spacing":{"margin":{"top":"0.25rem"}}}} /--></div></div>
<!-- /wp:cover -->

<!-- wp:group {"style":{"spacing":{"padding":{"top":"0.75rem","right":"1rem","bottom":"0.75rem","left":"1rem"}},"border":{"radius":{"bottomLeft":"12px","bottomRight":"12px"}}},"backgroundColor":"white","layout":{"type":"flex","flexWrap":"nowrap","justifyContent":"space-between"}} -->
<div class="wp-block-group has-white-background-color has-background" style="border-bottom-left-radius:12px;border-bottom-right-radius:12px;padding-top:0.75rem;padding-right:1rem;padding-bottom:0.75rem;padding-left:1rem"><!-- wp:nemtly/event-date {"showTime":true,"showIcon":true,"style":{"typography":{"fontSize":"0.8125rem"},"color":{"text":"#374151"}}} /-->

<!-- wp:nemtly/price {"style":{"typography":{"fontSize":"0.8125rem","fontWeight":"600"},"color":{"text":"#374151"}}} /--></div>
<!-- /wp:group --></div>
<!-- /wp:group -->
<!-- /wp:post-template -->

<!-- wp:query-pagination {"layout":{"type":"flex","justifyContent":"center"}} -->
<!-- wp:query-pagination-previous /-->

<!-- wp:query-pagination-numbers /-->

<!-- wp:query-pagination-next /-->
<!-- /wp:query-pagination -->

<!-- wp:query-no-results -->
<!-- wp:paragraph {"placeholder":"Add text or blocks that will display when a query returns no results."} -->
<p>' . esc_html__( 'No events found.', 'nemtly-booking' ) . '</p>
<!-- /wp:paragraph -->
<!-- /wp:query-no-results --></div>
<!-- /wp:query -->',
			]
		);
	}

	/**
	 * Register Sidebar Event Info pattern (vertical stack for single event sidebars).
	 *
	 * @since PBOOKING_SINCE
	 */
	private function register_sidebar_event_info() {
		register_block_pattern(
			'nemtly-booking/sidebar-event-info',
			[
				'title'       => __( 'Sidebar Event Info', 'nemtly-booking' ),
				'description' => __( 'Vertical stack of event details for sidebars.', 'nemtly-booking' ),
				'categories'  => [ 'nemtly-events' ],
				'content'     => '<!-- wp:group {"style":{"border":{"width":"1px","radius":"8px"},"spacing":{"padding":{"top":"1.5rem","right":"1.5rem","bottom":"1.5rem","left":"1.5rem"}}},"backgroundColor":"base"} -->
<div class="wp-block-group has-base-background-color has-background" style="border-width:1px;border-radius:8px;padding-top:1.5rem;padding-right:1.5rem;padding-bottom:1.5rem;padding-left:1.5rem"><!-- wp:heading {"level":3,"style":{"spacing":{"margin":{"top":"0"}}}} -->
<h3 class="wp-block-heading" style="margin-top:0">' . esc_html__( 'Event Details', 'nemtly-booking' ) . '</h3>
<!-- /wp:heading -->

<!-- wp:nemtly/price {"style":{"typography":{"fontSize":"2rem","fontWeight":"700"}}} /-->

<!-- wp:spacer {"height":"1rem"} /-->

<!-- wp:nemtly/event-date {"showTime":true,"showIcon":true} /-->

<!-- wp:nemtly/event-location {"showIcon":true} /-->

<!-- wp:spacer {"height":"2rem"} /-->

<!-- wp:nemtly/booking-trigger {"text":"' . esc_attr__( 'Book Now', 'nemtly-booking' ) . '"} /--></div>
<!-- /wp:group -->',
			]
		);
	}
}
