<?php
/**
 * Free Docs module.
 *
 * Self-contained entrypoint for the Docs / How to Use 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_Docs' ) ) {
    /**
     * Free Docs module.
     */
    class Noesia_Swa_Module_Docs implements Noesia_SWA_Module_Interface {

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

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

            return (bool) $enabled;
        }

        /**
         * Register module hooks.
         *
         * Docs is a strict lazy-load section. Its module-local CSS can be loaded
         * by the shell manifest when the tab is opened, so this class only binds
         * the section through the registry and renders the local module view.
         *
         * @return void
         */
        public function register_hooks() {
            add_action( 'noesia_swa_register_admin_sections', array( $this, 'register_admin_section' ) );

            /**
             * Fires when the Docs module is booted.
             *
             * @param Noesia_Swa_Module_Docs $this Module instance.
             */
            do_action( 'noesia_swa_module_docs_loaded', $this );
        }

        /**
         * Register the Docs 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'          => 'docs',
                    'label'       => $this->get_label(),
                    'nav_label'   => $this->get_label(),
                    'nav_sub'     => __( 'Live Command - Shortcodes', 'noesia-smart-waiter' ),
                    'icon'        => 'dashicons-media-text',
                    'priority'    => 70,
                    'tier'        => 'free',
                    'owner'       => 'docs',
                    'hero'        => false,
                    'is_visible'  => true,
                    'nav_visible' => true,
                    'callback'    => array( $this, 'render_admin' ),
                )
            );
        }

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

            /**
             * Fires before the Docs admin module view renders.
             *
             * @param Noesia_Swa_Module_Docs $this Module instance.
             */
            do_action( 'noesia_swa_before_render_docs_admin', $this );

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

                /**
                 * Fires after the Docs admin module view renders.
                 *
                 * @param Noesia_Swa_Module_Docs $this Module instance.
                 */
                do_action( 'noesia_swa_after_render_docs_admin', $this );
                return;
            }

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

                /**
                 * Fires after the Docs legacy fallback view renders.
                 *
                 * @param Noesia_Swa_Module_Docs $this Module instance.
                 */
                do_action( 'noesia_swa_after_render_docs_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__( 'How to Use', '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 Docs missing-view fallback renders.
             *
             * @param Noesia_Swa_Module_Docs $this Module instance.
             */
            do_action( 'noesia_swa_after_render_docs_admin', $this );
        }
    }
}
