<?php
/**
 * Comfisc — Module Loader (Comptabilité & Fiscalité)
 *
 * @package DamalionBlogAI
 * @subpackage Comfisc
 */

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

class DBA_Comfisc_Loader {

    private static $languages = null;

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

        self::$languages = array();

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

        if ( ! is_dir( $dir ) ) {
            if ( defined( 'DBA_PLUGIN_DIR' ) ) {
                $dir = DBA_PLUGIN_DIR . 'comfisc/data/';
            }
            if ( ! is_dir( $dir ) ) {
                error_log( 'Comfisc: ❌ data/ directory NOT FOUND' );
                return self::$languages;
            }
        }

        $scan = @scandir( $dir );
        if ( ! is_array( $scan ) ) {
            error_log( 'Comfisc: ❌ 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( 'Comfisc: ❌ ' . $item . ' invalid (not array or missing code)' );
                continue;
            }

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

        error_log( 'Comfisc: ' . 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 ──
$comfisc_dir = dirname( __FILE__ ) . '/';
require_once $comfisc_dir . 'class-comfisc-dedupe.php';
require_once $comfisc_dir . 'class-comfisc-validator.php';
require_once $comfisc_dir . 'class-comfisc-generator.php';
require_once $comfisc_dir . 'class-comfisc-scheduler.php';
require_once $comfisc_dir . 'class-comfisc-corrections.php';

// Kaizen (v3.0.0)
if ( file_exists( $comfisc_dir . 'class-comfisc-kaizen.php' ) ) {
    require_once $comfisc_dir . 'class-comfisc-kaizen.php';
}

// Homepage filter
if ( file_exists( $comfisc_dir . 'class-comfisc-homepage-filter.php' ) ) {
    require_once $comfisc_dir . 'class-comfisc-homepage-filter.php';
}

// Anti-Orphan protection (v2.19.0)
if ( file_exists( $comfisc_dir . 'class-comfisc-anti-orphan.php' ) ) {
    require_once $comfisc_dir . 'class-comfisc-anti-orphan.php';
}

// ── Initialise immediately ──
DBA_Comfisc_Scheduler::get_instance();

// Homepage filter (frontend only)
if ( ! is_admin() && class_exists( 'DBA_Comfisc_Homepage_Filter' ) ) {
    DBA_Comfisc_Homepage_Filter::get_instance();
}

// Anti-Orphan (both frontend and cron)
if ( class_exists( 'DBA_Comfisc_Anti_Orphan' ) ) {
    DBA_Comfisc_Anti_Orphan::get_instance();
}

error_log( 'Comfisc: ✅ Loader complete' );
