<?php
/**
 * Plugin Name: Račun123 for WooCommerce
 * Plugin URI: https://www.racun123.si/integracije
 * Description: Automatically send WooCommerce orders to Račun123 and create invoices.
 * Version: 1.0.2
 * Author: ATH INVEST d.o.o.
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: racun123-for-woocommerce
 * Domain Path: /languages
 */

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

define('R123_WC_VERSION', '1.0.2');
define('R123_WC_FILE', __FILE__);
define('R123_WC_PATH', plugin_dir_path(__FILE__));
define('R123_WC_URL', plugin_dir_url(__FILE__));

final class R123_WC_Plugin {
	private static ?R123_WC_Plugin $instance = null;

	public static function instance(): R123_WC_Plugin {
		if (self::$instance === null) {
			self::$instance = new self();
		}

		return self::$instance;
	}

	private function __construct() {
		$this->includes();

		add_action('plugins_loaded', [$this, 'bootstrap']);
		register_activation_hook(R123_WC_FILE, [$this, 'activate']);
		register_deactivation_hook(R123_WC_FILE, [$this, 'deactivate']);
	}

	private function includes(): void {
		require_once R123_WC_PATH . 'includes/class-r123-wc-settings.php';
		require_once R123_WC_PATH . 'includes/class-r123-wc-api-client.php';
		require_once R123_WC_PATH . 'includes/class-r123-wc-order-mapper.php';
		require_once R123_WC_PATH . 'includes/class-r123-wc-order-sync.php';
		require_once R123_WC_PATH . 'includes/class-r123-wc-admin.php';
		require_once R123_WC_PATH . 'includes/class-r123-wc-logger.php';
		require_once R123_WC_PATH . 'includes/class-r123-wc-my-account.php';
	}

	public function bootstrap(): void {
		if (!class_exists('WooCommerce')) {
			add_action('admin_notices', static function () {
				echo '<div class="notice notice-error"><p>' .
					esc_html__('Račun123 for WooCommerce requires WooCommerce to be installed and active.', 'racun123-for-woocommerce') .
					'</p></div>';
			});
			return;
		}

		$required_classes = [
			'R123_WC_Settings',
			'R123_WC_API_Client',
			'R123_WC_Order_Mapper',
			'R123_WC_Order_Sync',
			'R123_WC_Admin',
			'R123_WC_Logger',
			'R123_WC_My_Account',
		];

		foreach ($required_classes as $class_name) {
			if (!class_exists($class_name)) {
				add_action('admin_notices', static function () use ($class_name) {
					echo '<div class="notice notice-error"><p>' .
						sprintf(
							/* translators: %s is class name */
							esc_html__('Error: required class %s could not be loaded.', 'racun123-for-woocommerce'),
							esc_html($class_name)
						) .
						'</p></div>';
				});
				return;
			}
		}

		R123_WC_Settings::init();
		R123_WC_Admin::init();
		R123_WC_Order_Sync::init();
		R123_WC_My_Account::init();
	}

	public function activate(): void {
		if (!wp_next_scheduled('r123_wc_retry_failed_jobs')) {
			wp_schedule_event(time() + 300, 'five_minutes', 'r123_wc_retry_failed_jobs');
		}
	}

	public function deactivate(): void {
		wp_clear_scheduled_hook('r123_wc_retry_failed_jobs');
	}
}

/**
 * Custom cron interval (5 minutes)
 */
add_filter('cron_schedules', static function ($schedules) {
	$schedules['five_minutes'] = [
		'interval' => 300,
		'display'  => 'Every 5 Minutes',
	];

	return $schedules;
});

/**
 * HPOS compatibility (WooCommerce custom order tables)
 */
add_action('before_woocommerce_init', static function () {
	if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility(
			'custom_order_tables',
			R123_WC_FILE,
			true
		);
	}
});

R123_WC_Plugin::instance();