<?php

/*
 * Plugin Name:       Noesia Smart Waiter
 * Plugin URI:        https://noesia.io/
 * Description:       An operating system with a unified app ecosystem, dashboard, and instant search for everyday work.
 * Version:           2.0.7
 * Requires at least: 6.2
 * Tested up to:      7.0
 * Requires PHP:      7.4
 * Author:            Noesia
 * Author URI:        https://noesia.io
 * Text Domain:       noesia-smart-waiter
 * Domain Path:       /languages
 * License:           GPLv2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * WC tested up to:   9.x
 */
if ( !defined( 'ABSPATH' ) ) {
    exit;
}
if ( function_exists( 'noesia_swa_fs' ) ) {
    noesia_swa_fs()->set_basename( false, __FILE__ );
} else {
    if ( !function_exists( 'noesia_swa_fs' ) ) {
        /**
         * Get the Freemius SDK instance.
         *
         * @return Freemius
         */
        function noesia_swa_fs() {
            global $noesia_swa_fs;
            if ( !isset( $noesia_swa_fs ) ) {
                require_once dirname( __FILE__ ) . '/vendor/freemius/start.php';
                $noesia_swa_fs = fs_dynamic_init( array(
                    'id'               => '24352',
                    'slug'             => 'noesia-smart-waiter',
                    'premium_slug'     => 'noesia-smart-waiter-pro',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_73c96e5049fd2e5cc711d04afe041',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => false,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug' => 'noesia_swa_settings',
                    ),
                    'is_live'          => true,
                ) );
            }
            return $noesia_swa_fs;
        }

    }
    noesia_swa_fs();
    do_action( 'noesia_swa_fs_loaded' );
}
if ( !function_exists( 'noesia_swa_has_valid_freemius_premium_access' ) ) {
    /**
     * Determine whether the current runtime has valid Freemius-backed premium access.
     *
     * @return bool
     */
    function noesia_swa_has_valid_freemius_premium_access() {
        if ( !function_exists( 'noesia_swa_fs' ) ) {
            return false;
        }
        $fs = noesia_swa_fs();
        if ( !is_object( $fs ) ) {
            return false;
        }
        $checks = array(
            'can_use_premium_code',
            'has_features_enabled_license',
            'has_active_valid_license',
            'has_any_active_valid_license',
            'is_trial',
            'is_paid_trial'
        );
        foreach ( $checks as $method ) {
            if ( method_exists( $fs, $method ) && true === (bool) $fs->{$method}() ) {
                return true;
            }
        }
        return false;
    }

}
if ( !function_exists( 'noesia_swa_freemius_pro_enabled' ) ) {
    /**
     * Primary Freemius-backed pro gate.
     *
     * @param bool $enabled Current enabled state.
     * @param mixed $context Optional caller context.
     *
     * @return bool
     */
    function noesia_swa_freemius_pro_enabled(  $enabled, $context = null  ) {
        return noesia_swa_has_valid_freemius_premium_access();
    }

}
if ( !function_exists( 'noesia_swa_lock_pro_enabled_to_freemius' ) ) {
    /**
     * Prevent third-party filters from elevating Pro access above Freemius license state.
     *
     * External code may still disable Pro, but cannot force-enable it when Freemius
     * does not report a valid premium/trial entitlement.
     *
     * @param bool $enabled Current enabled state.
     * @param mixed $context Optional caller context.
     *
     * @return bool
     */
    function noesia_swa_lock_pro_enabled_to_freemius(  $enabled, $context = null  ) {
        return (bool) $enabled && noesia_swa_has_valid_freemius_premium_access();
    }

}
add_filter(
    'noesia_swa_pro_enabled',
    'noesia_swa_freemius_pro_enabled',
    10,
    2
);
add_filter(
    'noesia_swa_pro_enabled',
    'noesia_swa_lock_pro_enabled_to_freemius',
    PHP_INT_MAX,
    2
);
define( 'NOESIA_SWA_VERSION', '2.0.7' );
define( 'NOESIA_SWA_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'NOESIA_SWA_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/noesia-swa-uninstall.php';
add_action( 'before_woocommerce_init', 'noesia_swa_declare_hpos_compatibility' );
/**
 * Declare WooCommerce HPOS compatibility.
 *
 * @return void
 */
function noesia_swa_declare_hpos_compatibility() {
    if ( class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
    }
}

require_once NOESIA_SWA_PLUGIN_DIR . 'includes/interface-noesia-swa-module.php';
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-modules.php';
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-admin-sections.php';
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-settings-panels.php';
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-dashboard-widgets.php';
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-admin.php';
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-cpt.php';
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-ajax.php';
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-public.php';
require_once NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-cron.php';
$noesia_swa_optional_bootstrap_files = array(
    NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-dashboard-data.php',
    NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-global-pulse-data.php',
    NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-calculator-data.php',
    NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-settings-shell-data.php',
    NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-settings-section-data.php',
    NOESIA_SWA_PLUGIN_DIR . 'includes/class-noesia-swa-setup-wizard.php'
);
foreach ( $noesia_swa_optional_bootstrap_files as $noesia_swa_optional_bootstrap_file ) {
    if ( file_exists( $noesia_swa_optional_bootstrap_file ) ) {
        require_once $noesia_swa_optional_bootstrap_file;
    }
}
unset($noesia_swa_optional_bootstrap_file);
unset($noesia_swa_optional_bootstrap_files);
if ( !class_exists( 'Noesia_Smart_Waiter' ) ) {
    /**
     * Main plugin bootstrap.
     */
    final class Noesia_Smart_Waiter {
        /**
         * Singleton instance.
         *
         * @var Noesia_Smart_Waiter|null
         */
        private static $instance = null;

        /**
         * Loaded service instances.
         *
         * @var array<string, object>
         */
        private $services = array();

        /**
         * Module manager instance.
         *
         * @var Noesia_SWA_Modules|null
         */
        private $modules = null;

        /**
         * Get plugin singleton.
         *
         * @return Noesia_Smart_Waiter
         */
        public static function get_instance() {
            if ( null === self::$instance ) {
                self::$instance = new self();
            }
            return self::$instance;
        }

        /**
         * Whether the runtime layer has already been booted.
         *
         * @var bool
         */
        private $runtime_booted = false;

        /**
         * Constructor.
         */
        private function __construct() {
            add_action( 'init', array($this, 'load_textdomain') );
            $this->boot_services();
            add_action( 'plugins_loaded', array($this, 'boot_runtime'), 30 );
        }

        /**
         * Boot runtime services that depend on late plugin/filter state.
         *
         * @return void
         */
        public function boot_runtime() {
            if ( $this->runtime_booted ) {
                return;
            }
            $this->runtime_booted = true;
            $this->boot_pro_loader();
            $this->boot_modules();
        }

        /**
         * Load plugin translation files.
         *
         * @return void
         */
        public function load_textdomain() {
            // No-op. WordPress handles plugin translations automatically on WordPress.org.
        }

        /**
         * Instantiate service classes.
         *
         * @return void
         */
        private function boot_services() {
            $this->services['admin_sections'] = new Noesia_SWA_Admin_Sections();
            $this->services['settings_panels'] = new Noesia_SWA_Settings_Panels();
            $this->services['dashboard_widgets'] = new Noesia_SWA_Dashboard_Widgets();
            $this->services['admin'] = new Noesia_SWA_Admin(__FILE__);
            $this->services['cpt'] = new Noesia_SWA_CPT();
            $this->services['ajax'] = new Noesia_SWA_Ajax();
            $this->services['public'] = new Noesia_SWA_Public();
            $this->services['cron'] = new Noesia_SWA_Cron();
            if ( class_exists( 'Noesia_SWA_Dashboard_Data' ) ) {
                $this->services['dashboard_data'] = new Noesia_SWA_Dashboard_Data();
            }
            if ( class_exists( 'Noesia_SWA_Global_Pulse_Data' ) ) {
                $this->services['global_pulse_data'] = new Noesia_SWA_Global_Pulse_Data();
            }
            if ( class_exists( 'Noesia_SWA_Calculator_Data' ) ) {
                $this->services['calculator_data'] = new Noesia_SWA_Calculator_Data();
            }
            if ( class_exists( 'Noesia_SWA_Settings_Shell_Data' ) ) {
                $this->services['settings_shell_data'] = new Noesia_SWA_Settings_Shell_Data();
            }
            if ( class_exists( 'Noesia_SWA_Settings_Section_Data' ) ) {
                $this->services['settings_section_data'] = new Noesia_SWA_Settings_Section_Data();
            }
            if ( class_exists( 'Noesia_SWA_Setup_Wizard' ) ) {
                $this->services['setup_wizard'] = new Noesia_SWA_Setup_Wizard();
            }
        }

        /**
         * Conditionally load the Pro bootstrap if present.
         *
         * @return void
         */
        private function boot_pro_loader() {
            $pro_loader = NOESIA_SWA_PLUGIN_DIR . 'pro/class-noesia-swa-pro-loader.php';
            if ( !file_exists( $pro_loader ) ) {
                return;
            }
            require_once $pro_loader;
            if ( !class_exists( 'Noesia_SWA_Pro_Loader' ) ) {
                return;
            }
            if ( method_exists( 'Noesia_SWA_Pro_Loader', 'get_instance' ) ) {
                $this->services['pro'] = Noesia_SWA_Pro_Loader::get_instance();
                return;
            }
            $this->services['pro'] = new Noesia_SWA_Pro_Loader();
        }

        /**
         * Instantiate and boot the module manager.
         *
         * @return void
         */
        private function boot_modules() {
            $this->modules = new Noesia_SWA_Modules(__FILE__);
            $this->modules->boot();
            $this->services['modules'] = $this->modules;
        }

        /**
         * Read known services via property syntax.
         *
         * @param string $key Requested property key.
         *
         * @return object|null
         */
        public function __get( $key ) {
            $key = sanitize_key( (string) $key );
            $allowed = array(
                'admin_sections',
                'settings_panels',
                'dashboard_widgets',
                'modules',
                'admin',
                'cpt',
                'ajax',
                'public',
                'cron',
                'pro',
                'dashboard_data',
                'global_pulse_data',
                'calculator_data',
                'settings_shell_data',
                'settings_section_data'
            );
            if ( in_array( $key, $allowed, true ) ) {
                return $this->get_service( $key );
            }
            return null;
        }

        /**
         * Check known service availability via property syntax.
         *
         * @param string $key Requested property key.
         *
         * @return bool
         */
        public function __isset( $key ) {
            return null !== $this->__get( $key );
        }

        /**
         * Get the module manager instance.
         *
         * @return Noesia_SWA_Modules|null
         */
        public function get_modules_manager() {
            return $this->modules;
        }

        /**
         * Get the admin sections registry.
         *
         * @return Noesia_SWA_Admin_Sections|null
         */
        public function get_admin_sections_registry() {
            $service = $this->get_service( 'admin_sections' );
            return ( $service instanceof Noesia_SWA_Admin_Sections ? $service : null );
        }

        /**
         * Get the settings panels registry.
         *
         * @return Noesia_SWA_Settings_Panels|null
         */
        public function get_settings_panels_registry() {
            $service = $this->get_service( 'settings_panels' );
            return ( $service instanceof Noesia_SWA_Settings_Panels ? $service : null );
        }

        /**
         * Get the dashboard widgets registry.
         *
         * @return Noesia_SWA_Dashboard_Widgets|null
         */
        public function get_dashboard_widgets_registry() {
            $service = $this->get_service( 'dashboard_widgets' );
            return ( $service instanceof Noesia_SWA_Dashboard_Widgets ? $service : null );
        }

        /**
         * Get the dashboard data helper service.
         *
         * @return Noesia_SWA_Dashboard_Data|null
         */
        public function get_dashboard_data_service() {
            $service = $this->get_service( 'dashboard_data' );
            return ( class_exists( 'Noesia_SWA_Dashboard_Data' ) && $service instanceof Noesia_SWA_Dashboard_Data ? $service : null );
        }

        /**
         * Get the Global Pulse data helper service.
         *
         * @return Noesia_SWA_Global_Pulse_Data|null
         */
        public function get_global_pulse_data_service() {
            $service = $this->get_service( 'global_pulse_data' );
            return ( class_exists( 'Noesia_SWA_Global_Pulse_Data' ) && $service instanceof Noesia_SWA_Global_Pulse_Data ? $service : null );
        }

        /**
         * Get the Calculator data helper service.
         *
         * @return Noesia_SWA_Calculator_Data|null
         */
        public function get_calculator_data_service() {
            $service = $this->get_service( 'calculator_data' );
            return ( class_exists( 'Noesia_SWA_Calculator_Data' ) && $service instanceof Noesia_SWA_Calculator_Data ? $service : null );
        }

        /**
         * Get the settings shell data helper service.
         *
         * @return Noesia_SWA_Settings_Shell_Data|null
         */
        public function get_settings_shell_data_service() {
            $service = $this->get_service( 'settings_shell_data' );
            return ( class_exists( 'Noesia_SWA_Settings_Shell_Data' ) && $service instanceof Noesia_SWA_Settings_Shell_Data ? $service : null );
        }

        /**
         * Get the settings section data helper service.
         *
         * @return Noesia_SWA_Settings_Section_Data|null
         */
        public function get_settings_section_data_service() {
            $service = $this->get_service( 'settings_section_data' );
            return ( class_exists( 'Noesia_SWA_Settings_Section_Data' ) && $service instanceof Noesia_SWA_Settings_Section_Data ? $service : null );
        }

        /**
         * Get one loaded service.
         *
         * @param string $service_id Service slug.
         *
         * @return object|null
         */
        public function get_service( $service_id ) {
            $service_id = sanitize_key( (string) $service_id );
            if ( isset( $this->services[$service_id] ) ) {
                return $this->services[$service_id];
            }
            return null;
        }

        /**
         * Activation callback.
         *
         * @return void
         */
        public static function activate() {
            $cpt = new Noesia_SWA_CPT();
            $cpt->register_cpt();
            flush_rewrite_rules( true );
            Noesia_SWA_Cron::schedule_event();
            if ( false === get_option( 'noesia_swa_options' ) ) {
                add_option( 'noesia_swa_options', self::get_default_options() );
            }
        }

        /**
         * Deactivation callback.
         *
         * @return void
         */
        public static function deactivate() {
            Noesia_SWA_Cron::clear_scheduled_event();
        }

        /**
         * Default plugin options for fresh installs.
         *
         * @return array<string, string>
         */
        private static function get_default_options() {
            $defaults = array(
                'shop_name'                => get_bloginfo( 'name' ),
                'logo_url'                 => NOESIA_SWA_PLUGIN_URL . 'admin/img/noesia-menu-icon.png',
                'menu_active'              => 'yes',
                'menu_url'                 => 'https://google.com',
                'btn_menu'                 => 'View Our Menu',
                'icon_menu'                => '📜',
                'btn_order'                => 'New Order',
                'icon_order'               => '🍽️',
                'btn_waiter'               => 'Call Waiter',
                'icon_waiter'              => '🙋',
                'btn_bill'                 => 'Bill Please',
                'icon_bill'                => '💳',
                'cst1_label'               => 'Urgent',
                'cst1_icon'                => '💧',
                'cst2_label'               => 'Pool pls',
                'cst2_icon'                => '✨',
                'rate_label'               => 'Rate Us on Google',
                'review_url'               => 'https://google.com',
                'color_bg'                 => '#121212',
                'color_title'              => '#ffffff',
                'color_text'               => '#ffffff',
                'sound_enable'             => 'yes',
                'weather_city'             => 'London',
                'weather_country'          => 'UK',
                'weather_units'            => 'metric',
                'news_language'            => 'en',
                'profile_business_name'    => '',
                'profile_host_name'        => '',
                'profile_business_type'    => '',
                'profile_address'          => '',
                'profile_phone'            => '',
                'profile_email'            => '',
                'profile_website'          => '',
                'profile_tax_id'           => '',
                'profile_registration_no'  => '',
                'briefing_enabled'         => 'yes',
                'briefing_show_updates'    => 'yes',
                'briefing_show_weather'    => 'yes',
                'briefing_show_schedule'   => 'yes',
                'screensaver_enabled'      => 'no',
                'screensaver_idle_minutes' => '5',
                'screensaver_show_clock'   => 'yes',
                'screensaver_show_updates' => 'yes',
            );
            for ($i = 1; $i <= 8; $i++) {
                $defaults['bookmark_' . $i . '_name'] = 'Google';
                $defaults['bookmark_' . $i . '_url'] = 'https://google.com';
            }
            return $defaults;
        }

    }

}
if ( !function_exists( 'noesia_swa' ) ) {
    /**
     * Get the main plugin singleton instance.
     *
     * @return Noesia_Smart_Waiter
     */
    function noesia_swa() {
        return Noesia_Smart_Waiter::get_instance();
    }

}
noesia_swa_fs()->add_action( 'after_uninstall', 'noesia_swa_freemius_after_uninstall' );
register_activation_hook( __FILE__, array('Noesia_Smart_Waiter', 'activate') );
register_deactivation_hook( __FILE__, array('Noesia_Smart_Waiter', 'deactivate') );
Noesia_Smart_Waiter::get_instance();