<?php
if (!defined('ABSPATH')) exit;

register_activation_hook(SEARCHSHIFTER_PLUGIN_FILE, function () {
    if (!get_option('ss_site_uuid')) {
        update_option('ss_site_uuid', wp_generate_uuid4());
    }
});

function searchshifter_register_site_with_hub() {

    $existing = get_option('ss_api_token');
    if ($existing) return;

    $uuid = get_option('ss_site_uuid');
    if (!$uuid) return;

    $response = wp_remote_post(
        SEARCHSHIFTER_HUB_API . '/api/register-site',
        [
            'timeout' => 10,
            'headers' => ['Content-Type' => 'application/json'],
            'body' => wp_json_encode([
                'site_uuid' => $uuid,
                'domain'    => wp_parse_url(home_url(), PHP_URL_HOST),
            ]),
        ]
    );

    if (is_wp_error($response)) return;

    $data = json_decode(wp_remote_retrieve_body($response), true);
    $token = $data['token'] ?? $data['api_token'] ?? null;

    if ($token) {
        update_option('ss_api_token', sanitize_text_field($token));
    }
}

function ss_register_site_with_hub() {
    searchshifter_register_site_with_hub();
}

register_activation_hook(SEARCHSHIFTER_PLUGIN_FILE, function () {
    searchshifter_register_site_with_hub();
});

function searchshifter_prevent_network_activation($network_wide) {
    if ($network_wide) {
        deactivate_plugins(plugin_basename(SEARCHSHIFTER_PLUGIN_FILE));
        wp_die(
            'SearchShifter cannot be network-activated on multisite installations.',
            'Network Activation Blocked',
            ['back_link' => true]
        );
    }
}

function ss_prevent_network_activation($network_wide) {
    searchshifter_prevent_network_activation($network_wide);
}

register_activation_hook(SEARCHSHIFTER_PLUGIN_FILE, 'searchshifter_prevent_network_activation');

function searchshifter_install_mu_bridge() {
    global $wp_filesystem;

    if (!function_exists('WP_Filesystem')) {
        require_once ABSPATH . 'wp-admin/includes/file.php';
    }

    WP_Filesystem();

    if (!$wp_filesystem) {
        update_option('ss_mu_bridge_failed', 'filesystem_unavailable');
        return;
    }

    $mu_dir  = WP_CONTENT_DIR . '/mu-plugins';
    $mu_file = $mu_dir . '/searchshifter-bridge.php';

    if (!file_exists($mu_dir)) {
        wp_mkdir_p($mu_dir);
    }

    if (file_exists($mu_file)) return;

    $code = "<?php /* SearchShifter Bridge */ ?>";

    $wp_filesystem->put_contents($mu_file, $code, FS_CHMOD_FILE);
}

function ss_install_mu_bridge() {
    searchshifter_install_mu_bridge();
}

register_activation_hook(SEARCHSHIFTER_PLUGIN_FILE, 'searchshifter_install_mu_bridge');

register_activation_hook(SEARCHSHIFTER_PLUGIN_FILE, [ 'SS_Setup', 'activate' ]);
register_deactivation_hook(SEARCHSHIFTER_PLUGIN_FILE, [ 'SS_Setup', 'deactivate' ]);

register_activation_hook(SEARCHSHIFTER_PLUGIN_FILE, function () {
    add_option('ss_do_activation_redirect', true);
});

add_action('admin_init', function () {
    if (get_option('ss_do_activation_redirect', false)) {
        delete_option('ss_do_activation_redirect');
        wp_safe_redirect(admin_url('admin.php?page=searchshifter'));
        exit;
    }
});