<?php
use NewsletterBridge\Config\NewsletterConfig;
use NewsletterBridge\Config\MailupConfig;
use NewsletterBridge\Config\DatabaseConfig;
use NewsletterBridge\Config\SiteConfig;

/*
Plugin Name: Newsletter Blazemedia
Plugin URI: http://www.blazemedia.it
Description: This Plugin it's a connector for newsletter services
Version: 2.0.0
Author: Emanuele Gennuso
Author URI: http://www.blazemedia.it/
License: LGPL
*/
if ( ! defined( 'PLUGIN_PATH_BRIDGE' ) )  define( 'PLUGIN_PATH_BRIDGE', plugin_dir_path( __FILE__ ) );

// Supporto per WordPress Network (Multisite)
if ( ! function_exists( 'is_multisite' ) ) {
    function is_multisite() {
        return false;
    }
}

require_once PLUGIN_PATH_BRIDGE . '/vendor/autoload.php';

use NewsletterBridge\Services;
use NewsletterBridge\Api;
use NewsletterBridge\Admin\Admin;
use NewsletterBridge\Services\Newsletter\NewsletterFeedEndpoint;
use NewsletterBridge\Api\CustomService;
use NewsletterBridge\Commands\NewsletterCommand;

# init site configuration and hooks
SiteConfig::init();

# init new admin system
new Admin();

# init newsletter feed endpoint
new NewsletterFeedEndpoint();

# load services for REST API
$files = glob(PLUGIN_PATH_BRIDGE.'/src/Services/External/*.{php}', GLOB_BRACE);
foreach($files as $file) {
    $className = basename($file, ".php");      
    $className = "NewsletterBridge\\Services\\External\\".$className;
    
    // Skip Sender class as it requires parameters
    if ($className === "NewsletterBridge\\Services\\External\\Sender") {
        continue;
    }
    
    $class = new $className;
    $services[$class->getCode()] = $class;
}

# load DB service
$dbService = new \NewsletterBridge\Lib\DB();
$services[$dbService->getCode()] = $dbService;

# load custom rest api
new CustomService($services);

# register WP-CLI command
if (defined('WP_CLI') && WP_CLI) {
    WP_CLI::add_command('newsletter', 'NewsletterBridge\\Commands\\NewsletterCommand');
}

/**
 * Funzioni helper per ottenere le configurazioni
 */
function get_newsletter_config($site_id = null) {
    // Se viene passato un site_id specifico, NON usare il cache - sempre ricarica
    // Questo garantisce che vengano sempre usate le configurazioni del sito specificato
    if ($site_id !== null) {
        return NewsletterConfig::getConfig($site_id);
    }
    
    // Altrimenti, usa il cache come prima
    static $config = null;
    static $blog_id = null;
    
    $current_blog_id = function_exists('get_current_blog_id') ? get_current_blog_id() : 0;
    
    // Se il blog ID è cambiato, resetta il cache
    if ($config !== null && $blog_id !== null && $blog_id != $current_blog_id) {
        $config = null;
        $blog_id = null;
    }
    
    // Se non c'è cache o il blog ID è cambiato, carica la nuova configurazione
    if ($config === null) {
        // Chiama getConfig() senza parametri per usare il contesto corrente (dopo switch_to_blog)
        $config = NewsletterConfig::getConfig();
        $blog_id = $current_blog_id;
    }
    
    return $config;
}

function get_mailup_config() {
    static $config = null;
    static $blog_id = null;
    
    $current_blog_id = function_exists('get_current_blog_id') ? get_current_blog_id() : 0;
    
    // Se il blog ID è cambiato, resetta il cache
    if ($config !== null && $blog_id !== null && $blog_id != $current_blog_id) {
        $config = null;
    }
    
    if ($config === null) {
        $config = MailupConfig::getConfig();
        $blog_id = $current_blog_id;
    }
    return $config;
}

function get_database_config() {
    static $config = null;
    static $blog_id = null;
    
    $current_blog_id = function_exists('get_current_blog_id') ? get_current_blog_id() : 0;
    
    // Se il blog ID è cambiato, resetta il cache
    if ($config !== null && $blog_id !== null && $blog_id != $current_blog_id) {
        $config = null;
    }
    
    if ($config === null) {
        $config = DatabaseConfig::getConfig();
        $blog_id = $current_blog_id;
    }
    return $config;
}



