<?php

namespace Booktics\Service\Templates;

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

use Booktics\Contracts\Hookable_Service_Contract;

/**
 * Single_Service_Template class handles custom template loading for single service pages
 *
 * @package Booktics\Service\Templates
 */
class Single_Service_Template implements Hookable_Service_Contract {

    /**
     * Register template filter hook
     *
     * @return void
     */
    public function register() {
        add_filter(
            'template_include', array(
				$this,
				'custom_single_service_template',
            )
        );
    }

    /**
     * Loads custom template for single service pages
     *
     * @param string $template Current template path
     *
     * @return string Modified template path
     */
    public function custom_single_service_template( $template ) {
        if ( is_single() ) {
            global $post; // Add this line to access the global $post variable

            // Check if the post type is 'booktics-service'
            if ( 'booktics-service' !== $post->post_type ) {
                return $template;
            }
            if ( $post->post_status === 'deactive' && ! current_user_can( 'manage_options' ) ) {
                wp_head();
                ?>
                <div
                    class="booktics-shortcode-wrapper booktics-shortcode-notice-wrapper">
                    <div>
                        <h3><?php echo esc_html__( 'Service Unavailable', 'booktics' ); ?></h3>
                    </div>
                </div>
                <?php
                wp_footer();
                exit;
            }
            // Load a custom template file
            $custom_template = BOOKTICS_DIR . '/templates/service/custom-single-service-template.php';

            if ( file_exists( $custom_template ) ) {
                return $custom_template;
            }
        }

        // Return the original template if no custom template is found
        return $template;
    }
}
