<?php
/**
 * Free QR Setup module.
 *
 * Strict-isolation entrypoint for the QR / Setup admin section.
 * Owns:
 * - section registration
 * - module-local admin view
 * - QR-specific shared strings/i18n used by local JS / shell bridge
 *
 * This module remains tab-local. Assets are lazy-loaded by the shell manifest.
 *
 * @package Noesia_Smart_Waiter
 */

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

if ( ! class_exists( 'Noesia_Swa_Module_Qr_Setup' ) ) {
    /**
     * Free QR Setup module.
     */
    class Noesia_Swa_Module_Qr_Setup implements Noesia_SWA_Module_Interface {

        /**
         * Unique module identifier.
         *
         * @return string
         */
        public function get_id() {
            return 'qr-setup';
        }

        /**
         * Human-readable module label.
         *
         * @return string
         */
        public function get_label() {
            return __( 'QR / Setup', 'noesia-smart-waiter' );
        }

        /**
         * Module tier.
         *
         * @return string
         */
        public function get_tier() {
            return 'free';
        }

        /**
         * Whether the module is enabled.
         *
         * @return bool
         */
        public function is_enabled() {
            $enabled = true;

            /**
             * Filter whether the free QR Setup module is enabled.
             *
             * @param bool                        $enabled Enabled state.
             * @param Noesia_Swa_Module_Qr_Setup $module  Module instance.
             */
            $enabled = apply_filters( 'noesia_swa_module_qr_setup_enabled', $enabled, $this );

            return (bool) $enabled;
        }

        /**
         * Register module hooks.
         *
         * @return void
         */
        public function register_hooks() {
            add_action( 'noesia_swa_register_admin_sections', array( $this, 'register_admin_section' ) );
            add_filter( 'noesia_swa_admin_strings', array( $this, 'filter_admin_strings' ), 10, 2 );
            add_filter( 'noesia_swa_admin_i18n', array( $this, 'filter_admin_i18n' ), 10, 2 );

            /**
             * Fires when the QR Setup module is booted.
             *
             * @param Noesia_Swa_Module_Qr_Setup $this Module instance.
             */
            do_action( 'noesia_swa_module_qr_setup_loaded', $this );
        }

        /**
         * Register the QR / Setup admin section.
         *
         * @param mixed $registry Admin sections registry.
         * @return void
         */
        public function register_admin_section( $registry ) {
            if ( ! $registry instanceof Noesia_SWA_Admin_Sections ) {
                return;
            }

            $registry->register_section(
                array(
                    'id'          => 'qr-setup',
                    'label'       => $this->get_label(),
                    'nav_label'   => $this->get_label(),
                    'nav_sub'     => __( 'Codes & Print Flow', 'noesia-smart-waiter' ),
                    'icon'        => 'dashicons-media-code',
                    'priority'    => 40,
                    'tier'        => 'free',
                    'owner'       => 'qr-setup',
                    'is_visible'  => true,
                    'nav_visible' => true,
                    'callback'    => array( $this, 'render_admin' ),
                )
            );
        }

        /**
         * Provide QR-owned shared strings.
         *
         * @param array<string,string> $strings Existing string payload.
         * @param mixed                $admin   Admin instance.
         * @return array<string,string>
         */
        public function filter_admin_strings( $strings, $admin ) {
            if ( ! is_array( $strings ) ) {
                $strings = array();
            }

            $strings['print_title'] = __( 'Print QR Codes', 'noesia-smart-waiter' );
            $strings['print_btn']   = __( 'PRINT NOW', 'noesia-smart-waiter' );
            $strings['scan_order']  = __( 'Scan to Order', 'noesia-smart-waiter' );
            $strings['err_popup']   = __( 'Please allow popups to print QR codes.', 'noesia-smart-waiter' );

            return $strings;
        }

        /**
         * Provide QR-owned i18n used by local JS.
         *
         * @param array<string,string> $i18n Existing i18n payload.
         * @param mixed                $admin Admin instance.
         * @return array<string,string>
         */
        public function filter_admin_i18n( $i18n, $admin ) {
            if ( ! is_array( $i18n ) ) {
                $i18n = array();
            }

            $i18n['qr_missing_base_url'] = __( 'Please enter a Base URL.', 'noesia-smart-waiter' );
            $i18n['qr_invalid_range']    = __( 'Invalid start/end range.', 'noesia-smart-waiter' );
            $i18n['qr_popup_blocked']    = __( 'Please allow popups to print QR codes.', 'noesia-smart-waiter' );
            $i18n['qr_print_title']      = __( 'Print QR Codes', 'noesia-smart-waiter' );
            $i18n['qr_print_now']        = __( 'PRINT NOW', 'noesia-smart-waiter' );

            return $i18n;
        }

        /**
         * Render the module-local QR / Setup admin view.
         *
         * @return void
         */
        public function render_admin() {
            $module_view = trailingslashit( __DIR__ ) . 'views/admin.php';
            $legacy_view = NOESIA_SWA_PLUGIN_DIR . 'admin/partials/sections/qr-setup.php';

            /**
             * Fires before the QR / Setup admin module view renders.
             *
             * @param Noesia_Swa_Module_Qr_Setup $this Module instance.
             */
            do_action( 'noesia_swa_before_render_qr_setup_admin', $this );

            if ( file_exists( $module_view ) ) {
                include $module_view;

                /**
                 * Fires after the QR / Setup admin module view renders.
                 *
                 * @param Noesia_Swa_Module_Qr_Setup $this Module instance.
                 */
                do_action( 'noesia_swa_after_render_qr_setup_admin', $this );
                return;
            }

            if ( file_exists( $legacy_view ) ) {
                include $legacy_view;

                /**
                 * Fires after the QR / Setup legacy fallback view renders.
                 *
                 * @param Noesia_Swa_Module_Qr_Setup $this Module instance.
                 */
                do_action( 'noesia_swa_after_render_qr_setup_admin', $this );
                return;
            }

            echo '<div class="noesia-swa-card noesia-swa-card--span2">';
            echo '<div class="noesia-swa-card-header"><div>';
            echo '<h3>' . esc_html__( 'QR / Setup', 'noesia-smart-waiter' ) . '</h3>';
            echo '<p class="noesia-swa-muted">' . esc_html__( 'Module view missing.', 'noesia-smart-waiter' ) . '</p>';
            echo '</div></div>';
            echo '</div>';

            /**
             * Fires after the QR / Setup missing-view fallback renders.
             *
             * @param Noesia_Swa_Module_Qr_Setup $this Module instance.
             */
            do_action( 'noesia_swa_after_render_qr_setup_admin', $this );
        }
    }
}
