<?php
/**
 * PeachPay Utility Functions for ConvesioPay Integration
 */

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

/**
 * Check if PeachPay is in test mode
 */
if ( ! function_exists( 'peachpay_is_test_mode' ) ) {
	function peachpay_is_test_mode() {
		// Check if test mode is enabled via option or constant
		$test_mode = get_option( 'peachpay_test_mode', false );
		return $test_mode || defined( 'PEACHPAY_TEST_MODE' ) && PEACHPAY_TEST_MODE;
	}
}

/**
 * Get PeachPay URL for assets
 */
if ( ! function_exists( 'peachpay_url' ) ) {
	function peachpay_url( $path = '' ) {
		$plugin_url = plugin_dir_url( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) );
		return $plugin_url . $path;
	}
}

/**
 * Check if ConvesioPay is connected
 */
if ( ! function_exists( 'peachpay_convesiopay_connected' ) ) {
	function peachpay_convesiopay_connected() {
		// Use the integration class method for consistent logic
		if (class_exists('PeachPay_ConvesioPay_Integration')) {
			return PeachPay_ConvesioPay_Integration::connected();
		}
		
		// Fallback check if class not available
		$account = get_option( 'peachpay_connected_convesiopay_account', false );
		$config = get_option( 'peachpay_connected_convesiopay_config', array() );
		
		return !empty($account) && !empty($config) && 
		       isset($account['status']) && $account['status'] === 'connected';
	}
}

/**
 * Define PEACHPAY_ABSPATH if not already defined
 */
if ( ! defined( 'PEACHPAY_ABSPATH' ) ) {
	define( 'PEACHPAY_ABSPATH', plugin_dir_path( dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) ) );
}

/**
 * Check if site is in local development
 */
if ( ! function_exists( 'peachpay_is_local_development_site' ) ) {
	function peachpay_is_local_development_site() {
		return defined( 'WP_DEBUG' ) && WP_DEBUG && ( strpos( home_url(), 'localhost' ) !== false || strpos( home_url(), '127.0.0.1' ) !== false );
	}
}

/**
 * Check if site is staging
 */
if ( ! function_exists( 'peachpay_is_staging_site' ) ) {
	function peachpay_is_staging_site() {
		return strpos( home_url(), 'staging' ) !== false || strpos( home_url(), 'dev' ) !== false;
	}
}

/**
 * Register ConvesioPay feature
 */
if ( ! function_exists( 'peachpay_convesiopay_register_feature' ) ) {
	function peachpay_convesiopay_register_feature( $features ) {
		$features['convesiopay_gateways'] = array(
			'name' => 'ConvesioPay Gateways',
			'description' => 'Enable ConvesioPay payment gateways',
			'enabled' => true,
		);
		return $features;
	}
}

/**
 * Simple PeachPay class for ConvesioPay integration
 */
if ( ! class_exists( 'PeachPay' ) ) {
	class PeachPay {
		/**
		 * Get plugin path
		 */
		public static function get_plugin_path() {
			return PEACHPAY_ABSPATH;
		}
	}
} 