<?php

/**
 * Plugin Name: PlantShop for WooCommerce
 * Description: PlantShop for WooCommerce
 * Version: 1.3.0
 * Requires at least: 6.0
 * Requires PHP: 8.3
 * Tested up to: 6.9
 * Author: Ritec GmbH
 * Text Domain: plantshop-wc
 * Domain Path: /languages
 * Requires Plugins: woocommerce
 * License: GPLv3
 * License URI: https://www.gnu.org/licenses/gpl-3.0.html
 */
use Rtc\PlantShopWC\Database\Migrator;
use Rtc\PlantShopWC\PlantShopWC;
use Rtc\PlantShopWC\PlantShopWC_Freemius_Proxy;
defined( 'ABSPATH' ) || exit;
if ( function_exists( 'plantshop_wc_fs' ) || function_exists( 'plantshop_wc_check_dependencies' ) || function_exists( 'init_plantshop_wc' ) ) {
    add_action( 'admin_notices', function () {
        echo '<div class="notice notice-warning"><p><strong>' . esc_html__( 'PlantShop for WooCommerce', 'plantshop-wc' ) . '</strong> ' . esc_html__( 'Only one version of PlantShop for WooCommerce can be active at a time. Please deactivate either the free or the Pro plugin.', 'plantshop-wc' ) . '</p></div>';
    } );
    if ( function_exists( 'plantshop_wc_log' ) ) {
        plantshop_wc_log( 'Detected multiple PlantShop for WooCommerce variants in one request. Skipping duplicate bootstrap.', 'warning' );
    }
    return;
}
if ( !defined( 'PLANTSHOP_WC_PATH' ) ) {
    define( 'PLANTSHOP_WC_PATH', plugin_dir_path( __FILE__ ) );
}
if ( !defined( 'PLANTSHOP_WC_PUBLIC_PATH' ) ) {
    define( 'PLANTSHOP_WC_PUBLIC_PATH', PLANTSHOP_WC_PATH . 'public/' );
}
if ( !defined( 'PLANTSHOP_WC_ASSET_PATH' ) ) {
    define( 'PLANTSHOP_WC_ASSET_PATH', PLANTSHOP_WC_PUBLIC_PATH . 'assets/' );
}
if ( !defined( 'PLANTSHOP_WC_ICON_PATH' ) ) {
    define( 'PLANTSHOP_WC_ICON_PATH', PLANTSHOP_WC_ASSET_PATH . 'icons/' );
}
if ( !defined( 'PLANTSHOP_WC_LOGO_PATH' ) ) {
    define( 'PLANTSHOP_WC_LOGO_PATH', PLANTSHOP_WC_ASSET_PATH . 'logo.svg' );
}
if ( !defined( 'PLANTSHOP_WC_URL' ) ) {
    define( 'PLANTSHOP_WC_URL', plugin_dir_url( __FILE__ ) );
}
if ( !defined( 'PLANTSHOP_WC_PUBLIC_URL' ) ) {
    define( 'PLANTSHOP_WC_PUBLIC_URL', PLANTSHOP_WC_URL . 'public/' );
}
if ( !defined( 'PLANTSHOP_WC_ASSET_URL' ) ) {
    define( 'PLANTSHOP_WC_ASSET_URL', PLANTSHOP_WC_PUBLIC_URL . 'assets/' );
}
if ( !defined( 'PLANTSHOP_WC_ICON_URL' ) ) {
    define( 'PLANTSHOP_WC_ICON_URL', PLANTSHOP_WC_ASSET_URL . 'icons/' );
}
if ( !defined( 'PLANTSHOP_WC_LOGO_URL' ) ) {
    define( 'PLANTSHOP_WC_LOGO_URL', PLANTSHOP_WC_ASSET_URL . 'logo.svg' );
}
// Some constants are normally defined by WordPress. Define safe fallbacks for non-WP/unit environments.
if ( !defined( 'MINUTE_IN_SECONDS' ) ) {
    define( 'MINUTE_IN_SECONDS', 60 );
}
if ( !function_exists( 'get_plugin_data' ) ) {
    require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_data = get_plugin_data( __FILE__, false, false );
if ( !defined( 'PLANTSHOP_WC_PLUGIN_VERSION' ) ) {
    define( 'PLANTSHOP_WC_PLUGIN_VERSION', $plugin_data["Version"] );
}
if ( !function_exists( 'plantshop_wc_log' ) ) {
    function plantshop_wc_log(  string $message, string $level = 'error'  ) : void {
        if ( function_exists( 'wc_get_logger' ) ) {
            wc_get_logger()->log( $level, $message, array(
                'source' => 'plantshop-wc',
            ) );
            return;
        }
        if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
            // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
            error_log( "[plantshop-wc][{$level}] {$message}" );
        }
    }

}
if ( !function_exists( 'plantshop_wc_demo_is_granted__premium_only' ) ) {
}
require_once __DIR__ . '/deps/scoper-autoload.php';
require_once __DIR__ . '/vendor/autoload.php';
$fs_start = __DIR__ . '/vendor/freemius/wordpress-sdk/start.php';
if ( file_exists( $fs_start ) ) {
    require_once $fs_start;
} else {
    add_action( 'admin_notices', function () {
        echo '<div class="notice notice-warning"><p>' . esc_html__( 'Freemius SDK (start.php) not found. Some features may be unavailable.', 'plantshop-wc' ) . '</p></div>';
    } );
    plantshop_wc_log( 'Freemius SDK start.php not found.', 'warning' );
}
// Ensure the Freemius exception class exists even if the SDK didn't load, so try/catch doesn't fatally error.
if ( !class_exists( 'Freemius_Exception' ) ) {
    class Freemius_Exception extends \Exception {
    }

}
if ( !defined( 'PLANTSHOP_WC_MIN_WC_VERSION' ) ) {
    define( 'PLANTSHOP_WC_MIN_WC_VERSION', '10.0.0' );
}
if ( !function_exists( 'plantshop_wc_fs' ) ) {
    /**
     * @throws Freemius_Exception
     */
    function plantshop_wc_fs() {
        static $fs = null, $proxy = null;
        // Test helper: allow resetting the cached Freemius instance in WP integration tests.
        if ( defined( 'PLANTSHOP_WC_TESTS_ALLOW_OVERRIDE' ) && PLANTSHOP_WC_TESTS_ALLOW_OVERRIDE && function_exists( 'plantshop_wc_tests_should_reset_fs' ) && plantshop_wc_tests_should_reset_fs() ) {
            $fs = null;
            $proxy = null;
        }
        if ( !isset( $fs ) ) {
            if ( function_exists( 'fs_dynamic_init' ) ) {
                // SDK is auto-loaded through Composer
                $fs = fs_dynamic_init( array(
                    'id'               => '20588',
                    'slug'             => 'plantshop-wc',
                    'premium_slug'     => 'plantshop-wc-pro',
                    'type'             => 'plugin',
                    'public_key'       => 'pk_13e4672565bc9016dbe9022507d63',
                    'is_premium'       => false,
                    'premium_suffix'   => 'Pro',
                    'has_addons'       => false,
                    'has_paid_plans'   => true,
                    'is_org_compliant' => true,
                    'menu'             => array(
                        'slug'       => 'plantshop-wc',
                        'first-path' => 'admin.php?page=plantshop-wc&tab=welcome',
                        'support'    => false,
                    ),
                    'navigation'       => 'tabs',
                    'is_live'          => true,
                ) );
            } else {
                // Fallback stub to avoid fatals in environments where the SDK is unavailable (e.g., CI integration tests).
                $fs = new class {
                    public function __call( $name, $args ) {
                        return false;
                    }

                }
;
                plantshop_wc_log( 'Freemius SDK not initialized (fs_dynamic_init missing). Using stub.', 'warning' );
            }
        }
        if ( $fs->is__premium_only() && plantshop_wc_demo_is_granted__premium_only() ) {
            if ( !isset( $proxy ) ) {
                $proxy = new PlantShopWC_Freemius_Proxy($fs);
            }
            return $proxy;
        }
        return $fs;
    }

    // Init Freemius.
    try {
        plantshop_wc_fs();
        do_action( 'plantshop-wc_fs_loaded' );
    } catch ( Freemius_Exception $e ) {
        plantshop_wc_log( 'Freemius init failed: ' . $e->getMessage(), 'error' );
        add_action( 'admin_notices', function () use($e) {
            echo '<div class="notice notice-error"><p>' . esc_html__( 'Freemius initialization failed:', 'plantshop-wc' ) . ' ' . esc_html( $e->getMessage() ) . '</p></div>';
        } );
    }
}
if ( !function_exists( 'plantshop_wc_check_dependencies' ) ) {
    function plantshop_wc_check_dependencies() : bool {
        // Testing override to force success in isolated WP test environments.
        if ( defined( 'PLANTSHOP_WC_TESTS_ASSUME_WC_PRESENT' ) && PLANTSHOP_WC_TESTS_ASSUME_WC_PRESENT ) {
            return true;
        }
        // Test override hook: allows unit tests to force dependency failure without loading WooCommerce.
        if ( defined( 'PLANTSHOP_WC_TESTS_ALLOW_OVERRIDE' ) && PLANTSHOP_WC_TESTS_ALLOW_OVERRIDE && function_exists( 'plantshop_wc_tests_force_wc_missing' ) && plantshop_wc_tests_force_wc_missing() ) {
            return false;
        }
        // WooCommerce not active
        if ( !class_exists( 'WooCommerce' ) ) {
            add_action( 'admin_notices', function () {
                echo '<div class="notice notice-error"><p><strong>' . esc_html__( 'PlantShop for WooCommerce', 'plantshop-wc' ) . '</strong> requires <strong>WooCommerce</strong> to be installed and active.</p></div>';
            } );
            return false;
        }
        // Version too low (guard against missing WC_VERSION constant)
        $wc_version = ( defined( 'WC_VERSION' ) ? WC_VERSION : null );
        if ( defined( 'PLANTSHOP_WC_TESTS_ALLOW_OVERRIDE' ) && PLANTSHOP_WC_TESTS_ALLOW_OVERRIDE && function_exists( 'plantshop_wc_tests_force_wc_version' ) ) {
            $forced_version = plantshop_wc_tests_force_wc_version();
            if ( is_string( $forced_version ) && $forced_version !== '' ) {
                $wc_version = $forced_version;
            }
        }
        if ( $wc_version === null || version_compare( $wc_version, PLANTSHOP_WC_MIN_WC_VERSION, '<' ) ) {
            add_action( 'admin_notices', function () use($wc_version) {
                echo '<div class="notice notice-error"><p><strong>' . esc_html__( 'PlantShop for WooCommerce', 'plantshop-wc' ) . '</strong> requires WooCommerce version ' . esc_html( PLANTSHOP_WC_MIN_WC_VERSION ) . ' or higher.' . (( $wc_version !== null ? ' You are running ' . esc_html( $wc_version ) . '.' : '' )) . '</p></div>';
            } );
            return false;
        }
        return true;
    }

}
if ( !function_exists( 'init_plantshop_wc' ) ) {
    function init_plantshop_wc() : void {
        if ( function_exists( 'fs_override_i18n' ) ) {
            fs_override_i18n( array(
                'premium-version'       => __( 'Pro', 'plantshop-wc' ),
                'free-version'          => __( 'Free', 'plantshop-wc' ),
                'download-paid-version' => __( 'Download Pro Version', 'plantshop-wc' ),
            ), 'plantshop-wc' );
        }
        if ( !plantshop_wc_check_dependencies() ) {
            return;
        }
        $plugin = new PlantShopWC();
        $plugin->init();
    }

}
add_action( 'init', 'init_plantshop_wc', 0 );