<?php
/**
 * Plugin Name: Easy Invoice
 * Plugin URI: https://matrixaddons.com/plugins/easy-invoice
 * Description: A beautiful, full-featured invoicing solution for WordPress. Create professional invoices, quotes, and manage payments with ease.
 * Version: 2.1.20
 * Author: MatrixAddons
 * Author URI: https://matrixaddons.com
 * License: GPL v2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: easy-invoice
 * Domain Path: /languages
 * Requires at least: 5.6
 * Tested up to: 6.9
 * Requires PHP: 7.4
 * Network: false
 *
 * @package EasyInvoice
 */

// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}


// Define plugin constants.
define( 'EASY_INVOICE_VERSION', '2.1.20' );
define( 'EASY_INVOICE_PLUGIN_FILE', __FILE__ );
define( 'EASY_INVOICE_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'EASY_INVOICE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );

// Load composer autoloader if it exists.
if ( file_exists( EASY_INVOICE_PLUGIN_DIR . 'vendor/autoload.php' ) ) {
    require_once EASY_INVOICE_PLUGIN_DIR . 'vendor/autoload.php';
}

// Include standalone function files (not autoloaded by Composer)
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Helpers/NotificationHelper.php';
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Admin/Helpers/Utility.php';
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Helpers/GeneralHelper.php';
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Helpers/ToastHelper.php';
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Helpers/TaxHelper.php';
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Helpers/FormattingHelper.php';
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Helpers/EmailHelper.php';
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Helpers/EnqueueHelper.php';
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Helpers/PdfHelper.php';
require_once EASY_INVOICE_PLUGIN_DIR . 'includes/Helpers/QuoteInvoiceHelper.php';

/**
 * Initialize the plugin.
 *
 * @since 1.0.0
 * @return void
 */
function easy_invoice_init() {
    // Initialize the main plugin class.
    $plugin = \EasyInvoice\EasyInvoice::getInstance();
    $plugin->init();

    // Initialize service providers.
    $invoice_service_provider = new \EasyInvoice\Providers\InvoiceServiceProvider();
    $invoice_service_provider->register();

    $invoice_item_service_provider = new \EasyInvoice\Providers\InvoiceItemServiceProvider();
    $invoice_item_service_provider->register();

    $client_service_provider = new \EasyInvoice\Providers\ClientServiceProvider();
    $client_service_provider->register();

    $invoice_number_service_provider = new \EasyInvoice\Providers\InvoiceNumberServiceProvider();
    $invoice_number_service_provider->register();

    $quote_number_service_provider = new \EasyInvoice\Providers\QuoteNumberServiceProvider();
    $quote_number_service_provider->register();

    $quote_service_provider = new \EasyInvoice\Providers\QuoteServiceProvider();
    $quote_service_provider->register();

    // Initialize EmailManager
    $email_manager = \EasyInvoice\Services\EmailManager::getInstance();

    // Initialize form manager
    //$form_manager = \EasyInvoice\Forms\FormManager::getInstance();
   // $form_manager->setInvoiceNumberServiceProvider($invoice_number_service_provider);

    // Initialize controllers.
    $payment_controller = new \EasyInvoice\Controllers\PaymentController();
    $payment_controller->init();

    $settings_controller = new \EasyInvoice\Controllers\SettingsController();
    $settings_controller->init();

    $quote_controller = new \EasyInvoice\Controllers\QuoteController();
    $quote_controller->init();

    // Initialize shortcode manager
    $shortcode_manager = new \EasyInvoice\Shortcodes\ShortcodeManager();

    // Initialize review notice service
    \EasyInvoice\Services\ReviewNoticeService::init();

    // Initialize promotion service
    \EasyInvoice\Services\PromotionService::init();

	do_action('easy_invoice_loaded');

}
add_action( 'init', 'easy_invoice_init' );

/**
 * Handle payment gateway logging
 */
function easy_invoice_payment_gateway_log_handler($message, $level, $gateway_name) {
    $log_message = sprintf('[%s] Easy Invoice %s Gateway: %s',
        date('Y-m-d H:i:s'),
        ucfirst($gateway_name),
        $message
    );

    if (defined('WP_DEBUG') && WP_DEBUG) {
        error_log($log_message);
    }
}
add_action('easy_invoice_payment_gateway_log', 'easy_invoice_payment_gateway_log_handler', 10, 3);

// Initialize migration system
if ( class_exists( 'EasyInvoice\Migration\MigrationInit' ) ) {

    EasyInvoice\Migration\MigrationInit::init();
}
/**
 * Register post types early to ensure they're available
 */
function easy_invoice_register_post_types() {
    // Initialize the main plugin class and register post types
    $plugin = \EasyInvoice\EasyInvoice::getInstance();
    $plugin->registerPostTypes();
    $plugin->registerCustomStatuses();
}
add_action( 'init', 'easy_invoice_register_post_types', 0 ); // Priority 0 to run early

/**
 * Load textdomain early to avoid translation loading warnings.
 *
 * @since 1.0.0
 * @return void
 */
function easy_invoice_load_textdomain() {
    load_plugin_textdomain('easy-invoice', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
add_action( 'init', 'easy_invoice_load_textdomain', 1 );





/**
 * Plugin activation hook.
 *
 * @since 1.0.0
 * @return void
 */
register_activation_hook( __FILE__, 'easy_invoice_activate' );
function easy_invoice_activate() {
    // Initialization of post types so rewrite rules can be flushed properly.
    \EasyInvoice\EasyInvoice::getInstance()->registerPostTypes();

    // Initialize default settings
    $settings_controller = new \EasyInvoice\Controllers\SettingsController();
    $settings_controller->initializeSettings();

    // Record first install timestamp (do not overwrite if it already exists)
    if (!get_option('easy_invoice_installed')) {
        update_option('easy_invoice_installed', current_time('mysql'));
    }

    // Check if this is a fresh installation or an upgrade
    $stored_version = get_option('easy_invoice_version', '');

    // If no version is stored, this is a fresh installation
    if (empty($stored_version)) {
        update_option('easy_invoice_version', EASY_INVOICE_VERSION);
        update_option('easy_invoice_migration_completed', true);
    } else {
        // This is an upgrade. Do NOT bump version here; migration will set it when completed.
    }

    // Flush rewrite rules.
    flush_rewrite_rules();
}

/**
 * Plugin deactivation hook.
 *
 * @since 1.0.0
 * @return void
 */
register_deactivation_hook( __FILE__, 'easy_invoice_deactivate' );
function easy_invoice_deactivate() {
    // Flush rewrite rules.
    flush_rewrite_rules();

    // Clear scheduled events
    \EasyInvoice\Services\QuoteExpirationService::clear_schedule();
}

/**
 * Toast notification helper functions
 */

