<?php
/**
 * Provide a public-facing view for the client menu.
 *
 * @package Noesia_Smart_Waiter
 */

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

$noesia_swa_client_atts = isset( $atts ) && is_array( $atts ) ? $atts : array();
( static function ( $noesia_swa_client_atts ) {
    $atts = $noesia_swa_client_atts;

    $opt = get_option( 'noesia_swa_options' );

    if ( ! is_array( $opt ) ) {
        $opt = array();
    }

    $shop       = ! empty( $opt['shop_name'] ) ? $opt['shop_name'] : 'Welcome';
    $logo       = ! empty( $opt['logo_url'] ) ? $opt['logo_url'] : '';
    $review_url = ! empty( $opt['review_url'] ) ? $opt['review_url'] : '';
    $rate_label = ! empty( $opt['rate_label'] ) ? $opt['rate_label'] : 'Rate Us on Google';

    $menu_active = isset( $opt['menu_active'] ) && in_array( (string) $opt['menu_active'], array( 'yes', '1' ), true );
    $menu_url    = ! empty( $opt['menu_url'] ) ? $opt['menu_url'] : '';
    $btn_menu    = ! empty( $opt['btn_menu'] ) ? $opt['btn_menu'] : 'View Our Menu';
    $icon_menu   = ! empty( $opt['icon_menu'] ) ? $opt['icon_menu'] : '📜';

    $btn_order   = ! empty( $opt['btn_order'] ) ? $opt['btn_order'] : 'New Order';
    $icon_order  = ! empty( $opt['icon_order'] ) ? $opt['icon_order'] : '🍽️';
    $btn_waiter  = ! empty( $opt['btn_waiter'] ) ? $opt['btn_waiter'] : 'Call Waiter';
    $icon_waiter = ! empty( $opt['icon_waiter'] ) ? $opt['icon_waiter'] : '🙋';
    $btn_bill    = ! empty( $opt['btn_bill'] ) ? $opt['btn_bill'] : 'Bill Please';
    $icon_bill   = ! empty( $opt['icon_bill'] ) ? $opt['icon_bill'] : '💳';

  $get_public_location_from_query = static function () {
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only public display-state parameter used only to label the client menu location/table.
	if ( ! isset( $_GET['loc'] ) ) {
		return '';
	}

	// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only public display-state parameter used only to label the client menu location/table.
	$location = sanitize_text_field( wp_unslash( (string) $_GET['loc'] ) );

	return '' !== $location ? $location : '';
};

$default_loc = isset( $atts['location'] ) && is_scalar( $atts['location'] )
	? sanitize_text_field( (string) $atts['location'] )
	: 'Table';

$location = $get_public_location_from_query();

if ( '' === $location ) {
	$location = $default_loc;
}

    $context = array(
        'surface'  => 'client',
        'atts'     => $atts,
        'options'  => $opt,
        'shop'     => $shop,
        'logo'     => $logo,
        'location' => $location,
    );
    ?>

    <?php do_action( 'noesia_swa_public_before_content', $context ); ?>

    <div class="ns-app-wrapper" data-noesia-swa-public-surface="client">
        <?php do_action( 'noesia_swa_public_client_before_header', $context ); ?>

        <div class="ns-app-header">
            <?php if ( $logo ) : ?>
                <img src="<?php echo esc_url( $logo ); ?>" class="ns-app-logo" alt="<?php echo esc_attr( $shop ); ?>">
            <?php endif; ?>

            <h1 class="ns-app-title"><?php echo esc_html( $shop ); ?></h1>
            <span class="ns-app-location"><?php echo esc_html( $location ); ?></span>
        </div>

        <?php do_action( 'noesia_swa_public_client_after_header', $context ); ?>

        <div class="ns-app-grid">
            <?php do_action( 'noesia_swa_public_before_main_buttons', $context ); ?>

            <?php if ( $menu_active && $menu_url ) : ?>
                <a href="<?php echo esc_url( $menu_url ); ?>" target="_blank" rel="noopener noreferrer" class="ns-card-btn ns-full-width ns-btn-menu">
                    <span class="ns-icon"><?php echo esc_html( $icon_menu ); ?></span>
                    <span class="ns-label"><?php echo esc_html( $btn_menu ); ?></span>
                </a>
            <?php endif; ?>

            <button class="ns-card-btn ns-action-btn" data-type="order" data-label="<?php echo esc_attr( $btn_order ); ?>">
                <span class="ns-icon"><?php echo esc_html( $icon_order ); ?></span>
                <span class="ns-label"><?php echo esc_html( $btn_order ); ?></span>
            </button>

            <button class="ns-card-btn ns-action-btn" data-type="waiter" data-label="<?php echo esc_attr( $btn_waiter ); ?>">
                <span class="ns-icon"><?php echo esc_html( $icon_waiter ); ?></span>
                <span class="ns-label"><?php echo esc_html( $btn_waiter ); ?></span>
            </button>

            <?php if ( ! empty( $opt['cst1_label'] ) ) : ?>
                <button class="ns-card-btn ns-action-btn" data-type="custom1" data-label="<?php echo esc_attr( $opt['cst1_label'] ); ?>">
                    <span class="ns-icon"><?php echo esc_html( ! empty( $opt['cst1_icon'] ) ? $opt['cst1_icon'] : '⚡' ); ?></span>
                    <span class="ns-label"><?php echo esc_html( $opt['cst1_label'] ); ?></span>
                </button>
            <?php endif; ?>

            <?php if ( ! empty( $opt['cst2_label'] ) ) : ?>
                <button class="ns-card-btn ns-action-btn" data-type="custom2" data-label="<?php echo esc_attr( $opt['cst2_label'] ); ?>">
                    <span class="ns-icon"><?php echo esc_html( ! empty( $opt['cst2_icon'] ) ? $opt['cst2_icon'] : '✨' ); ?></span>
                    <span class="ns-label"><?php echo esc_html( $opt['cst2_label'] ); ?></span>
                </button>
            <?php endif; ?>

            <button class="ns-card-btn ns-full-width ns-action-btn ns-text-danger" data-type="bill" data-label="<?php echo esc_attr( $btn_bill ); ?>">
                <span class="ns-icon"><?php echo esc_html( $icon_bill ); ?></span>
                <span class="ns-label"><?php echo esc_html( $btn_bill ); ?></span>
            </button>

            <?php do_action( 'noesia_swa_public_after_main_buttons', $context ); ?>

            <?php if ( $review_url ) : ?>
                <a href="<?php echo esc_url( $review_url ); ?>" target="_blank" rel="noopener noreferrer" class="ns-review-card">
                    <div class="ns-review-left">
                        <span class="ns-review-star">⭐</span>
                        <span class="ns-review-text"><?php echo esc_html( $rate_label ); ?></span>
                    </div>
                    <div class="ns-review-arrow">➜</div>
                </a>
            <?php endif; ?>
        </div>

        <?php do_action( 'noesia_swa_public_before_footer', $context ); ?>

        <input type="hidden" class="ns-location-input" value="<?php echo esc_attr( $location ); ?>">
    </div>

    <?php do_action( 'noesia_swa_public_after_content', $context ); ?>
    <?php
} )( $noesia_swa_client_atts );
