<?php
/**
 * Eastern Europe GeoBot — Module Loader
 *
 * This file bootstraps the entire eeurope module.
 * Include it from the main damalion-blog-ai plugin:
 *
 *   require_once __DIR__ . '/eeurope/eeurope.php';
 *
 * @since 3.0.0
 */

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

/* ── Load all class files ── */

require_once __DIR__ . '/data/class-eeurope-cities.php';
require_once __DIR__ . '/class-eeurope-registry.php';
require_once __DIR__ . '/class-eeurope-validator.php';
require_once __DIR__ . '/class-eeurope-corrections.php';
require_once __DIR__ . '/class-eeurope-dedupe.php';
require_once __DIR__ . '/class-eeurope-cron.php';
require_once __DIR__ . '/class-eeurope-scheduler.php';
require_once __DIR__ . '/class-eeurope-shortcodes.php';
require_once __DIR__ . '/class-eeurope-homepage-filter.php';
require_once __DIR__ . '/class-eeurope-inbound.php';

if ( is_admin() ) {
    require_once __DIR__ . '/class-eeurope-admin.php';
}

/* ── Initialize ── */

DBA_EEurope_Cron::init();
DBA_EEurope_Shortcodes::init();
DBA_EEurope_Scheduler::get_instance();
DBA_EEurope_Homepage_Filter::get_instance();
DBA_EEurope_Inbound::get_instance();

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

/**
 * AUTO-ACTIVATE: Ensure cron events are scheduled if module is enabled.
 * Without this, cron only gets scheduled when admin saves settings manually.
 * Runs once on `init` — checks if events exist, schedules them if not.
 */
add_action( 'init', function() {
    $settings = get_option( 'dba_eeurope_settings', array() );

    // Guard: module must be enabled
    if ( empty( $settings['enabled'] ) ) return;

    $posting_days = ! empty( $settings['posting_days'] ) ? (array) $settings['posting_days'] : array( 'thursday', 'friday' );

    // Check if ANY cron event is scheduled
    $has_event = false;
    foreach ( $posting_days as $day ) {
        if ( wp_next_scheduled( DBA_EEurope_Scheduler::CRON_HOOK, array( $day ) ) ) {
            $has_event = true;
            break;
        }
    }

    // Also check no-arg version
    if ( ! $has_event && wp_next_scheduled( DBA_EEurope_Scheduler::CRON_HOOK ) ) {
        $has_event = true;
    }

    // If no events found, schedule them
    if ( ! $has_event ) {
        error_log( 'DBA EEurope: No cron events found — auto-activating for days: ' . implode( ', ', $posting_days ) );
        DBA_EEurope_Scheduler::activate();
    }
}, 20 );

/**
 * DIAGNOSTIC: Log cron status on admin_init (once per hour max).
 * Check error_log for "DBA EEurope CRON STATUS" to verify scheduling.
 */
add_action( 'admin_init', function() {
    if ( get_transient( 'dba_eeurope_cron_diag_done' ) ) return;
    set_transient( 'dba_eeurope_cron_diag_done', 1, HOUR_IN_SECONDS );

    $settings = get_option( 'dba_eeurope_settings', array() );
    $enabled  = ! empty( $settings['enabled'] ) ? 'YES' : 'NO';
    $days     = ! empty( $settings['posting_days'] ) ? implode( ', ', (array) $settings['posting_days'] ) : 'DEFAULT (thu, fri)';

    $events = array();
    foreach ( array( 'monday','tuesday','wednesday','thursday','friday','saturday','sunday' ) as $d ) {
        $ts = wp_next_scheduled( DBA_EEurope_Scheduler::CRON_HOOK, array( $d ) );
        if ( $ts ) $events[] = ucfirst( $d ) . '=' . gmdate( 'Y-m-d H:i:s', $ts ) . ' UTC';
    }
    $ts_bare = wp_next_scheduled( DBA_EEurope_Scheduler::CRON_HOOK );
    if ( $ts_bare ) $events[] = 'bare=' . gmdate( 'Y-m-d H:i:s', $ts_bare ) . ' UTC';

    $countries_enabled = DBA_EEurope_Registry::get_enabled_countries();
    $next_country = DBA_EEurope_Registry::peek_next_country();

    error_log( sprintf(
        'DBA EEurope CRON STATUS: enabled=%s | days=%s | events=[%s] | enabled_countries=[%s] (%d) | next=%s',
        $enabled, $days,
        $events ? implode( ' | ', $events ) : 'NONE — CRON NOT SCHEDULED!',
        $countries_enabled ? implode( ', ', $countries_enabled ) : 'NONE — NO COUNTRIES ENABLED!',
        count( $countries_enabled ),
        $next_country ?: 'none'
    ) );

    // WARN if module enabled but no countries enabled
    if ( ! empty( $settings['enabled'] ) && empty( $countries_enabled ) ) {
        error_log( 'DBA EEurope ⚠️ WARNING: Module is enabled but ZERO countries are enabled! Go to EEurope admin → enable at least one country.' );
    }
}, 10 );
