<?php
/**
 * Free Preview App module.
 *
 * Self-contained entrypoint for the Preview App 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_Preview_App' ) ) {
    /**
     * Free Preview App module.
     */
    class Noesia_Swa_Module_Preview_App implements Noesia_SWA_Module_Interface {

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

        /**
         * Human-readable module label.
         *
         * @return string
         */
        public function get_label() {
            return __( 'Preview App', '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 Preview App module is enabled.
             *
             * @param bool                          $enabled Enabled state.
             * @param Noesia_Swa_Module_Preview_App $module  Module instance.
             */
            $enabled = apply_filters( 'noesia_swa_module_preview_app_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 only binds the section
         * through the registry and renders the module-local admin view.
         *
         * @return void
         */
        public function register_hooks() {
            add_action( 'noesia_swa_register_admin_sections', array( $this, 'register_admin_section' ) );

            /**
             * Fires when the Preview App module is booted.
             *
             * @param Noesia_Swa_Module_Preview_App $this Module instance.
             */
            do_action( 'noesia_swa_module_preview_app_loaded', $this );
        }

        /**
         * Register the Preview App 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'          => 'preview-app',
                    'label'       => $this->get_label(),
                    'nav_label'   => $this->get_label(),
                    'nav_sub'     => 'Live',
                    'icon'        => 'dashicons-smartphone',
                    'priority'    => 70,
                    'tier'        => 'free',
                    'owner'       => 'preview-app',
                    'hero'        => false,
                    'is_visible'  => true,
                    'nav_visible' => true,
                    'callback'    => array( $this, 'render_admin' ),
                )
            );
        }

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

            /**
             * Fires before the Preview App admin module view renders.
             *
             * @param Noesia_Swa_Module_Preview_App $this Module instance.
             */
            do_action( 'noesia_swa_before_render_preview_app_admin', $this );

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

                /**
                 * Fires after the Preview App admin module view renders.
                 *
                 * @param Noesia_Swa_Module_Preview_App $this Module instance.
                 */
                do_action( 'noesia_swa_after_render_preview_app_admin', $this );
                return;
            }

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

                /**
                 * Fires after the Preview App legacy fallback view renders.
                 *
                 * @param Noesia_Swa_Module_Preview_App $this Module instance.
                 */
                do_action( 'noesia_swa_after_render_preview_app_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__( 'Preview App', '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 Preview App missing-view fallback renders.
             *
             * @param Noesia_Swa_Module_Preview_App $this Module instance.
             */
            do_action( 'noesia_swa_after_render_preview_app_admin', $this );
        }
    }
}
