<?php
if ( ! defined( 'ABSPATH' ) ) exit;

class ROS_Shortcodes {

    private static $is_customer = false;
    private static $is_kitchen  = false;

    public function __construct() {
        add_shortcode( 'restaurant_customer', [ $this, 'customer_frontend' ] );
        add_shortcode( 'restaurant_kitchen',  [ $this, 'kitchen_frontend' ] );
        add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] );

        // Hook early to detect shortcode on current page, then override template
        add_action( 'template_redirect', [ $this, 'detect_and_override' ] );
        add_action( 'wp_head',           [ $this, 'inject_fullpage_css' ] );
    }

    /**
     * Detect if the current page contains one of our shortcodes.
     * If so, register a custom full-page template via template_include.
     */
    public function detect_and_override() {
        global $post;
        if ( ! $post ) return;

        $content = $post->post_content;

        if ( has_shortcode( $content, 'restaurant_customer' ) ) {
            self::$is_customer = true;
            add_filter( 'template_include', [ $this, 'fullpage_template' ] );
        } elseif ( has_shortcode( $content, 'restaurant_kitchen' ) ) {
            self::$is_kitchen = true;
            add_filter( 'template_include', [ $this, 'fullpage_template' ] );
        }
    }

    /**
     * Return our own standalone HTML template — completely bypasses the theme.
     */
    public function fullpage_template( $template ) {
        $our_template = ROS_PATH . 'templates/fullpage.php';
        if ( file_exists( $our_template ) ) {
            return $our_template;
        }
        return $template;
    }

    /**
     * Aggressive CSS reset injected into <head> that nukes every theme wrapper
     * even when we cannot replace the template (edge cases / page builders).
     */
    public function inject_fullpage_css() {
        if ( ! self::$is_customer && ! self::$is_kitchen ) return;
        echo '<style id="ros-fullpage-kill">
/* Kill every possible theme wrapper */
html, body { margin:0!important; padding:0!important; background:#F8F6F2!important; overflow-x:hidden!important; }
body.ros-kitchen-body, html.ros-kitchen-body { background:#111!important; }
/* Hide theme elements — NO wildcard class*= selectors (they kill our own classes too) */
header, footer, #header, #footer, #masthead, #colophon,
.site-header, .site-footer, .navbar, .nav-bar, .top-bar,
.header-wrap, .footer-wrap, #wpadminbar,
nav.navigation, .main-navigation, #site-navigation,
.wp-block-template-part, #secondary, .sidebar, aside { display:none!important; }
/* Our own elements must never be hidden */
.ros-wrap, .ros-wrap * { visibility:visible; }
</style>' . "\n";
    }

    public function enqueue() {
        wp_enqueue_style( 'ros-style', ROS_URL . 'public/css/ros-style.css', [], ROS_VERSION );
        wp_enqueue_script( 'ros-shapes',   ROS_URL . 'public/js/ros-table-shapes.js',  [], ROS_VERSION, true );
        wp_enqueue_script( 'ros-customer', ROS_URL . 'public/js/ros-customer.js', [ 'jquery', 'ros-shapes' ], ROS_VERSION, true );
        wp_enqueue_script( 'ros-kitchen',  ROS_URL . 'public/js/ros-kitchen.js',  [ 'jquery', 'ros-shapes' ], ROS_VERSION, true );
        $data = [
            'ajax_url' => admin_url( 'admin-ajax.php' ),
            'nonce'    => wp_create_nonce( 'ros_nonce' ),
        ];
        wp_localize_script( 'ros-customer', 'rosData', $data );
        wp_localize_script( 'ros-kitchen',  'rosData', $data );
    }

    public function customer_frontend() {
        ob_start();
        include ROS_PATH . 'templates/customer/customer.php';
        return ob_get_clean();
    }

    public function kitchen_frontend() {
        ob_start();
        include ROS_PATH . 'templates/kitchen/kitchen.php';
        return ob_get_clean();
    }

    public static function is_customer() { return self::$is_customer; }
    public static function is_kitchen()  { return self::$is_kitchen; }
}
