<?php
/**
 * Plugin Name:       PB UAE VAT Invoicing System for WooCommerce
 * Plugin URI:        https://pluginsbuy.com
 * Description:       UAE FTA-compliant VAT invoices, receipts, credit notes and documents for WooCommerce with bundled PDF generation, local QR codes, VAT reports, customer downloads and email automation.
 * Version:            1.7.7
 * Author:            PluginsBuy
 * License:           GPL-2.0+
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       pb-invoicing-system
 * Domain Path:       /languages
 * Requires at least: 6.2
 * Requires PHP:      7.4
 * WC requires at least: 8.0
 * WC tested up to:   9.9
 */

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

// ── WooCommerce dependency check ───────────────────────────────────────────────
add_action( 'admin_notices', function() {
    if ( ! class_exists( 'WooCommerce' ) ) {
        echo '<div class="notice notice-error"><p>';
        echo wp_kses_post( '<strong>PB UAE VAT Invoicing System</strong> requires <a href="https://wordpress.org/plugins/woocommerce/" target="_blank">WooCommerce</a> to be installed and active.' );
        echo '</p></div>';
    }
} );

// ── Core constants ─────────────────────────────────────────────────────────────
define( 'PBUAIV_VERSION',     '1.7.7' );
define( 'PBUAIV_PLUGIN_FILE', __FILE__ );
define( 'PBUAIV_PLUGIN_DIR',  plugin_dir_path( __FILE__ ) );
define( 'PBUAIV_PLUGIN_URL',  plugin_dir_url( __FILE__ ) );
define( 'PBUAIV_SLUG',        'pb-invoicing-system' );
define( 'PBUAIV_DB_VERSION',  '1.0' );

/**
 * Output a style tag without embedding a literal tag in template files.
 * Document templates are standalone HTML/PDF/email documents, not WordPress pages.
 */
function pbuaiv_style_tag_open() : void { echo '<' . 'style>'; }
function pbuaiv_style_tag_close() : void { echo '</' . 'style>'; }
function pbuaiv_script_tag_open() : void { echo '<' . 'script>'; }
function pbuaiv_script_tag_close() : void { echo '</' . 'script>'; }


// ── Activation / Deactivation ──────────────────────────────────────────────────
require_once PBUAIV_PLUGIN_DIR . 'includes/class-pbuaiv-activator.php';
register_activation_hook(   __FILE__, [ 'PBUAIV_Activator', 'activate'   ] );
register_deactivation_hook( __FILE__, [ 'PBUAIV_Activator', 'deactivate' ] );

// ── Autoloader ─────────────────────────────────────────────────────────────────
spl_autoload_register( function( $class ) {
    if ( strpos( $class, 'PBUAIV_' ) !== 0 ) return;
    $file  = strtolower( str_replace( '_', '-', $class ) );
    $paths = [
        PBUAIV_PLUGIN_DIR . 'includes/class-' . $file . '.php',
        PBUAIV_PLUGIN_DIR . 'admin/class-'    . $file . '.php',
        PBUAIV_PLUGIN_DIR . 'public/class-'   . $file . '.php',
    ];
    foreach ( $paths as $path ) {
        if ( file_exists( $path ) ) { require_once $path; return; }
    }
} );

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

// ── Boot ───────────────────────────────────────────────────────────────────────
add_action( 'plugins_loaded', function() {
    if ( ! class_exists( 'WooCommerce' ) ) return;

    // Core services (always loaded)
    PBUAIV_Sequence::instance();
    PBUAIV_Download_Handler::instance();
    PBUAIV_Email_Manager::instance();
    PBUAIV_Order_Hooks::instance();

    if ( is_admin() ) {
        PBUAIV_Admin::instance();
        PBUAIV_Onboarding::instance();
        PBUAIV_Order_Metabox::instance();
        PBUAIV_Ajax::instance();
    }

    // Frontend My Account
    if ( ! is_admin() ) {
        new PBUAIV_Public();
    }
}, 10 );
