<?php
/**
 * Plugin Name: Frisbii Pay
 * Description: Get a plug-n-play payment solution for WooCommerce, that is easy to use, highly secure and is built to maximize the potential of your e-commerce.
 * Author: Frisbii
 * Author URI: https://frisbii.com
 * Version: 1.8.13.2
 * Text Domain: reepay-checkout-gateway
 * Domain Path: /languages
 * WC requires at least: 3.0.0
 * WC tested up to: 10.8.1
 *
 * @package Reepay\Checkout
 */

use Reepay\Checkout\Api;
use Reepay\Checkout\Api\Controller\DebugController;
use Reepay\Checkout\Api\Controller\MetaFieldsController;
use Reepay\Checkout\DIContainer;
use Reepay\Checkout\Gateways;
use Reepay\Checkout\Gateways\ReepayGateway;
use Reepay\Checkout\Plugin\LifeCycle;
use Reepay\Checkout\Plugin\Statistics;
use Reepay\Checkout\Plugin\WoocommerceExists;
use Reepay\Checkout\Plugin\WoocommerceHPOS;

defined( 'ABSPATH' ) || exit();

/**
 * Main plugin class
 */
class WC_ReepayCheckout {
	/**
	 * Class instance
	 *
	 * @var WC_ReepayCheckout
	 */
	private static $instance;

	/**
	 * Settings array
	 *
	 * @var array
	 */
	private $settings = array();

	/**
	 * Gateways class instance
	 *
	 * @var Gateways
	 */
	private $gateways = null;

	/**
	 * Dependency injection container
	 *
	 * @var DIContainer
	 */
	private $di_container = null;

	/**
	 * Constructor
	 */
	private function __construct() {
		include_once __DIR__ . '/vendor/autoload.php';

		Statistics::get_instance( $this->get_setting( 'plugin_file' ) );

		new LifeCycle( $this->get_setting( 'plugin_path' ) );

		new Reepay\Checkout\Functions\Main();

		add_action( 'plugins_loaded', array( $this, 'include_classes' ), 0 );

		// Priority 0 to ensure gateways are initialized before other plugins that depend on them
		add_action( 'init', array( $this, 'init' ), 0 );

		add_action( 'rest_api_init', array( $this, 'init_rest_api' ) );
	}

	/**
	 * Init
	 */
	public function init(){
		// Load text domain first before any classes that use translation functions
		load_plugin_textdomain( 'reepay-checkout-gateway', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );

		// Initialize classes that may use translation functions
		new WoocommerceExists();
		new WoocommerceHPOS();

		// Initialize gateways after translations are loaded
		if ( WoocommerceExists::woo_activated() ) {
			$this->gateways = new Reepay\Checkout\Gateways();
		}
	}

	/**
	 * Get main class instance.
	 *
	 * @return WC_ReepayCheckout
	 */
	public static function get_instance(): WC_ReepayCheckout {
		static $instance_requested = false;

		if ( true === $instance_requested && is_null( self::$instance ) ) {
			$message = 'Function Frisbii Pay called in time of initialization main plugin class. Recursion prevented';

			// Security: Log stack trace to file instead of displaying to users
			if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
				error_log( 'Reepay Plugin Initialization Error: ' . print_r( //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
					debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ), //phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_debug_backtrace
					true
				) );
			}

			// Security: Display generic error message to users
			wp_die( esc_html__( 'Plugin initialization error. Please contact support.', 'reepay-checkout-gateway' ) );
		}

		$instance_requested = true;

		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	/**
	 * Get plugin or reepay checkout gateway setting
	 *
	 * @param string $name Setting key.
	 *
	 * @return string|string[]|null
	 */
	public function get_setting( string $name ) {
		if ( empty( $this->settings ) ) {
			$gateway_settings = get_option( 'woocommerce_reepay_checkout_settings' );

			if ( ! is_array( $gateway_settings ) ) {
				$gateway_settings = array();
			}

			if ( isset( $gateway_settings['private_key'] ) ) {
				$gateway_settings['private_key'] = apply_filters( 'woocommerce_reepay_private_key', $gateway_settings['private_key'] );
			}

			if ( isset( $gateway_settings['private_key_test'] ) ) {
				$gateway_settings['private_key_test'] = apply_filters( 'woocommerce_reepay_private_key_test', $gateway_settings['private_key_test'] );
			}

			require_once ABSPATH . 'wp-admin/includes/plugin.php';
			$plugin_data = get_file_data( __FILE__, array( 'Version' => 'Version' ) );

			$this->settings = array(
				'plugin_version'             => $plugin_data['Version'],

				'plugin_file'                => __FILE__,
				'plugin_basename'            => plugin_basename( __FILE__ ),

				'plugin_url'                 => plugin_dir_url( __FILE__ ),
				'plugin_path'                => plugin_dir_path( __FILE__ ),

				'templates_url'              => plugin_dir_url( __FILE__ ) . 'templates/',
				'templates_path'             => plugin_dir_path( __FILE__ ) . 'templates/',

				'css_url'                    => plugin_dir_url( __FILE__ ) . 'assets/dist/css/',
				'css_path'                   => plugin_dir_path( __FILE__ ) . 'assets/dist/css/',

				'js_url'                     => plugin_dir_url( __FILE__ ) . 'assets/dist/js/',
				'js_path'                    => plugin_dir_path( __FILE__ ) . 'assets/dist/js/',

				'images_url'                 => plugin_dir_url( __FILE__ ) . 'assets/images/',
				'images_path'                => plugin_dir_path( __FILE__ ) . 'assets/images/',

				'assets_url'                 => plugin_dir_url( __FILE__ ) . 'assets/',
				'assets_path'                => plugin_dir_path( __FILE__ ) . 'assets/',

				'vite_url'                   => plugin_dir_url( __FILE__ ) . 'assets/dist/vite/',
				'vite_path'                  => plugin_dir_path( __FILE__ ) . 'assets/dist/vite/',

				'languages_path'             => plugin_dir_path( __FILE__ ) . 'languages/',

				'private_key'                => ! empty( $gateway_settings['private_key'] ) ? $gateway_settings['private_key'] : '',
				'private_key_test'           => ! empty( $gateway_settings['private_key_test'] ) ? $gateway_settings['private_key_test'] : '',
				'test_mode'                  => ! empty( $gateway_settings['test_mode'] ) ? $gateway_settings['test_mode'] : '',
				'settle'                     => ! empty( $gateway_settings['settle'] ) ? $gateway_settings['settle'] : array(),
				'allow_partial_settle'       => ! empty( $gateway_settings['allow_partial_settle'] ) ? $gateway_settings['allow_partial_settle'] : 'yes',
				'skip_pro_rated_on_thankyou' => ! empty( $gateway_settings['skip_pro_rated_on_thankyou'] ) ? $gateway_settings['skip_pro_rated_on_thankyou'] : 'no',
				'language'                   => ! empty( $gateway_settings['language'] ) ? $gateway_settings['language'] : '',
				'debug'                      => ! empty( $gateway_settings['debug'] ) ? $gateway_settings['debug'] : '',
				'show_meta_fields_in_orders' => ! empty( $gateway_settings['show_meta_fields_in_orders'] ) ? $gateway_settings['show_meta_fields_in_orders'] : '',
				'show_meta_fields_in_users'  => ! empty( $gateway_settings['show_meta_fields_in_users'] ) ? $gateway_settings['show_meta_fields_in_users'] : '',
				'disable_auto_settle'        => ! empty( $gateway_settings['disable_auto_settle'] ) ? $gateway_settings['disable_auto_settle'] : '',
				'payment_type'               => ! empty( $gateway_settings['payment_type'] ) ? $gateway_settings['payment_type'] : '',
				'skip_order_lines'           => ! empty( $gateway_settings['skip_order_lines'] ) ? $gateway_settings['skip_order_lines'] : '',
				'enable_order_autocancel'    => ! empty( $gateway_settings['enable_order_autocancel'] ) ? $gateway_settings['enable_order_autocancel'] : '',
				'is_webhook_configured'      => ! empty( $gateway_settings['is_webhook_configured'] ) ? $gateway_settings['is_webhook_configured'] : '',
				'handle_failover'            => ! empty( $gateway_settings['handle_failover'] ) ? $gateway_settings['handle_failover'] : '',
				'order_handle_prefix'        => isset ( $gateway_settings['order_handle_prefix'] ) ? $gateway_settings['order_handle_prefix'] : 'order-',
				'payment_button_text'        => ! empty( $gateway_settings['payment_button_text'] ) ? $gateway_settings['payment_button_text'] : '',
				'enable_sync'                => ! empty( $gateway_settings['enable_sync'] ) ? $gateway_settings['enable_sync'] : '',
				'status_created'             => ! empty( $gateway_settings['status_created'] ) ? $gateway_settings['status_created'] : '',
				'status_authorized'          => ! empty( $gateway_settings['status_authorized'] ) ? $gateway_settings['status_authorized'] : '',
				'status_settled'             => ! empty( $gateway_settings['status_settled'] ) ? $gateway_settings['status_settled'] : '',
				'logo_height'                => ! empty( $gateway_settings['logo_height'] ) ? $gateway_settings['logo_height'] : '20',
			);
		}

		return $this->settings[ $name ] ?? null;
	}

	/**
	 * Reset options from database. Now using only for testing purposes
	 */
	public function reset_settings() {
		$this->settings = null;
		$this->get_setting( '' );
	}

	/**
	 * Wrapper of wc_get_template function
	 *
	 * @param string $template Template name.
	 * @param array  $args Arguments.
	 * @param bool   $return_template Return or echo template.
	 */
	public function get_template( string $template, array $args = array(), bool $return_template = false ) {
		if ( $return_template ) {
			ob_start();
		}

		wc_get_template(
			$template,
			$args,
			'',
			$this->get_setting( 'templates_path' )
		);

		if ( $return_template ) {
			return ob_get_clean();
		}

		return true;
	}

	/**
	 * Set logging source.
	 *
	 * @param ReepayGateway|WC_Order|string $source Source for logging.
	 *
	 * @return Api
	 */
	public function api( $source ): Api {
		/**
		 * Api instance
		 *
		 * @var Api $api
		 */
		$api = $this->di()->get( Api::class );
		$api->set_logging_source( $source );

		return $api;
	}

	/**
	 * Get gateways class instance
	 *
	 * @return Gateways|null
	 */
	public function gateways(): ?Gateways {
		return $this->gateways;
	}

	/**
	 * Get dependency injection container
	 *
	 * @return DIContainer
	 */
	public function di(): DIContainer {
		if ( is_null( $this->di_container ) ) {
			$this->di_container = new DIContainer();
		}

		return $this->di_container;
	}

	/**
	 * WooCommerce Loaded: load classes
	 *
	 * @return void
	 */
	public function include_classes() {
		if ( ! WoocommerceExists::woo_activated() ) {
			return;
		}

		new Reepay\Checkout\Admin\Main();

		new Reepay\Checkout\Tokens\Main();

		new Reepay\Checkout\Plugin\UpdateDB();

		new Reepay\Checkout\OrderFlow\Main();

		// Gateways initialization moved to init() to ensure translations are loaded first

		new Reepay\Checkout\Integrations\Main();

		new Reepay\Checkout\Frontend\Main();

		new Reepay\Checkout\Actions\Main();

		// Initialize Analytics Sync for better WooCommerce Analytics integration
		new Reepay\Checkout\Analytics\AnalyticsSync();
	}

	/**
	 * Init rest api
	 *
	 * @return void
	 */
	public function init_rest_api(): void {
		( new MetaFieldsController() )->register_routes();
		( new DebugController() )->register_routes();
	}
}

/**
 * Get reepay checkout instance
 *
 * @return WC_ReepayCheckout
 */
function reepay(): WC_ReepayCheckout {
	return WC_ReepayCheckout::get_instance();
}

reepay();
