<?php
/**
 * Plugin Name: Cregis Payment for WooCommerce
 * Plugin URI: https://support.cregis.com/payment-engine/introduction
 * Description: Accept cryptocurrency payments via Cregis in your WooCommerce store
 * Version: 2.0.3
 * Author: Cregis
 * Author URI: https://cregis.com
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: cregis-payment-for-woocommerce
 * Requires Plugins: woocommerce
 * WC requires at least: 3.0
 * WC tested up to: 10.5
 */

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

// Define plugin constants
define('CREGIS_PAYMENT_VERSION', '2.0.3');
define('CREGIS_PAYMENT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('CREGIS_PAYMENT_PLUGIN_URL', plugin_dir_url(__FILE__));
define('CREGIS_PAYMENT_API_BASE_URL', 'https://api.cregis.com');

// Load translations early
load_plugin_textdomain('cregis-payment-for-woocommerce', false, dirname(plugin_basename(__FILE__)) . '/languages');

// Include required files
require_once CREGIS_PAYMENT_PLUGIN_DIR . 'includes/class-cregis-api.php';
require_once CREGIS_PAYMENT_PLUGIN_DIR . 'includes/class-cregis-payment.php';
require_once CREGIS_PAYMENT_PLUGIN_DIR . 'includes/class-cregis-admin.php';
require_once CREGIS_PAYMENT_PLUGIN_DIR . 'includes/pages/class-cregis-return-handler.php';

// Add WooCommerce payment gateway
function cregis_add_payment_gateway($gateways) {
    $gateways['cregispay'] = 'Cregis_WC_Gateway';
    return $gateways;
}

// Register WooCommerce payment gateway
function cregis_register_payment_gateway() {
    if (class_exists('WooCommerce') && class_exists('WC_Payment_Gateway')) {
        require_once CREGIS_PAYMENT_PLUGIN_DIR . 'includes/class-cregis-wc-gateway.php';
        add_filter('woocommerce_payment_gateways', 'cregis_add_payment_gateway', 999);
    }
}

// Initialize the plugin
function cregis_payment_init() {
    // Load text domain for translations
    load_plugin_textdomain('cregis-payment-for-woocommerce', false, dirname(plugin_basename(__FILE__)) . '/languages');
    
    // Initialize the payment class
    new Cregis_Payment();

    // Initialize admin settings
    if (is_admin()) {
        new Cregis_Admin();
    }

    // Initialize return handler
    $return_handler = new Cregis_Return_Handler();
    $return_handler->init();

    // Initialize blocks payment method
    if (class_exists('WooCommerce')) {
        add_action('woocommerce_blocks_loaded', function() {
            if (class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
                require_once CREGIS_PAYMENT_PLUGIN_DIR . 'includes/class-cregis-blocks-payment-method.php';
            }
        });
    }
}

// Hook into WordPress initialization
// Load after WooCommerce
add_action('plugins_loaded', 'cregis_payment_init', 10);

// Register WooCommerce payment gateway (later priority to ensure WooCommerce is loaded)
add_action('plugins_loaded', 'cregis_register_payment_gateway', 15);

// Activation hook
function cregis_payment_activate() {
    // Create necessary database tables or options
    add_option('cregis_payment_settings', array(
        'project_id' => '',
        'secret_key' => '',
        'api_mode' => 'test',
        'debug' => false
    ));

    // Enable the payment gateway by default
    // Note: WooCommerce requires this specific option name for payment gateway settings
    add_option('woocommerce_cregispay_settings', array(
        'enabled' => 'yes',
        'title' => 'Cryptocurrency Payment',
        'description' => 'Pay with cryptocurrencies using Cregis'
    ));

    // Flush rewrite rules for /cregis-return/ URL
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'cregis_payment_activate');

// Deactivation hook
function cregis_payment_deactivate() {
    // Clean up any temporary data
}
register_deactivation_hook(__FILE__, 'cregis_payment_deactivate');


