<?php
/**
 * Plugin Name:       Integration for WooCommerce to Zoho Apps
 * Plugin URI:        https://integrazo.com/products/woocommerce-zoho-integration
 * Description:       Sync WooCommerce orders, customers, and products to Zoho CRM, Books, Invoice, Inventory, Desk, Campaigns, and Bigin automatically.
 * Version:           1.0.3
 * Author:            Integrazo
 * Author URI:        https://integrazo.com/
 * License:           GPLv2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       integrazo-wc-zoho-integration
 * Requires at least: 5.0
 * Requires PHP:      7.0
 * Requires Plugins:  woocommerce
 */

if (! defined('ABSPATH')) {
    exit; // No direct access
}

// Define Plugin Constants
define('INTEGRAZO_WCZO_PLUGIN_VERSION', '1.0.3');
define('INTEGRAZO_WCZO_PLUGIN_NAME', 'Integration for WooCommerce to Zoho Apps');
define('INTEGRAZO_WCZO_PLUGIN_FILE', __FILE__);
define('INTEGRAZO_WCZO_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('INTEGRAZO_WCZO_PLUGIN_URL', plugin_dir_url(__FILE__));

// Include core functionality files
require_once INTEGRAZO_WCZO_PLUGIN_DIR . 'includes/includes.php';


/**
 * Plugin activation callback.
 *
 * @param bool $network_wide Whether the plugin is being activated network-wide in multisite.
 */
function integrazo_wczo_activate($network_wide)
{

    // Multisite activation handling
    if (is_multisite() && $network_wide) {
        $sites = get_sites(['fields' => 'ids']);
        foreach ($sites as $site_id) {
            switch_to_blog($site_id);
            $db_setup = new INTEGRAZO_WCZO_DB_Setup();
            $db_setup->create_tables();
            restore_current_blog();
        }
    } else {
        // Single site or site-specific activation
        $db_setup = new INTEGRAZO_WCZO_DB_Setup();
        $db_setup->create_tables();
    }
}

// Register the activation hook
register_activation_hook(__FILE__, 'integrazo_wczo_activate');

/**
 * Function to handle tasks during plugin deactivation
 */
function integrazo_wczo_deactivate($network_wide)
{
    if (is_multisite() && $network_wide) {
        // Handle network-wide deactivation
        $sites = get_sites(array('fields' => 'ids')); // Fetch only site IDs for efficiency
        foreach ($sites as $site_id) {
            switch_to_blog($site_id); // Switch to the current site in the loop
            //  integrazo_wczo_uninstall(); // Call uninstall function for each site
            restore_current_blog(); // Restore the original site context
        }
    } else {
        // Handle single site deactivation
        //integrazo_wczo_uninstall(); // Call uninstall function for the current site
    }
}

// Hook for plugin deactivation
register_deactivation_hook(__FILE__, 'integrazo_wczo_deactivate');


/**
 * Add "Settings" and "Upgrade to Pro" links to the plugin action links.
 *
 * @param array $links Existing plugin action links.
 * @return array Modified plugin action links.
 */
function integrazo_wczo_plugin_action_links($links)
{
    // Add Settings link
    $settings_link = '<a href="admin.php?page=integrazo-wc-zoho-integration&tab=setup">' . esc_html__('Settings', 'integrazo-wc-zoho-integration') . '</a>';
    array_unshift($links, $settings_link);
    if (!integrazo_wczo_is_pro_file_exists()) {
        $upgrade_link = '<a href="https://integrazo.com/products/woocommerce-zoho-integration" style="color: rgb(0, 163, 42); font-weight: bold;" target="_blank">' . esc_html__('Upgrade to Pro', 'integrazo-wc-zoho-integration') . '</a>';
        $links[] = $upgrade_link;
    }
    return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'integrazo_wczo_plugin_action_links');
