<?php
/**
 * Plugin Name: Portfolio Filter Gallery
 * Plugin URI: https://awplife.com/
 * Description: Create stunning filterable portfolio galleries with masonry layouts, lightbox, and drag-drop management.
 * Version: 2.0.2
 * Author: A WP Life
 * Author URI: https://awplife.com/
 * License: GPLv2 or later
 * Text Domain: portfolio-filter-gallery
 * Domain Path: /languages
 */

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

/**
 * Plugin constants - wrapped with defined() checks to prevent conflicts
 * if both free and premium versions are accidentally activated
 */
if ( ! defined( 'PFG_VERSION' ) ) {
    define( 'PFG_VERSION', '2.0.2' );
}
if ( ! defined( 'PFG_PLUGIN_FILE' ) ) {
    define( 'PFG_PLUGIN_FILE', __FILE__ );
}
if ( ! defined( 'PFG_PLUGIN_PATH' ) ) {
    define( 'PFG_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
}
if ( ! defined( 'PFG_PLUGIN_URL' ) ) {
    define( 'PFG_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
if ( ! defined( 'PFG_PLUGIN_BASENAME' ) ) {
    define( 'PFG_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
}

// Legacy constants for backward compatibility
if ( ! defined( 'PFG_PLUGIN_VER' ) ) {
    define( 'PFG_PLUGIN_VER', PFG_VERSION );
}
if ( ! defined( 'PFG_PLUGIN_NAME' ) ) {
    define( 'PFG_PLUGIN_NAME', 'Portfolio Filter Gallery' );
}
if ( ! defined( 'PFG_PLUGIN_SLUG' ) ) {
    define( 'PFG_PLUGIN_SLUG', 'awl_filter_gallery' );
}
if ( ! defined( 'PFG_PLUGIN_DIR' ) ) {
    define( 'PFG_PLUGIN_DIR', PFG_PLUGIN_PATH );
}

/**
 * The code that runs during plugin activation.
 */
function pfg_activate() {
    require_once PFG_PLUGIN_PATH . 'includes/class-pfg-activator.php';
    PFG_Activator::activate();
    
    // Onboarding tour
    require_once PFG_PLUGIN_PATH . 'includes/class-pfg-onboarding-tour.php';
    PFG_Onboarding_Tour::activate();
}
/**
 * The code that runs during plugin deactivation.
 */
function pfg_deactivate() {
    require_once PFG_PLUGIN_PATH . 'includes/class-pfg-deactivator.php';
    PFG_Deactivator::deactivate();
}

register_activation_hook( __FILE__, 'pfg_activate' );
register_deactivation_hook( __FILE__, 'pfg_deactivate' );

/**
 * Check if Premium version is active - if so, stop Free version execution
 * Premium has priority and includes all Free features
 */
function pfg_check_premium_conflict() {
    // If Premium version's constant is defined, Premium is already loaded
    if ( defined( 'PFG_PREMIUM' ) && PFG_PREMIUM ) {
        // Show admin notice
        add_action( 'admin_notices', 'pfg_free_premium_conflict_notice' );
        return true;
    }
    return false;
}

/**
 * Show notice when both versions are active
 */
function pfg_free_premium_conflict_notice() {
    echo '<div class="notice notice-warning is-dismissible">';
    echo '<p><strong>Portfolio Filter Gallery:</strong> ' . esc_html__( 'Both Free and Premium versions are active. Please deactivate the Free version to use Premium features.', 'portfolio-filter-gallery' ) . '</p>';
    echo '</div>';
}

/**
 * Show upgrade notice on plugins page for major updates.
 * This displays a prominent warning when updating to version 2.0.0+
 */
add_action( 'in_plugin_update_message-portfolio-filter-gallery/portfolio-filter-gallery.php', 'pfg_show_upgrade_notice', 10, 2 );
function pfg_show_upgrade_notice( $data, $response ) {
    // Show warning for major version updates
    if ( isset( $data['new_version'] ) && version_compare( $data['new_version'], '2.0.0', '>=' ) ) {
        echo '<br><br>';
        echo '<span style="display:block; background:#fcf0f1; border-left:4px solid #d63638; padding:12px 15px; margin-top:10px;">';
        echo '<strong style="color:#d63638;">⚠️ ' . esc_html__( 'Major Update - Please Read Before Updating!', 'portfolio-filter-gallery' ) . '</strong><br>';
        echo esc_html__( 'Version 2.0 includes a completely redesigned admin interface. Your existing galleries will be automatically migrated.', 'portfolio-filter-gallery' );
        echo '<br><strong>' . esc_html__( 'We strongly recommend backing up your site before updating.', 'portfolio-filter-gallery' ) . '</strong>';
        echo '</span>';
    }
}

/**
 * Show a dismissible admin notice at the top of plugins page about major update
 */
add_action( 'admin_notices', 'pfg_major_update_admin_notice' );
function pfg_major_update_admin_notice() {
    // Only show on plugins page
    $screen = get_current_screen();
    if ( ! $screen || $screen->id !== 'plugins' ) {
        return;
    }
    
    // Check if update is available
    $update_plugins = get_site_transient( 'update_plugins' );
    if ( ! isset( $update_plugins->response['portfolio-filter-gallery/portfolio-filter-gallery.php'] ) ) {
        return;
    }
    
    $update = $update_plugins->response['portfolio-filter-gallery/portfolio-filter-gallery.php'];
    
    // Only show for major updates (2.0.0+)
    if ( ! isset( $update->new_version ) || version_compare( $update->new_version, '2.0.0', '<' ) ) {
        return;
    }
    
    // Check if dismissed
    if ( get_option( 'pfg_major_update_notice_dismissed' ) === $update->new_version ) {
        return;
    }
    ?>
    <div class="notice notice-warning is-dismissible">
        <p>
            <strong>⚠️ <?php esc_html_e( 'Portfolio Filter Gallery - Major Update Available!', 'portfolio-filter-gallery' ); ?></strong><br>
            <?php esc_html_e( 'Version 2.0 includes a completely redesigned admin interface. Your galleries will be automatically migrated.', 'portfolio-filter-gallery' ); ?>
            <strong><?php esc_html_e( 'Please backup your site before updating.', 'portfolio-filter-gallery' ); ?></strong>
        </p>
    </div>
    <?php
}

/**
 * Begins execution of the plugin.
 */
function pfg_run() {
    // Stop if Premium is already active
    if ( pfg_check_premium_conflict() ) {
        return;
    }
    
    require_once PFG_PLUGIN_PATH . 'includes/class-portfolio-filter-gallery.php';
    $plugin = new Portfolio_Filter_Gallery();
    $plugin->run();
}
pfg_run();