<?php
/**
 * Public/front-end handlers for Noesia Smart Waiter.
 *
 * Transitional module-local public runtime owned by the Design module until
 * the Public/App layer is extracted into its own fully isolated module.
 *
 * Kept with the same class name for backward compatibility while ownership
 * moves out of the central includes/ kernel.
 *
 * @package Noesia_Smart_Waiter
 */

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

if ( ! class_exists( 'Noesia_SWA_Public' ) ) {

    class Noesia_SWA_Public {

        /**
         * Wire up public hooks.
         */
        public function __construct() {
            add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_public_assets' ) );
            add_shortcode( 'noesia_swa_client', array( $this, 'render_client_shortcode' ) );
            add_shortcode( 'noesia_swa_dashboard', array( $this, 'render_dashboard_shortcode' ) );
        }

        /**
         * Enqueue public CSS/JS.
         */
        public function enqueue_public_assets() {
            if ( ! defined( 'NOESIA_SWA_PLUGIN_DIR' ) || ! defined( 'NOESIA_SWA_PLUGIN_URL' ) ) {
                return;
            }

            $css_path = NOESIA_SWA_PLUGIN_DIR . 'public/css/noesia-public.css';
            $js_path  = NOESIA_SWA_PLUGIN_DIR . 'public/js/noesia-public.js';

            $css_ver = file_exists( $css_path ) ? (string) filemtime( $css_path ) : ( defined( 'NOESIA_SWA_VERSION' ) ? NOESIA_SWA_VERSION : false );
            $js_ver  = file_exists( $js_path ) ? (string) filemtime( $js_path ) : ( defined( 'NOESIA_SWA_VERSION' ) ? NOESIA_SWA_VERSION : false );

            wp_enqueue_style(
                'noesia-public-css',
                NOESIA_SWA_PLUGIN_URL . 'public/css/noesia-public.css',
                array(),
                $css_ver
            );

            $opt = get_option( 'noesia_swa_options', array() );
            if ( ! is_array( $opt ) ) {
                $opt = array();
            }

            $col_bg    = ! empty( $opt['color_bg'] ) ? sanitize_hex_color( (string) $opt['color_bg'] ) : '';
            $col_title = ! empty( $opt['color_title'] ) ? sanitize_hex_color( (string) $opt['color_title'] ) : '';
            $col_text  = ! empty( $opt['color_text'] ) ? sanitize_hex_color( (string) $opt['color_text'] ) : '';

            if ( empty( $col_bg ) ) {
                $col_bg = '#121212';
            }
            if ( empty( $col_title ) ) {
                $col_title = '#ffffff';
            }
            if ( empty( $col_text ) ) {
                $col_text = '#ffffff';
            }

            $custom_css = ':root { --ns-dyn-bg: ' . $col_bg . '; --ns-dyn-title: ' . $col_title . '; --ns-dyn-text: ' . $col_text . '; }';
            wp_add_inline_style( 'noesia-public-css', $custom_css );

            wp_enqueue_script(
                'noesia-public-js',
                NOESIA_SWA_PLUGIN_URL . 'public/js/noesia-public.js',
                array( 'jquery' ),
                $js_ver,
                true
            );

            wp_localize_script(
                'noesia-public-js',
                'noesia_vars',
                array(
                    'ajax_url'          => admin_url( 'admin-ajax.php' ),
                    'nonce'             => wp_create_nonce( 'noesia_swa_nonce' ),
                    'lbl_sound_on'      => __( '🔊 Sound On', 'noesia-smart-waiter' ),
                    'lbl_sound_off'     => __( '🔇 Sound Off', 'noesia-smart-waiter' ),
                    'msg_conn_error'    => __( '❌ Connection Error', 'noesia-smart-waiter' ),
                    'msg_timeout'       => __( '❌ Server Timeout', 'noesia-smart-waiter' ),
                    'msg_all_quiet'     => __( 'All quiet... ☕', 'noesia-smart-waiter' ),
                    'btn_complete'      => __( '✓ Complete', 'noesia-smart-waiter' ),
                    'lbl_unknown'       => __( 'Unknown', 'noesia-smart-waiter' ),
                    'msg_error_gen'     => __( '❌ Error', 'noesia-smart-waiter' ),
                    'msg_audio_blocked' => __( 'Audio blocked. Click OK to enable.', 'noesia-smart-waiter' ),
                )
            );
        }

        /**
         * Shortcode: [noesia_swa_client]
         *
         * @param array $atts Shortcode attributes.
         * @return string
         */
        public function render_client_shortcode( $atts ) {
            if ( ! defined( 'NOESIA_SWA_PLUGIN_DIR' ) ) {
                return '';
            }

            $atts = shortcode_atts(
                array(
                    'location' => 'Table',
                ),
                (array) $atts,
                'noesia_swa_client'
            );

            // Keep variable name `$atts` for the included template.
            $atts['location'] = sanitize_text_field( (string) $atts['location'] );

            ob_start();
            include NOESIA_SWA_PLUGIN_DIR . 'public/partials/client-display.php';
            return (string) ob_get_clean();
        }

        /**
         * Shortcode: [noesia_swa_dashboard]
         *
         * @param array $atts Shortcode attributes.
         * @return string
         */
        public function render_dashboard_shortcode( $atts ) {
            if ( ! defined( 'NOESIA_SWA_PLUGIN_DIR' ) ) {
                return '';
            }

            $atts = shortcode_atts(
                array(
                    'demo' => 'false',
                ),
                (array) $atts,
                'noesia_swa_dashboard'
            );

            $demo_raw = strtolower( sanitize_text_field( (string) $atts['demo'] ) );
            $is_demo  = ( 'true' === $demo_raw );
            $is_admin = current_user_can( 'manage_options' );

            if ( ! $is_admin && ! $is_demo ) {
                return '<p>' . esc_html__( 'Authorized Personnel Only.', 'noesia-smart-waiter' ) . '</p>';
            }

            ob_start();
            include NOESIA_SWA_PLUGIN_DIR . 'public/partials/dashboard-display.php';
            return (string) ob_get_clean();
        }
    }
}
