<?php
/**
 * Global Data Hub module.
 *
 * @package Noesia_Smart_Waiter
 */

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

if ( ! class_exists( 'Noesia_Swa_Module_Global_Data_Hub' ) ) {
    /**
     * Global Data Hub module owner.
     */
    class Noesia_Swa_Module_Global_Data_Hub implements Noesia_SWA_Module_Interface {

        /**
         * Service instance.
         *
         * @var Noesia_SWA_Global_Data_Hub_Service|null
         */
        private $service = null;

        /**
         * Ajax instance.
         *
         * @var Noesia_SWA_Global_Data_Hub_Ajax|null
         */
        private $ajax = null;

        /**
         * Get module id.
         *
         * @return string
         */
        public function get_id() {
            return 'global-data-hub';
        }

        /**
         * Get module label.
         *
         * @return string
         */
        public function get_label() {
            return __( 'Global Data Hub', 'noesia-smart-waiter' );
        }

        /**
         * Get module tier.
         *
         * @return string
         */
        public function get_tier() {
            $normalized = wp_normalize_path( __DIR__ );
            if ( false !== strpos( $normalized, '/pro/modules/' ) ) {
                return 'pro';
            }
            return 'free';
        }

        /**
         * Whether the module is enabled.
         *
         * @return bool
         */
        public function is_enabled() {
            return (bool) apply_filters( 'noesia_swa_module_global_data_hub_enabled', true, $this );
        }

        /**
         * Register hooks.
         *
         * @return void
         */
        public function register_hooks() {
            $this->boot_dependencies();

            if ( ! $this->service instanceof Noesia_SWA_Global_Data_Hub_Service ) {
                return;
            }

            add_action( 'noesia_swa_register_admin_sections', array( $this, 'register_admin_section' ) );
            add_filter( 'noesia_swa_localized_options', array( $this, 'filter_localized_options' ), 10, 2 );
            add_filter( 'noesia_swa_admin_strings', array( $this, 'filter_admin_strings' ), 10, 2 );
            add_action( 'noesia_swa_after_factory_reset', array( $this, 'handle_factory_reset_cleanup' ) );

            if ( $this->ajax instanceof Noesia_SWA_Global_Data_Hub_Ajax ) {
                $this->ajax->register_hooks();
            }
        }

        /**
         * Boot dependencies.
         *
         * @return void
         */
        private function boot_dependencies() {
            $files = array(
                'Noesia_SWA_Global_Data_Hub_Service' => __DIR__ . '/includes/class-noesia-swa-global-data-hub-service.php',
                'Noesia_SWA_Global_Data_Hub_Ajax'    => __DIR__ . '/includes/class-noesia-swa-global-data-hub-ajax.php',
            );

            foreach ( $files as $class_name => $file ) {
                if ( ! class_exists( $class_name ) && file_exists( $file ) ) {
                    require_once $file;
                }
            }

            if ( ! $this->service instanceof Noesia_SWA_Global_Data_Hub_Service && class_exists( 'Noesia_SWA_Global_Data_Hub_Service' ) ) {
                $this->service = new Noesia_SWA_Global_Data_Hub_Service();
            }

            if ( ! $this->ajax instanceof Noesia_SWA_Global_Data_Hub_Ajax && $this->service instanceof Noesia_SWA_Global_Data_Hub_Service && class_exists( 'Noesia_SWA_Global_Data_Hub_Ajax' ) ) {
                $this->ajax = new Noesia_SWA_Global_Data_Hub_Ajax( $this->service );
            }
        }

        /**
         * Register admin section.
         *
         * @param mixed $registry Registry instance.
         * @return void
         */
        public function register_admin_section( $registry ) {
            if ( ! $registry instanceof Noesia_SWA_Admin_Sections ) {
                return;
            }

            $registry->register_section(
                array(
                    'id'          => 'global-data-hub',
                    'label'       => $this->get_label(),
                    'nav_label'   => $this->get_label(),
                    'nav_sub'     => __( 'Global intelligence: official statistics & health data', 'noesia-smart-waiter' ),
                    'icon'        => 'dashicons-chart-area',
                    'priority'    => 83,
                    'tier'        => $this->get_tier(),
                    'owner'       => 'global-data-hub',
                    'hero'        => false,
                    'is_visible'  => true,
                    'nav_visible' => true,
                    'callback'    => array( $this, 'render_admin' ),
                )
            );
        }

        /**
         * Inject localized options.
         *
         * @param array<string,mixed> $options Existing options.
         * @param mixed               $admin   Admin shell.
         * @return array<string,mixed>
         */
        public function filter_localized_options( $options, $admin ) {
            if ( ! is_array( $options ) ) {
                $options = array();
            }

            $options['global_data_hub'] = $this->service->get_public_settings();
            return $options;
        }

        /**
         * Inject admin strings.
         *
         * @param array<string,string> $strings Existing strings.
         * @param mixed                $admin   Admin shell.
         * @return array<string,string>
         */
        public function filter_admin_strings( $strings, $admin ) {
            if ( ! is_array( $strings ) ) {
                $strings = array();
            }

            $strings['gdh_loading']            = __( 'Loading official Eurostat tourism data…', 'noesia-smart-waiter' );
            $strings['gdh_loading_development'] = __( 'Loading official World Bank WDI data…', 'noesia-smart-waiter' );
            $strings['gdh_loading_imf']        = __( 'Loading official IMF macro data…', 'noesia-smart-waiter' );
            $strings['gdh_loading_digital']    = __( 'Loading official digital and connectivity data…', 'noesia-smart-waiter' );
            $strings['gdh_loading_population'] = __( 'Loading official population and society data…', 'noesia-smart-waiter' );
            $strings['gdh_loading_oecd']       = __( 'Loading official OECD labour and activity data…', 'noesia-smart-waiter' );
            $strings['gdh_loading_health']     = __( 'Loading official WHO health data…', 'noesia-smart-waiter' );
            $strings['gdh_loading_medical']    = __( 'Loading official medical evidence…', 'noesia-smart-waiter' );
            $strings['gdh_loading_compare']    = __( 'Loading strict country comparison data…', 'noesia-smart-waiter' );
            $strings['gdh_saved']              = __( 'Global Data Hub settings saved.', 'noesia-smart-waiter' );
            return $strings;
        }

        /**
         * Cleanup option on factory reset.
         *
         * @return void
         */
        public function handle_factory_reset_cleanup() {
            delete_option( Noesia_SWA_Global_Data_Hub_Service::OPTION_KEY );
        }

        /**
         * Render admin.
         *
         * @return void
         */
        public function render_admin() {
            if ( ! $this->service instanceof Noesia_SWA_Global_Data_Hub_Service ) {
                return;
            }

            $view_file = __DIR__ . '/views/admin.php';
            if ( file_exists( $view_file ) ) {
                $module  = $this;
                $service = $this->service;
                require $view_file;
            }
        }
    }
}
