<?php
/**
 * Free Unlock Boxes module.
 *
 * Self-contained entrypoint for the Unlock Boxes admin section.
 * View and assets are loaded from the local module folder.
 *
 * @package Noesia_Smart_Waiter
 */

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

if ( ! class_exists( 'Noesia_Swa_Module_Unlock_Boxes' ) ) {
    /**
     * Free Unlock Boxes module.
     */
    class Noesia_Swa_Module_Unlock_Boxes implements Noesia_SWA_Module_Interface {

        /**
         * Module identifier.
         */
        const MODULE_ID = 'unlock-boxes';

        /**
         * Admin style handle.
         */
        const ADMIN_STYLE_HANDLE = 'noesia-swa-unlock-boxes-admin';

        /**
         * Unique module identifier.
         *
         * @return string
         */
        public function get_id() {
            return self::MODULE_ID;
        }

        /**
         * Human-readable module label.
         *
         * @return string
         */
        public function get_label() {
            return __( 'Unlock Boxes', '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 Unlock Boxes module is enabled.
             *
             * @param bool                           $enabled Enabled state.
             * @param Noesia_Swa_Module_Unlock_Boxes $module  Module instance.
             */
            $enabled = apply_filters( 'noesia_swa_module_unlock_boxes_enabled', $enabled, $this );

            return (bool) $enabled;
        }

        /**
         * Register module hooks.
         *
         * Assets remain strict lazy-load through the shell manifest from this
         * module's local assets directory. This class also enqueues the local
         * admin stylesheet directly on the Smart Waiter screen as a defensive
         * fallback so the section keeps its intended contrast and layout.
         *
         * @return void
         */
        public function register_hooks() {
            add_action( 'noesia_swa_register_admin_sections', array( $this, 'register_admin_section' ) );
            add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ), 30 );

            /**
             * Fires when the Unlock Boxes module is booted.
             *
             * @param Noesia_Swa_Module_Unlock_Boxes $this Module instance.
             */
            do_action( 'noesia_swa_module_unlock_boxes_loaded', $this );
        }

        /**
         * Enqueue module-local admin assets on the Smart Waiter settings page.
         *
         * @param string $hook_suffix Current admin hook.
         * @return void
         */
        public function enqueue_admin_assets( $hook_suffix ) {
            if ( 'toplevel_page_noesia_swa_settings' !== (string) $hook_suffix ) {
                return;
            }

            $style_file = trailingslashit( __DIR__ ) . 'assets/css/admin.css';

            if ( ! file_exists( $style_file ) ) {
                return;
            }

            wp_enqueue_style(
                self::ADMIN_STYLE_HANDLE,
                trailingslashit( plugin_dir_url( __FILE__ ) ) . 'assets/css/admin.css',
                array(),
                (string) filemtime( $style_file )
            );
        }

        /**
         * Get the commercial upgrade URL.
         *
         * Kept in one method so the destination can be filtered without
         * scattering raw URLs through the view.
         *
         * @return string
         */
        public function get_upgrade_url() {
            $url = apply_filters(
                'noesia_swa_unlock_boxes_upgrade_url',
                'https://noesia.io/pricing-table-smart-waiter-assistant/',
                $this
            );

            return esc_url_raw( (string) $url );
        }

        /**
         * Register the Unlock Boxes 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'          => self::MODULE_ID,
                    'label'       => $this->get_label(),
                    'nav_label'   => $this->get_label(),
                    'nav_sub'     => __( 'Upgrade to Titan Edition', 'noesia-smart-waiter' ),
                    'icon'        => 'dashicons-lock',
                    'priority'    => 110,
                    'tier'        => 'free',
                    'owner'       => self::MODULE_ID,
                    'hero'        => false,
                    'is_visible'  => true,
                    'nav_visible' => true,
                    'callback'    => array( $this, 'render_admin' ),
                )
            );
        }

        /**
         * Render the module-local Unlock Boxes admin view.
         *
         * @return void
         */
        public function render_admin() {
            $module_view = trailingslashit( __DIR__ ) . 'views/admin.php';
            $legacy_view = NOESIA_SWA_PLUGIN_DIR . 'admin/partials/sections/unlock-boxes.php';
            $module      = $this;

            /**
             * Fires before the Unlock Boxes admin module view renders.
             *
             * @param Noesia_Swa_Module_Unlock_Boxes $this Module instance.
             */
            do_action( 'noesia_swa_before_render_unlock_boxes_admin', $this );

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

                /**
                 * Fires after the Unlock Boxes admin module view renders.
                 *
                 * @param Noesia_Swa_Module_Unlock_Boxes $this Module instance.
                 */
                do_action( 'noesia_swa_after_render_unlock_boxes_admin', $this );
                return;
            }

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

                /**
                 * Fires after the Unlock Boxes legacy fallback view renders.
                 *
                 * @param Noesia_Swa_Module_Unlock_Boxes $this Module instance.
                 */
                do_action( 'noesia_swa_after_render_unlock_boxes_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__( 'Unlock Boxes', '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 Unlock Boxes missing-view fallback renders.
             *
             * @param Noesia_Swa_Module_Unlock_Boxes $this Module instance.
             */
            do_action( 'noesia_swa_after_render_unlock_boxes_admin', $this );
        }
    }
}
