<?php
/**
 * Plugin Name: WPNest Returns for WooCommerce
 * Plugin URI: https://wpnest.pl/wpnest-returns-for-woocommerce/
 * Description: EU Directive 2023/2673 compliance — adds a "Return this order" button to WooCommerce My Account, manages return requests with custom statuses, and notifies customer and merchant by email with a confirmation PDF.
 * Version: 1.0.0
 * Author: WPNest
 * Author URI: https://wpnest.pl
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: wpnest-returns-for-woocommerce
 * Domain Path: /languages
 * Requires at least: 6.2
 * Requires PHP: 7.4
 * Requires Plugins: woocommerce
 * WC requires at least: 7.0
 * WC tested up to: 10.0
 *
 * @package WPNest_Returns_For_WooCommerce
 */

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

define( 'WPNEST_RFW_VERSION', '1.0.0' );
define( 'WPNEST_RFW_DB_VERSION', '1.2.0' );
define( 'WPNEST_RFW_PLUGIN_FILE', __FILE__ );
define( 'WPNEST_RFW_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
define( 'WPNEST_RFW_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'WPNEST_RFW_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );

/**
 * Main plugin class.
 */
class WPNest_Returns_For_WooCommerce {

    /**
     * @var WPNest_Returns_For_WooCommerce
     */
    private static $instance = null;

    public static function get_instance() {
        if ( null === self::$instance ) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    private function __construct() {
        register_activation_hook( __FILE__, array( 'WPNest_RFW_Activator', 'activate' ) );
        register_deactivation_hook( __FILE__, array( 'WPNest_RFW_Deactivator', 'deactivate' ) );

        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-activator.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-deactivator.php';

        add_action( 'plugins_loaded', array( $this, 'init' ) );
        add_action( 'before_woocommerce_init', array( $this, 'declare_hpos_compatibility' ) );
    }

    public function init() {
        if ( ! class_exists( 'WooCommerce' ) ) {
            add_action( 'admin_notices', array( $this, 'woocommerce_missing_notice' ) );
            return;
        }

        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-upgrade.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-order-status.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-reason-codes.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-request-model.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-request-item-model.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-eligibility.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-pdf-generator.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-guest-handler.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-email-token.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-form-handler.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-refund-service.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-email-notifications.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-frontend.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-returns-page.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-rest-controller.php';

        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-admin-settings.php';
        require_once WPNEST_RFW_PLUGIN_DIR . 'includes/class-admin-dashboard.php';

        WPNest_RFW_Order_Status::get_instance();
        WPNest_RFW_Email_Notifications::get_instance();
        WPNest_RFW_Frontend::get_instance();
        WPNest_RFW_Returns_Page::get_instance();
        WPNest_RFW_REST_Controller::get_instance();
        WPNest_RFW_Admin_Settings::get_instance();
        WPNest_RFW_Admin_Dashboard::get_instance();

        WPNest_RFW_Activator::maybe_upgrade_db();
        WPNest_RFW_Upgrade::maybe_run();
    }

    public function declare_hpos_compatibility() {
        if ( class_exists( '\Automattic\WooCommerce\Utilities\FeaturesUtil' ) ) {
            \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( 'custom_order_tables', __FILE__, true );
        }
    }

    /**
     * Single source of truth for Pro detection. A future WPNest Returns Pro plugin
     * hooks into this filter and returns true; everywhere else in the codebase can
     * branch on `WPNest_Returns_For_WooCommerce::is_pro_active()`.
     */
    public static function is_pro_active() {
        return (bool) apply_filters( 'wpnest_rfw_pro_active', false );
    }

    public function woocommerce_missing_notice() {
        echo '<div class="notice notice-error"><p>'
            . esc_html__( 'WPNest Returns for WooCommerce requires the WooCommerce plugin to be active.', 'wpnest-returns-for-woocommerce' )
            . '</p></div>';
    }
}

/**
 * Returns full table name for return requests.
 *
 * @return string
 */
function wpnest_rfw_requests_table() {
    global $wpdb;
    return $wpdb->prefix . 'wpnest_rfw_requests';
}

/**
 * Returns full table name for status change log.
 *
 * @return string
 */
function wpnest_rfw_status_log_table() {
    global $wpdb;
    return $wpdb->prefix . 'wpnest_rfw_status_log';
}

/**
 * Returns full table name for per-item return rows.
 *
 * @return string
 */
function wpnest_rfw_request_items_table() {
    global $wpdb;
    return $wpdb->prefix . 'wpnest_rfw_request_items';
}

WPNest_Returns_For_WooCommerce::get_instance();
