<?php
/**
 * Plugin Name: Klamra Paycal for Aspaclaria
 * Plugin URI: https://paycal.online/
 * Description: PayCal branded checkout and payment-management connector for WooCommerce, synced with PayCal Platform and approved payment providers.
 * Version: 1.4.2
 * Requires at least: 6.0
 * Requires PHP: 8.0
 * Author: Klamra22
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: klamra-paycal-for-aspaclaria
 */

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

define('PAYCAL_VERSION', '1.4.2');
define('PAYCAL_PLUGIN_FILE', __FILE__);
define('PAYCAL_PATH', plugin_dir_path(__FILE__));
define('PAYCAL_URL', plugin_dir_url(__FILE__));

add_action('before_woocommerce_init', function () {
    if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
        \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('custom_order_tables', __FILE__, true);
    }
});

if (!function_exists('paycal_debug_log')) {
    function paycal_debug_log(string $message): void {
        if (!defined('WP_DEBUG') || !WP_DEBUG || !function_exists('wc_get_logger')) {
            return;
        }

        wc_get_logger()->debug(sanitize_text_field($message), ['source' => 'paycal']);
    }
}

if (!file_exists(PAYCAL_PATH . 'includes/Core/Plugin.php')) {
    paycal_debug_log('[PayCal] Missing core plugin loader.');
    add_action('admin_notices', function () {
        if (current_user_can('manage_options')) {
            echo '<div class="notice notice-error"><p>' . esc_html__('PayCal could not load because a required file is missing. Reinstall the plugin package.', 'klamra-paycal-for-aspaclaria') . '</p></div>';
        }
    });
    return;
}

try {
    require_once PAYCAL_PATH . 'includes/Core/Plugin.php';
} catch (Throwable $e) {
    paycal_debug_log('[PayCal] Failed loading core plugin loader: ' . $e->getMessage());
    add_action('admin_notices', function () {
        if (current_user_can('manage_options')) {
            echo '<div class="notice notice-error"><p>' . esc_html__('PayCal failed to load safely. Check the server error log and reinstall the plugin package if needed.', 'klamra-paycal-for-aspaclaria') . '</p></div>';
        }
    });
    return;
}
if (class_exists(\PayCal\Core\Plugin::class)) {
    \PayCal\Core\Plugin::init();
}
