<?php
/**
 * DESCRIPTION: Serves the events archive and single-event views on classic
 * (non-block) themes, with WooCommerce-style child/parent/plugin overriding.
 *
 * @package Nemtly_Booking
 */

namespace NemtlyBooking\Core;

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

/**
 * Classic theme template loader.
 *
 * Block themes render the events archive and single event through the plugin's
 * FSE templates (see Block_Template_Manager). Classic themes have no Site
 * Editor, so this loader supplies matching PHP templates and lets a theme
 * override them by dropping a file in its own `nemtly-booking/` folder —
 * the same pattern WooCommerce and EDD use.
 *
 * The layouts themselves live once, in the block (.html) templates. The
 * classic PHP templates reuse that block markup so there is a single source
 * of truth; the active theme's header/footer wrap it.
 *
 * @since PBOOKING_SINCE
 */
class Classic_Template_Loader {

    /**
     * Register hooks — only on classic themes.
     *
     * @since PBOOKING_SINCE
     *
     * @return void
     */
    public function init() {
        if ( wp_is_block_theme() ) {
            return;
        }

        add_filter( 'template_include', [ $this, 'load_template' ] );
        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_block_styles' ] );
    }

    /**
     * Swap in our archive/single template for nemtly_event requests.
     *
     * @since PBOOKING_SINCE
     *
     * @param string $template Template path WordPress resolved.
     *
     * @return string
     */
    public function load_template( $template ) {
        if ( is_singular( 'nemtly_event' ) ) {
            return $this->locate( 'single-nemtly_event.php' );
        }

        if ( is_post_type_archive( 'nemtly_event' ) ) {
            return $this->locate( 'archive-nemtly_event.php' );
        }

        return $template;
    }

    /**
     * Find a template: child theme → parent theme → plugin.
     *
     * A theme overrides any template by placing it at
     * `wp-content/themes/<theme>/nemtly-booking/<name>`. The plugin's own
     * classic templates live in `templates/classic/`.
     *
     * @since PBOOKING_SINCE
     *
     * @param string $name Template file name, e.g. 'single-nemtly_event.php'.
     *
     * @return string Absolute path to the template to load.
     */
    public function locate( $name ) {
        return Template::locate( $name, 'classic' );
    }

    /**
     * Render a block template's body, minus its header/footer template parts.
     *
     * Reuses the FSE (.html) layout so classic and block themes stay visually
     * identical. On single views a post id is passed so the inner post blocks
     * (title, content, price, booking trigger) resolve their context.
     *
     * @since PBOOKING_SINCE
     *
     * @param string $slug    Block template slug, e.g. 'single-nemtly_event'.
     * @param int    $post_id Optional post id to provide as block context.
     *
     * @return string Rendered HTML.
     */
    public static function render_block_body( $slug, $post_id = 0 ) {
        $file = NEMTLY_BOOKING_PATH . 'templates/' . $slug . '.html';

        if ( ! is_readable( $file ) ) {
            return '';
        }

        $markup = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Reading a bundled plugin template file.

        // Drop the block-theme header/footer parts; the classic theme supplies those.
        $markup = preg_replace( '#<!--\s*wp:template-part\b.*?/-->#s', '', $markup );

        if ( ! $post_id ) {
            return do_blocks( $markup );
        }

        // Provide post context so core post-* blocks render on single views.
        $context = [
            'postId'   => $post_id,
            'postType' => get_post_type( $post_id ),
        ];

        $html = '';
        foreach ( parse_blocks( $markup ) as $block ) {
            $html .= ( new \WP_Block( $block, $context ) )->render();
        }

        return $html;
    }

    /**
     * Load core block styles on classic themes that don't enqueue them.
     *
     * @since PBOOKING_SINCE
     *
     * @return void
     */
    public function enqueue_block_styles() {
        if ( is_singular( 'nemtly_event' ) || is_post_type_archive( 'nemtly_event' ) ) {
            wp_enqueue_style( 'wp-block-library' );
        }
    }
}
