<?php

/**
 * @wordpress-plugin
 * Plugin Name: BookiFlex
 * Description: Direct Booking Plugin for Apartments and Short-Term Rentals
 * Plugin URI: https://bookiflex.com
 * Version: 1.7.1
 * Author: BookiFlex
 * License: GPL-2.0-or-later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Requires at least: 6.6
 * Requires PHP: 8.1
 * Text Domain: bookiflex
 * Domain Path: /languages
 *
 */
declare (strict_types = 1);
use BookiFlex\Plugin\Plugin;
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
// Check PHP version and extensions
if ( version_compare( phpversion(), '8.1', '<' ) ) {
    add_action( 'admin_notices', function () {
        printf( '<div class="notice notice-error is-dismissible"><p>%s</p></div>', esc_html__( 'BookiFlex requires PHP 8.1 or higher. Please upgrade your PHP version.', 'bookiflex' ) );
    } );
    return;
}
if ( !extension_loaded( 'pdo_mysql' ) ) {
    add_action( 'admin_notices', function () {
        printf( '<div class="notice notice-error is-dismissible"><p>%s</p></div>', esc_html__( 'BookiFlex requires "pdo_mysql" extension. Please activate this extension.', 'bookiflex' ) );
    } );
    return;
}
// Carbon Fields bootstrap guard: detect if another plugin already loaded CF before us.
// PSR-4 lazy loading prevents "Cannot redeclare class", but a version mismatch can
// cause missing methods. We capture this state before our autoloader runs.
define( 'BOOKIFLEX_CARBON_FIELDS_PRELOADED', class_exists( 'Carbon_Fields\\Carbon_Fields', false ) );
// Load Composer autoloader
// Guard: when both free and pro versions are installed simultaneously, only one vendor
// is loaded. The second plugin registers its own PSR-4 paths with the existing loader.
$bookiflexAutoloader = __DIR__ . '/vendor/autoload.php';
if ( !file_exists( $bookiflexAutoloader ) ) {
    add_action( 'admin_notices', function () {
        printf( '<div class="notice notice-error is-dismissible"><p>%s</p></div>', esc_html__( 'BookiFlex dependencies are missing. Please run "composer install" in the plugin directory.', 'bookiflex' ) );
    } );
    return;
}
if ( !class_exists( 'ComposerAutoloaderInitBookiFlex', false ) ) {
    require_once $bookiflexAutoloader;
} else {
    $bookiflexLoader = ComposerAutoloaderInitBookiFlex::getLoader();
    $bookiflexLoader->addPsr4( 'BookiFlex\\Plugin\\', __DIR__ . '/src/' );
    $bookiflexLoader->addPsr4( 'BookiFlex\\Core\\', __DIR__ . '/src/Core' );
    $bookiflexLoader->addPsr4( 'BookiFlex\\Extensions\\', __DIR__ . '/src/Extensions' );
    unset($bookiflexLoader);
}
unset($bookiflexAutoloader);
// Set Freemius
if ( !function_exists( 'bookiflex_freemius' ) ) {
    function bookiflex_freemius() {
        global $bookiflex_freemius;
        if ( !isset( $bookiflex_freemius ) ) {
            $firstPath = ( get_option( 'bookiflex_onboarding_completed' ) ? 'admin.php?page=bookiflex-settings' : 'admin.php?page=bookiflex-onboarding' );
            // Include Freemius SDK.
            // SDK is auto-loaded through Composer
            $bookiflex_freemius = fs_dynamic_init( [
                'id'               => '21965',
                'slug'             => 'bookiflex',
                'premium_slug'     => 'bookiflex-pro',
                'type'             => 'plugin',
                'public_key'       => 'pk_6cef228fe785555b55cdf6fe090c4',
                'is_premium'       => false,
                'premium_suffix'   => 'Business',
                'has_addons'       => false,
                'has_paid_plans'   => true,
                'is_org_compliant' => true,
                'menu'             => [
                    'slug'       => 'bookiflex-settings',
                    'first-path' => $firstPath,
                    'parent'     => [
                        'slug' => 'bookiflex',
                    ],
                ],
                'is_live'          => true,
            ] );
        }
        return $bookiflex_freemius;
    }

    // Init Freemius.
    bookiflex_freemius();
    // Signal that SDK was initiated.
    do_action( 'bookiflex_freemius_loaded' );
}
// Guard against double initialization when both free and pro versions are active simultaneously.
// The second plugin to load contributes only its PSR-4 paths (handled above) and exits here.
if ( defined( 'BOOKIFLEX_VERSION' ) ) {
    add_action( 'admin_notices', function () {
        printf(
            '<div class="notice notice-warning"><p>%s <a href="%s" data-turbo="false">%s</a></p></div>',
            esc_html__( 'BookiFlex: both Free and Pro versions are active simultaneously. Please deactivate the one you do not need.', 'bookiflex' ),
            esc_url( admin_url( 'plugins.php' ) ),
            esc_html__( 'Go to Plugins', 'bookiflex' )
        );
    } );
    return;
}
// Define plugin constants
define( 'BOOKIFLEX_VERSION', '1.7.1' );
define( 'BOOKIFLEX_FILE', __FILE__ );
define( 'BOOKIFLEX_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
// Define dev constants
if ( !defined( 'BOOKIFLEX_ENV' ) ) {
    define( 'BOOKIFLEX_ENV', 'production' );
}
if ( !defined( 'BOOKIFLEX_MAILHOG_DSN' ) ) {
    define( 'BOOKIFLEX_MAILHOG_DSN', '' );
}
require_once plugin_dir_path( __FILE__ ) . '/vendor/woocommerce/action-scheduler/action-scheduler.php';
$plugin = new Plugin(__FILE__, BOOKIFLEX_VERSION);
// Initialize plugin
add_action( 'plugins_loaded', function () use($plugin) {
    $plugin->init();
    // Make plugin instance globally available
    $GLOBALS['bookiflex_plugin'] = $plugin;
}, 10 );
/**
 * Get plugin instance
 *
 * @return Plugin|null
 */
if ( !function_exists( 'bookiflex' ) ) {
    function bookiflex() : ?Plugin {
        return $GLOBALS['bookiflex_plugin'] ?? null;
    }

}