<?php
/**
 * Prevent direct access to the file.
 */

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

// Define Constant for Unfied API.
define( 'ARXEC_API_BASE', 'https://aramex-prod-gw.boomi.cloud:443/ws/rest/api' );
define( 'ARXEC_ORDER_ENDPOINT', ARXEC_API_BASE . '/orders' );
define( 'ARXEC_AUTH_ENDPOINT', ARXEC_API_BASE . '/orders/wms/inquiry_stock' );
define( 'ARXEC_ORDER_UPDATE_ENDPOINT', ARXEC_API_BASE . '/orders/update-or-cancel' );

define( 'ARXEC_ORDER_CREATE_SHIPMENT', ARXEC_API_BASE . '/shipping/shipment/store/create-shipment' );
define( 'ARXEC_ORDER_RATE_CALCULATOR', ARXEC_API_BASE . '/shipping/rate-calculator/calculate-multi-rates' );

// Aramex stock tag.
define( 'ARXEC_STOCK_TAG', 'Aramex-Stock' );

// Aramex facility ID (Netherlands).
define( 'ARXEC_NETHERLAND_FACILITY', 'WMWHSE9' );

// Tracking URLs.
define( 'ARXEC_NL_TRACKING_BASE_URL', 'https://www.track.aramex.nl/order-status?code=eyJlbWFpbCI6InRyYWNrQGFyYW1leC5ubCJ9&order-number=' );
define( 'ARXEC_DEFAULT_TRACKING_BASE_URL', 'https://www.aramex.com/sa/en/track/results?mode=0&ShipmentNumber=' );

// eCommCore portal login base URL.
define( 'ARXEC_UNIFIED_PORTAL_LOGIN_URL', 'https://www.aramex.com/ecommcore/login' );

// Secret key for authentication with Aramex eCommCore API.
define('WOOCOM_API_SECRET', '4ebe60c4882d6cfaafb900b27c15359dfedd6227c3bebb08e8b19e57112b8e5587139e0b17655ad4b4aa2e21cfecc12bd0ba2496c36d3a4e60e114077ac02d9d');

/**
 * Function to generate a unique identifier for the store.
 * This is used to identify the store in the Aramex eCommCore system.
 */
function aramex_unified_portal_plugin_unique_identifier() {
    if ( ! get_option( 'aramex_unified_identifier' ) ) {
        $unique_id = 'ARX-' . md5(
            wp_generate_uuid4() .
            AUTH_KEY .
            NONCE_KEY .
            site_url() .
            microtime(true)
        );
        add_option( 'aramex_unified_identifier', $unique_id, '', 'no' );
    }
}

// function to create default shipping zone named Aramex Shipping on plugin activation.
function aramex_create_default_shipping_zone() {
    if (!class_exists('WC_Shipping_Zones')) {
        return;
    }

    // Check if zone already exists
    $existing_zones = WC_Shipping_Zones::get_zones();
    $zone_exists = false;

    foreach ($existing_zones as $zone) {
        if (strtolower($zone['zone_name']) === 'aramex shipping') {
            $zone_exists = true;
            break;
        }
    }

    if (!$zone_exists) {
        // Create a new zone
        $zone = new WC_Shipping_Zone();
        $zone->set_zone_name('Aramex Shipping');
        $zone->set_locations([]); // apply to all locations
        $zone_id = $zone->save();

        // Add a dummy flat rate method to make zone active
        $zone = new WC_Shipping_Zone($zone_id);
        $zone->add_shipping_method('flat_rate');
    }
}

/**
 * Function to delete User Token on plugin deactivation.
 */
function aramex_unified_portal_plugin_deactivation() {
	
	// Delete the token from wp_options.
	delete_option( 'aramex_unified_jwt_token' );
}


/**
 * Create htaccess rules on plugin activation.
 */
function aramex_unified_portal_get_htaccess_path() {
	$upload_dir = wp_upload_dir();
	return trailingslashit( $upload_dir['basedir'] ) . 'aramex-unified/.htaccess';
}

/**
 * Create htaccess rules on plugin activation.
 */
function aramex_unified_portal_create_htaccess_file() {
	$htaccess_path = aramex_unified_portal_get_htaccess_path();

	// Ensure the directory exists.
	if ( ! file_exists( dirname( $htaccess_path ) ) ) {
		wp_mkdir_p( dirname( $htaccess_path ) );
	}

	if ( ! file_exists( $htaccess_path ) ) {
		$htaccess_content = '# Allow access to CSS, JS, Images' . "\n" .
				'<FilesMatch "\.(css|js|png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot|otf)$">' . "\n" .
				'    <IfModule !mod_authz_core.c>' . "\n" .
				'        Allow from all' . "\n" .
				'    </IfModule>' . "\n" .
				'    <IfModule mod_authz_core.c>' . "\n" .
				'        Require all granted' . "\n" .
				'    </IfModule>' . "\n" .
				'</FilesMatch>' . "\n\n" .
				'# Block access to PHP files directly in plugins folder' . "\n" .
				'<FilesMatch "\.(php|ini|log|sh|sql|bak|conf)$">' . "\n" .
				'    Order Allow,Deny' . "\n" .
				'    Deny from all' . "\n" .
				'</FilesMatch>';

		file_put_contents( $htaccess_path, $htaccess_content );
	}
}
register_activation_hook( __FILE__, 'aramex_unified_portal_create_htaccess_file' );

/**
 * Delete htaccess on plugin deactivation.
 */
function aramex_unified_portal_htaccess_file() {
	$htaccess_path = aramex_unified_portal_get_htaccess_path();
	if ( file_exists( $htaccess_path ) ) {
		wp_delete_file( $htaccess_path );
	}
}
register_deactivation_hook( __FILE__, 'aramex_unified_portal_htaccess_file' );
