<?php
/**
 * LuxStrat International — Module Loader
 *
 * @package DamalionBlogAI
 * @subpackage LuxStratIntl
 */

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

class DBA_LuxStrat_Intl_Loader {

    private static $languages = null;

    public static function get_languages() {
        if ( self::$languages !== null ) {
            return self::$languages;
        }

        self::$languages = array();

        $dir = dirname( __FILE__ ) . '/languages/';

        if ( ! is_dir( $dir ) ) {
            // Fallback: try via DBA_PLUGIN_DIR
            if ( defined( 'DBA_PLUGIN_DIR' ) ) {
                $dir = DBA_PLUGIN_DIR . 'luxstrat-intl/languages/';
            }
            if ( ! is_dir( $dir ) ) {
                error_log( 'LuxStrat Intl: ❌ languages/ directory NOT FOUND' );
                return self::$languages;
            }
        }

        $scan = @scandir( $dir );
        if ( ! is_array( $scan ) ) {
            error_log( 'LuxStrat Intl: ❌ scandir() failed on ' . $dir );
            return self::$languages;
        }

        foreach ( $scan as $item ) {
            if ( $item === '.' || $item === '..' ) continue;
            if ( substr( $item, -4 ) !== '.php' ) continue;

            $file = $dir . $item;
            if ( ! is_file( $file ) ) continue;

            $config = @include $file;

            if ( ! is_array( $config ) || empty( $config['code'] ) ) {
                error_log( 'LuxStrat Intl: ❌ ' . $item . ' invalid (not array or missing code)' );
                continue;
            }

            $lang = sanitize_key( $config['code'] );
            self::$languages[ $lang ] = $config;
            error_log( 'LuxStrat Intl: ✅ Loaded "' . $lang . '"' );
        }

        error_log( 'LuxStrat Intl: ' . count( self::$languages ) . ' language(s) loaded' );
        return self::$languages;
    }

    public static function get_language( $lang ) {
        $all = self::get_languages();
        return isset( $all[ $lang ] ) ? $all[ $lang ] : null;
    }
}

// ── Load engine classes ──
$intl_dir = dirname( __FILE__ ) . '/';
require_once $intl_dir . 'class-luxstrat-intl-dedupe.php';
require_once $intl_dir . 'class-luxstrat-intl-validator.php';
require_once $intl_dir . 'class-luxstrat-intl-generator.php';
require_once $intl_dir . 'class-luxstrat-intl-scheduler.php';
require_once $intl_dir . 'class-luxstrat-intl-inbound.php';
require_once $intl_dir . 'class-luxstrat-intl-corrections.php';
// Kaizen + Anti-Orphan (v2.17.0)
if ( file_exists( $intl_dir . 'class-luxstrat-intl-kaizen.php' ) ) {
    require_once $intl_dir . 'class-luxstrat-intl-kaizen.php';
}
if ( file_exists( $intl_dir . 'class-luxstrat-intl-anti-orphan.php' ) ) {
    require_once $intl_dir . 'class-luxstrat-intl-anti-orphan.php';
}
require_once $intl_dir . 'class-luxstrat-intl-admin.php';

// ── Initialise immediately ──
// This runs during plugins_loaded (from dba_init_plugin).
// admin_menu has NOT fired yet, so the admin constructor's hook WILL fire.
DBA_LuxStrat_Intl_Scheduler::get_instance();

if ( is_admin() ) {
    DBA_LuxStrat_Intl_Admin::get_instance();
}

error_log( 'LuxStrat Intl: ✅ Loader complete' );
