<?php
/**
 * Plugin Name: Direct Chat Inquiry by XD
 * Plugin URI: https://wordpress.org/plugins/direct-chat-inquiry-by-xd/
 * Text Domain: direct-chat-inquiry-by-xd
 * Description: Add WhatsApp inquiry button to WooCommerce product pages.
 * Version: 1.2.1
 * Author: Xpert Dezine IT
 * Author URI: https://facebook.com/xpertdezineit
 * License: GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * WC requires at least: 3.0.0
 * WC tested up to: 7.0.0
 */

defined('ABSPATH') || exit;

// Define plugin constants
if (!defined('DC_WA_INQUIRY_BY_XD_VERSION')) {
    define('DC_WA_INQUIRY_BY_XD_VERSION', '1.2.1');
}
if (!defined('DC_WA_INQUIRY_BY_XD_PLUGIN_DIR')) {
    define('DC_WA_INQUIRY_BY_XD_PLUGIN_DIR', plugin_dir_path(__FILE__));
}
if (!defined('DC_WA_INQUIRY_BY_XD_PLUGIN_URL')) {
    define('DC_WA_INQUIRY_BY_XD_PLUGIN_URL', plugin_dir_url(__FILE__));
}
if (!defined('DC_WA_INQUIRY_BY_XD_DB_PREFIX')) {
    define('DC_WA_INQUIRY_BY_XD_DB_PREFIX', 'dc_inquiry_xd_');
}

// Check if WooCommerce is active
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
    add_action('admin_notices', 'dc_wa_inquiry_by_xd_missing_wc_notice');
    return;
}

function dc_wa_inquiry_by_xd_missing_wc_notice() {
    ?>
    <div class="notice notice-error">
        <p><?php esc_html_e('Direct Chat Inquiry by XD requires WooCommerce to be installed and active!', 'direct-chat-inquiry-by-xd'); ?></p>
    </div>
    <?php
}

// Declare compatibility with WooCommerce features
add_action('before_woocommerce_init', function() {
    if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
            'custom_order_tables', 
            __FILE__, 
            true
        );
    }
});

// Include required files
$required_files = array(
    'includes/class-dc-wa-inquiry-by-xd-loader.php',
    'includes/class-dc-wa-inquiry-by-xd-core.php',
    'includes/class-dc-wa-inquiry-by-xd-activator.php',
    'includes/class-dc-wa-inquiry-by-xd-deactivator.php',
    'admin/class-dc-wa-inquiry-by-xd-admin.php',
    'public/class-dc-wa-inquiry-by-xd-public.php'
);

foreach ($required_files as $file) {
    $file_path = DC_WA_INQUIRY_BY_XD_PLUGIN_DIR . $file;
    if (!file_exists($file_path)) {
        if (is_admin()) {
            add_action('admin_notices', function() use ($file) {
                /* translators: %s: The name of the missing file */
                echo '<div class="notice notice-error"><p>' .
                     esc_html(sprintf(
                         __('Direct Chat Inquiry by XD: Required file is missing! Please reinstall the plugin.', 'direct-chat-inquiry-by-xd'),
                         $file
                     )) . '</p></div>';
            });
        }
        return;
    }
    require_once $file_path;
}

// Initialize the plugin
function dc_wa_inquiry_by_xd_init() {
    try {
        $plugin = new DC_WA_Inquiry_By_XD_Core();
        $plugin->run();
    } catch (Exception $e) {
        if (is_admin()) {
            add_action('admin_notices', function() use ($e) {
                echo '<div class="notice notice-error"><p>' . 
                     esc_html__('Direct Chat Inquiry by XD initialization error: ', 'direct-chat-inquiry-by-xd') . 
                     esc_html($e->getMessage()) . '</p></div>';
            });
        }
    }
}
add_action('plugins_loaded', 'dc_wa_inquiry_by_xd_init');

/**
 * Add settings link to plugin actions
 */
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'dc_wa_inquiry_by_xd_add_settings_link');

function dc_wa_inquiry_by_xd_add_settings_link($links) {
    $settings_link = '<a href="' . admin_url('admin.php?page=dc-wa-inquiry-by-xd') . '">' . __('Settings', 'direct-chat-inquiry-by-xd') . '</a>';
    array_unshift($links, $settings_link);
    return $links;
}

// Activation/Deactivation hooks
register_activation_hook(__FILE__, 'dc_wa_inquiry_by_xd_activate');
register_deactivation_hook(__FILE__, 'dc_wa_inquiry_by_xd_deactivate');

function dc_wa_inquiry_by_xd_activate() {
    DC_WA_Inquiry_By_XD_Activator::activate();
}

function dc_wa_inquiry_by_xd_deactivate() {
    DC_WA_Inquiry_By_XD_Deactivator::deactivate();
}