<?php
/**
 * Plugin Name: Invoicing Integration for iFirma and WooCommerce
 * Plugin URI: https://wordpress.org/plugins/invoicing-integration-for-ifirma-and-woocommerce/
 * Description: WooCommerce integration with iFirma accounting system for Polish businesses.
 * Version: 1.0.27
 * Author: Devikit
 * Author URI: https://devikit.pl/
 * Text Domain: invoicing-integration-for-ifirma-and-woocommerce
 * Domain Path: /languages
 * Requires at least: 5.8
 * Requires PHP: 7.4
 * Requires Plugins: woocommerce
 * WC requires at least: 5.0
 * License: GPLv3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 */

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

// Define plugin constants
define( 'DEVIKIT_IFIRMA_VERSION', '1.0.27' );
define( 'DEVIKIT_IFIRMA_FILE', __FILE__ );
define( 'DEVIKIT_IFIRMA_PATH', plugin_dir_path( __FILE__ ) );
define( 'DEVIKIT_IFIRMA_URL', plugin_dir_url( __FILE__ ) );
define( 'DEVIKIT_IFIRMA_BASENAME', plugin_basename( __FILE__ ) );

// Native Autoloader (No Composer needed)
spl_autoload_register( function ( $class ) {
	$prefix = 'Devikit\\Ifirma\\';
	$base_dir = DEVIKIT_IFIRMA_PATH;

	$len = strlen( $prefix );
	if ( strncmp( $prefix, $class, $len ) !== 0 ) {
		return;
	}

	$relative_class = substr( $class, $len );
	
	// Map namespaces to directories
	$map = [
		'Admin\\' => 'admin/',
		'Api\\'   => 'api/',
		'Frontend\\' => 'includes/Frontend/',
	];

	$file_path = '';
	$found = false;

	foreach ( $map as $ns_prefix => $dir ) {
		if ( strncmp( $ns_prefix, $relative_class, strlen( $ns_prefix ) ) === 0 ) {
			$class_file = str_replace( $ns_prefix, '', $relative_class );
			$file_path = $base_dir . $dir . str_replace( '\\', '/', $class_file ) . '.php';
			$found = true;
			break;
		}
	}

	if ( ! $found ) {
		// Default to includes
		$file_path = $base_dir . 'includes/' . str_replace( '\\', '/', $relative_class ) . '.php';
	}

	if ( file_exists( $file_path ) ) {
		require $file_path;
	}
} );

// Translate plugin name and description
add_filter( 'all_plugins', function( $plugins ) {
	$plugin_file = DEVIKIT_IFIRMA_BASENAME;
	if ( isset( $plugins[ $plugin_file ] ) ) {
		$plugins[ $plugin_file ]['Name'] = __( 'Invoicing Integration for iFirma and WooCommerce', 'invoicing-integration-for-ifirma-and-woocommerce' );
		$plugins[ $plugin_file ]['Description'] = __( 'WooCommerce integration with iFirma accounting system for Polish businesses.', 'invoicing-integration-for-ifirma-and-woocommerce' );
	}
	return $plugins;
} );

// Initialize the plugin
function devikit_ifirma_init() {
	if ( ! function_exists( 'WC' ) ) {
		add_action( 'admin_notices', function() {
			echo '<div class="error"><p>' . esc_html__( 'iFirma WooCommerce requires WooCommerce to be installed and active.', 'invoicing-integration-for-ifirma-and-woocommerce' ) . '</p></div>';
		} );
		return;
	}

	// Initialize main plugin class
	\Devikit\Ifirma\Plugin::instance();
}
add_action( 'plugins_loaded', 'devikit_ifirma_init' );

// Declare compatibility with HPOS
add_action( 'before_woocommerce_init', function() {
	if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
		\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', DEVIKIT_IFIRMA_FILE, true );
	}
} );
