<?php
/**
 * Plugin Name: apCampaign
 * Plugin URI: https://artistprowp.com/apcampaign/
 * Description: Build, personalize, test, schedule, track, retarget, and improve email campaigns from WordPress. Includes native unlimited audiences, visual and HTML editors, templates, preflight checks, spam scoring, campaign intelligence, suppression, and optional advanced mail routing.
 * Version: 1.0.2
 * Requires at least: 6.0
 * Requires PHP: 7.4
 * Author: ArtistProWP
 * Author URI: https://artistprowp.com/
 * Text Domain: apcampaign
 * Network: true
 * Update URI: https://artistprowp.com/apcampaign/
 */

declare(strict_types=1);

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

define('APCAMPAIGN_VERSION', '1.0.2');
define('APCAMPAIGN_PLUGIN_FILE', __FILE__);
define('APCAMPAIGN_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('APCAMPAIGN_PLUGIN_URL', plugin_dir_url(__FILE__));

require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-contract.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-schema.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-renderer.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-spam-score.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-intelligence.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-preflight.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-lifecycle.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-delivery.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-admin.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-app.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-plugin.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-artistprowp-license-client.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-license.php';
require_once APCAMPAIGN_PLUGIN_DIR . 'includes/class-apcampaign-cli.php';

register_activation_hook(__FILE__, static function (bool $network_wide = false): void {
    if (is_multisite() && $network_wide) {
        foreach (get_sites(['fields' => 'ids']) as $site_id) {
            switch_to_blog((int) $site_id);
            APCampaign_Schema::install();
            restore_current_blog();
        }
    } else {
        APCampaign_Schema::install();
    }
    APCampaign_App::register_routes();
    flush_rewrite_rules(false);
});

register_deactivation_hook(__FILE__, static function (): void {
    flush_rewrite_rules(false);
});

add_action('plugins_loaded', static function (): void {
    if ((string) get_option('apcampaign_schema_version', '') !== APCAMPAIGN_VERSION) {
        APCampaign_Schema::install();
    }
    APCampaign_Plugin::instance()->boot();
    APCampaign_License::init();
});

if (!function_exists('apcampaign_component')) {
    /**
     * @return array<string,mixed>
     */
    function apcampaign_component(): array
    {
        return APCampaign_Contract::component();
    }
}

if (!function_exists('apcampaign_score_campaign')) {
    /**
     * @param array<string,mixed> $campaign
     * @return array<string,mixed>
     */
    function apcampaign_score_campaign(array $campaign): array
    {
        return APCampaign_Intelligence::score_campaign($campaign);
    }
}

if (!function_exists('apcampaign_spam_score')) {
    /**
     * @return array<string,mixed>
     */
    function apcampaign_spam_score(string $subject, string $html = '', string $preheader = ''): array
    {
        return APCampaign_Spam_Score::score($subject, $html, $preheader);
    }
}

if (defined('WP_CLI') && WP_CLI) {
    WP_CLI::add_command('apcampaign', 'APCampaign_CLI');
}
