<?php
/**
 * ──────────────────────────────────────────────────────────────────────
 * LATAM GeoBot Integration Patch
 * ──────────────────────────────────────────────────────────────────────
 *
 * This file documents the EXACT lines to add to damalion-blog-ai.php
 * to load the LATAM module. Apply these 3 patches in order.
 *
 * After patching, bump the plugin version to 2.11.0.
 *
 * ──────────────────────────────────────────────────────────────────────
 */

/**
 * ┌─────────────────────────────────────────────────────────────────────┐
 * │ PATCH 1: Add to dba_init_plugin() — after the LuxStrat block       │
 * │ Location: damalion-blog-ai.php, around line 311                     │
 * └─────────────────────────────────────────────────────────────────────┘
 *
 * Find this line:
 *     define( 'DBA_LUXSTRAT_LOADED', true );
 *   }
 *
 * ADD AFTER IT:
 */

    // --- LATAM GeoBot module — Latin America investment blogs (isolated) ---
    if ( file_exists( DBA_PLUGIN_DIR . 'latam/class-latam-registry.php' ) ) {
        require_once DBA_PLUGIN_DIR . 'latam/class-latam-cron.php';
        require_once DBA_PLUGIN_DIR . 'latam/class-latam-registry.php';
        require_once DBA_PLUGIN_DIR . 'latam/data/class-latam-cities.php';
        require_once DBA_PLUGIN_DIR . 'latam/class-latam-scheduler.php';
        require_once DBA_PLUGIN_DIR . 'latam/class-latam-shortcodes.php';
        require_once DBA_PLUGIN_DIR . 'latam/class-latam-calendar.php';
        define( 'DBA_LATAM_LOADED', true );
    }

/**
 * ┌─────────────────────────────────────────────────────────────────────┐
 * │ PATCH 2: Add to is_admin() block — after LuxStrat admin            │
 * │ Location: damalion-blog-ai.php, around line 325                     │
 * └─────────────────────────────────────────────────────────────────────┘
 *
 * Find this line:
 *         if ( defined( 'DBA_LUXSTRAT_LOADED' ) && DBA_LUXSTRAT_LOADED ) {
 *             require_once DBA_PLUGIN_DIR . 'luxstrat/class-luxstrat-admin.php';
 *             DBA_LuxStrat_Admin::get_instance();
 *         }
 *
 * ADD AFTER IT:
 */

        if ( defined( 'DBA_LATAM_LOADED' ) && DBA_LATAM_LOADED ) {
            require_once DBA_PLUGIN_DIR . 'latam/class-latam-admin.php';
            DBA_LATAM_Admin::get_instance();
            DBA_LATAM_Calendar::get_instance();
        }

/**
 * ┌─────────────────────────────────────────────────────────────────────┐
 * │ PATCH 3: Add instances — after LuxStrat instances                   │
 * │ Location: damalion-blog-ai.php, around line 352                     │
 * └─────────────────────────────────────────────────────────────────────┘
 *
 * Find this line:
 *     if ( defined( 'DBA_LUXSTRAT_LOADED' ) && DBA_LUXSTRAT_LOADED ) {
 *         DBA_LuxStrat_Generator::get_instance();
 *         DBA_LuxStrat_Scheduler::get_instance();
 *     }
 *
 * ADD AFTER IT:
 */

    // --- LATAM instances ---
    if ( defined( 'DBA_LATAM_LOADED' ) && DBA_LATAM_LOADED ) {
        DBA_LATAM_Registry::register_in_geobot();
        DBA_LATAM_Scheduler::get_instance();
        DBA_LATAM_Shortcodes::get_instance();
    }

/**
 * ┌─────────────────────────────────────────────────────────────────────┐
 * │ PATCH 4: Add to dba_activate() — after LuxStrat activation         │
 * │ Location: damalion-blog-ai.php, around line 446                     │
 * └─────────────────────────────────────────────────────────────────────┘
 *
 * Find this line:
 *     if ( file_exists( DBA_PLUGIN_DIR . 'luxstrat/class-luxstrat-registry.php' ) ) {
 *         ...
 *         DBA_LuxStrat_Scheduler::activate();
 *     }
 *
 * ADD AFTER IT:
 */

    // --- LATAM module activation ---
    if ( file_exists( DBA_PLUGIN_DIR . 'latam/class-latam-registry.php' ) ) {
        require_once DBA_PLUGIN_DIR . 'latam/class-latam-cron.php';
        require_once DBA_PLUGIN_DIR . 'latam/class-latam-registry.php';
        require_once DBA_PLUGIN_DIR . 'latam/data/class-latam-cities.php';
        require_once DBA_PLUGIN_DIR . 'latam/class-latam-scheduler.php';
        DBA_LATAM_Scheduler::activate();
        DBA_LATAM_Cities::load_all_cities();

        // Default LATAM settings
        if ( ! get_option( 'dba_latam_settings' ) ) {
            add_option( 'dba_latam_settings', array(
                'enabled'            => false,
                'post_status'        => 'publish',
                'author_id'          => 1,
                'category_id'        => 0,
                'word_count_min'     => 500,
                'word_count_max'     => 800,
                'enabled_countries'  => array(),
                'cta_text'           => 'Damalion supports international investors, entrepreneurs, and family offices establishing and structuring their business in {Country}.',
                'cta_link_text'      => 'Contact your Damalion experts now.',
                'cta_link_url'       => 'https://www.damalion.com/lets-connect/',
            ) );
        }

        // Default settings per LATAM country
        foreach ( DBA_LATAM_Registry::get_countries() as $key => $geo_config ) {
            if ( ! get_option( $geo_config['settings_option'] ) ) {
                add_option( $geo_config['settings_option'], array(
                    'enabled'          => false,
                    'posting_days'     => $geo_config['default_posting_days'],
                    'posts_per_run'    => $geo_config['default_posts_per_run'],
                    'post_status'      => 'publish',
                    'author_id'        => 1,
                    'active_communes'  => array(),
                    'cta_text'         => $geo_config['cta_text'],
                    'cta_link_text'    => $geo_config['cta_link_text'],
                    'cta_link_url'     => $geo_config['cta_link_url'],
                    'word_count_min'   => $geo_config['word_count_min'],
                    'word_count_max'   => $geo_config['word_count_max'],
                ) );
            }
        }

        error_log( 'DBA LATAM: Module activated — 15 countries, 75 cities loaded.' );
    }

/**
 * ┌─────────────────────────────────────────────────────────────────────┐
 * │ PATCH 5: Add to dba_deactivate() — after LuxStrat deactivation     │
 * │ Location: damalion-blog-ai.php, around line 559                     │
 * └─────────────────────────────────────────────────────────────────────┘
 *
 * Find this line:
 *     if ( file_exists( DBA_PLUGIN_DIR . 'luxstrat/class-luxstrat-scheduler.php' ) ) {
 *         ...
 *         DBA_LuxStrat_Scheduler::deactivate();
 *     }
 *
 * ADD AFTER IT:
 */

    // --- LATAM cron cleanup ---
    if ( file_exists( DBA_PLUGIN_DIR . 'latam/class-latam-scheduler.php' ) ) {
        require_once DBA_PLUGIN_DIR . 'latam/class-latam-scheduler.php';
        DBA_LATAM_Scheduler::deactivate();
    }
