<?php
/**
 * Plugin Name: PayCal Invoices
 * Description: מערכת חשבוניות פנימית לווקומרס: יצירה אוטומטית, מספור, שליחה במייל, ועורך עיצוב נגיש.
 * Version: 2.0.0
 * Author: PayCal
 */
if (!defined('ABSPATH')) exit;

define('PAYCAL_INV_PATH', plugin_dir_path(__FILE__));
define('PAYCAL_INV_URL', plugin_dir_url(__FILE__));

// Back-compat constants (older code).
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
if (!defined('ASP_INV_PATH')) define('ASP_INV_PATH', PAYCAL_INV_PATH);
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound
if (!defined('ASP_INV_URL'))  define('ASP_INV_URL',  PAYCAL_INV_URL);

$paycal_invoice_files = [
    'includes/cpt.php',
    'includes/numbering.php',
    'includes/order-hook.php',
    'includes/email.php',
    'includes/pdf/download.php',
    'includes/render.php',
    'admin/settings.php',
];

foreach ($paycal_invoice_files as $paycal_invoice_file) {
    $path = PAYCAL_INV_PATH . $paycal_invoice_file;
    if (!file_exists($path)) {
        \paycal_debug_log('[PayCal Invoices] Missing file: ' . $path);
        add_action('admin_notices', function () use ($paycal_invoice_file) {
            if (current_user_can('manage_options')) {
                /* translators: %s: missing PayCal Invoices file path. */
                echo '<div class="notice notice-error"><p>' . esc_html(sprintf(__('PayCal Invoices could not load %s. Reinstall the plugin package.', 'klamra-paycal-for-aspaclaria'), $paycal_invoice_file)) . '</p></div>';
            }
        });
        return;
    }

    try {
        require_once $path;
    } catch (Throwable $e) {
        \paycal_debug_log('[PayCal Invoices] Failed loading ' . $path . ': ' . $e->getMessage());
        add_action('admin_notices', function () use ($paycal_invoice_file) {
            if (current_user_can('manage_options')) {
                /* translators: %s: failed PayCal Invoices file path. */
                echo '<div class="notice notice-error"><p>' . esc_html(sprintf(__('PayCal Invoices failed to load %s safely. Check the server error log.', 'klamra-paycal-for-aspaclaria'), $paycal_invoice_file)) . '</p></div>';
            }
        });
        return;
    }
}
