<?php
/**
 * Plugin Name: Harbor EU E-Invoicing for WooCommerce
 * Description: Generate EU e-invoices from WooCommerce orders as UBL 2.1 XML in the Peppol BIS Billing 3.0 or EN 16931 profile.
 * Version:     1.0.0
 * Requires at least: 6.3
 * Requires PHP: 8.0
 * Requires Plugins: woocommerce
 * Author:      Harbor Plugins
 * Author URI:  https://harborplugins.com
 * License:     GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: harbor-eu-einvoicing-for-woocommerce
 * Domain Path: /languages
 * WC requires at least: 8.0
 * WC tested up to:      10.9
 * Tested up to:         7.0
 *
 * @fs_premium_only /includes/class-pro.php, /includes/class-xrechnung-generator.php
 * @fs_premium_only /includes/class-zugferd-generator.php, /includes/class-pdf-attacher.php
 * @fs_premium_only /includes/class-fatturaPA-generator.php, /includes/class-verifactu-sender.php
 * @fs_premium_only /includes/class-peppol-sender.php, /includes/class-bulk-generator.php
 * @fs_premium_only /includes/class-font.php, /assets/fonts
 * @fs_premium_only /includes/class-remote-sender.php
 * @fs_premium_only /assets/admin-pro.js
 * @fs_premium_only /languages/harbor-eu-einvoicing-for-woocommerce-de_DE.po, /languages/harbor-eu-einvoicing-for-woocommerce-de_DE.mo
 * @fs_premium_only /languages/harbor-eu-einvoicing-for-woocommerce-es_ES.po, /languages/harbor-eu-einvoicing-for-woocommerce-es_ES.mo
 * @fs_premium_only /languages/harbor-eu-einvoicing-for-woocommerce-fr_FR.po, /languages/harbor-eu-einvoicing-for-woocommerce-fr_FR.mo
 * @fs_premium_only /languages/harbor-eu-einvoicing-for-woocommerce-it_IT.po, /languages/harbor-eu-einvoicing-for-woocommerce-it_IT.mo
 *
 * @package Harbor_EU_EInvoicing_For_WooCommerce
 */

defined( 'ABSPATH' ) || exit;

define( 'HEIN_VERSION', '1.0.0' );
define( 'HEIN_FILE', __FILE__ );
define( 'HEIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'HEIN_URL', plugin_dir_url( __FILE__ ) );
define( 'HEIN_INCLUDES', HEIN_PATH . 'includes/' );

/**
 * PHP passes the class name exactly as written at the call site, so the override map is keyed on
 * the lowercased name. file_exists() is what lets class_exists() return false, rather than fatal,
 * for the paths that are physically absent from the free build
 */
function hein_autoload( string $class ): void {
	$key = strtolower( $class );

	if ( 0 !== strpos( $key, 'hein_' ) ) {
		return;
	}

	$overrides = [ 'hein_fatturapa_generator' => 'class-fatturaPA-generator.php' ];
	$file      = $overrides[ $key ] ?? 'class-' . str_replace( '_', '-', substr( $key, 5 ) ) . '.php';
	$path      = HEIN_INCLUDES . $file;

	if ( file_exists( $path ) ) {
		require_once $path;
	}
}

spl_autoload_register( 'hein_autoload' );

function hein_invoices_table(): string {
	global $wpdb;

	return $wpdb->prefix . 'hein_invoices';
}

function hein_counters_table(): string {
	global $wpdb;

	return $wpdb->prefix . 'hein_counters';
}

function hein_numbers_table(): string {
	global $wpdb;

	return $wpdb->prefix . 'hein_invoice_numbers';
}

add_action(
	'before_woocommerce_init',
	function () {
		if ( ! class_exists( \Automattic\WooCommerce\Utilities\FeaturesUtil::class ) ) {
			return;
		}

		foreach ( [ 'custom_order_tables', 'cart_checkout_blocks' ] as $hein_feature ) {
			\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( $hein_feature, HEIN_FILE, true );
		}
	}
);

/**
 * Freemius boots at file scope, not from hein_init: WP includes this file during uninstall_plugin()
 * after plugins_loaded has already fired, so binding after_uninstall on that hook never runs and
 * the delete-data-on-uninstall setting silently does nothing. It also has no WooCommerce
 * dependency, and gating it behind that guard cuts premium licensing and updates off whenever
 * WooCommerce is inactive
 */
HEIN_Freemius::init();

add_action( 'plugins_loaded', 'hein_init', 20 );

/**
 * The generator registry is not booted here: it registers lazily on first use, so no listener can
 * miss hein_register_generators through hook-ordering
 */
function hein_init(): void {
	if ( ! class_exists( 'WooCommerce' ) ) {
		add_action( 'admin_notices', 'hein_woocommerce_notice' );

		return;
	}

	HEIN_Install::maybe_upgrade();
	HEIN_Admin_Settings::init();
	HEIN_Checkout_Fields::init();
	HEIN_Order_Actions::init();

	if ( class_exists( 'HEIN_Pro' ) ) {
		HEIN_Pro::init();
	}
}

function hein_woocommerce_notice(): void {
	// A dependency notice belongs where a user acts on it, not on every admin screen: WooCommerce is
	// inactive here so its own screen ids do not exist to match against
	$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;

	if ( ! current_user_can( 'activate_plugins' ) || ! $screen || ! in_array( $screen->id, [ 'plugins', 'dashboard' ], true ) ) {
		return;
	}

	echo '<div class="notice notice-error"><p>'
		. esc_html__(
			'Harbor EU E-Invoicing requires WooCommerce to be installed and active.',
			'harbor-eu-einvoicing-for-woocommerce'
		)
		. '</p></div>';
}

register_activation_hook( HEIN_FILE, [ 'HEIN_Install', 'activate' ] );
register_deactivation_hook( HEIN_FILE, [ 'HEIN_Install', 'deactivate' ] );
