<?php
/*
Plugin Name: PDF InvoiceX
Description: Professional PDF invoice generator for WooCommerce with email attachments, admin/frontend downloads, and customizable templates.
Version: 1.0.0
Author: Vishwas R
Text Domain: pdf-invoicex
Requires at least: 6.0
Requires PHP: 7.4
WC requires at least: 8.0
WC tested up to: 9.8
License: GPL-2.0+
*/
defined('ABSPATH') || exit;

define('PDFINVX51_VERSION', '1.0.0');
define('PDFINVX51_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('PDFINVX51_PLUGIN_URL', plugin_dir_url(__FILE__));
define('PDFINVX51_TEMPLATE_DIR', PDFINVX51_PLUGIN_DIR . 'templates/');

// Make sure composer dependencies are installed
if (!file_exists(PDFINVX51_PLUGIN_DIR . 'vendor/autoload.php')) {
    add_action('admin_notices', function() {
        printf(
            '<div class="error"><p>%s</p></div>',
            esc_html__('PDF InvoiceX requires dependencies to be installed. Please contact the plugin author.', 'pdf-invoicex')
        );
    });
    return;
}

// Enqueue styles for PDF invoice based on template
function pdfinvx_enqueue_pdf_styles($template = 'default') {
    $templates = ['default', 'classic', 'modern', 'colorful'];
    $template = in_array($template, $templates) ? $template : 'default';

    global $wp_styles;
    
    // Remove your specific style from the 'done' array
    $style_handle = "pdfinvx-{$template}";
    if (isset($wp_styles->done) && is_array($wp_styles->done)) {
        $key = array_search($style_handle, $wp_styles->done);
        if ($key !== false) {
            unset($wp_styles->done[$key]);
        }
    }

    wp_register_style(
        "pdfinvx-{$template}",
        PDFINVX51_PLUGIN_URL . "assets/css/templates/{$template}.css",
        array(),
        PDFINVX51_VERSION,
        'all'
    );
    wp_enqueue_style("pdfinvx-{$template}");
}

add_action('plugins_loaded', function () {
    if (!class_exists('WooCommerce')) {
        add_action('admin_notices', function () {
            printf(
                '<div class="error"><p>%s</p></div>',
                esc_html__('PDF InvoiceX requires WooCommerce to be installed and active.', 'pdf-invoicex')
            );
        });
        return;
    }
    
    // Load dependencies
    require_once PDFINVX51_PLUGIN_DIR . 'vendor/autoload.php';
    
    // Load plugin files
    require_once PDFINVX51_PLUGIN_DIR . 'includes/class-pdf-invoicex-settings.php';
    require_once PDFINVX51_PLUGIN_DIR . 'includes/admin/class-pdf-invoicex-admin.php';
    require_once PDFINVX51_PLUGIN_DIR . 'includes/admin/class-pdf-invoicex-bulk.php';
    require_once PDFINVX51_PLUGIN_DIR . 'includes/frontend/class-pdf-invoicex-frontend.php';
    require_once PDFINVX51_PLUGIN_DIR . 'includes/pdf/class-pdf-invoicex-generator.php';
    
    // Initialize plugin components
    $settings = new PDFInvX_Settings();
    new PDFInvX_Admin($settings);
    new PDFInvX_Bulk();
    new PDFInvX_Frontend();
    
    // Initialize email attachment hooks if settings say so
    $pdfinvx_settings = get_option('pdfinvx_settings', []);
    if (!empty($pdfinvx_settings['attach_to_emails']) && is_array($pdfinvx_settings['attach_to_emails'])) {
        add_filter('woocommerce_email_attachments', function($attachments, $email_id, $order, $email) {
            $generator = new PDFInvX_Generator();
            return $generator->attach_to_email($attachments, $email_id, $order, $email);
        }, 10, 4);
    }
    
    // Enqueue frontend styles and scripts
    add_action('wp_enqueue_scripts', function() {
        if (is_account_page()) {
            wp_enqueue_style(
                'pdf-invoicex-frontend-styles', 
                PDFINVX51_PLUGIN_URL . 'assets/css/frontend.css',
                array(),
                PDFINVX51_VERSION
            );
            
            wp_enqueue_script(
                'pdf-invoicex-frontend',
                PDFINVX51_PLUGIN_URL . 'assets/js/frontend.js',
                array('jquery'),
                PDFINVX51_VERSION,
                true
            );
            
            // If using Font Awesome for the icon
            wp_enqueue_style(
                'font-awesome',
                PDFINVX51_PLUGIN_URL . 'assets/css/fontawesome.min.css',
                array(),
                '6.4.2'
            );
        }
    });
    
    // Register PDF styles enqueuing hook
    add_action('pdfinvx_before_pdf_render', 'pdfinvx_enqueue_pdf_styles', 10, 1);
});

add_action('init', function () {});

// Register activation hook to create necessary directories and flush rewrite rules
register_activation_hook(__FILE__, function() {
    // Create invoice directory
    $upload_dir = wp_upload_dir();
    $invoice_dir = $upload_dir['basedir'] . '/pdf-invoicex-invoices/';
    
    if (!is_dir($invoice_dir)) {
        wp_mkdir_p($invoice_dir);
    }
    
    // Create .htaccess file to protect the invoice directory
    if (!file_exists($invoice_dir . '.htaccess')) {
        $htaccess_content = "Order deny,allow\nDeny from all\n";
        @file_put_contents($invoice_dir . '.htaccess', $htaccess_content);
    }
    
    // Flag to flush rewrite rules on next load to avoid doing it during activation
    update_option('pdfinvx_flush_rewrite_rules', true);
});

// Register deactivation hook to flush rewrite rules
register_deactivation_hook(__FILE__, function() {
    flush_rewrite_rules();
});
?>