<?php
/**
 * Plugin Name:       Zuta Lucky Wheel
 * Plugin URI:        https://zutathemes.com/en/products/skyrocket-your-conversions-and-lead-generation-with-zuta-lucky-wheel-pro
 * Description: Boost marketing engagement with a physics-based Lucky Wheel. Create interactive spin-to-win campaigns to collect leads and increase conversions.
 * Version:           1.0.2
 * Requires at least: 6.2
 * Requires PHP:      7.4
 * Author:            Zuta Themes
 * Author URI:        https://zutathemes.com
 * Text Domain:       zuta-lucky-wheel
 * Domain Path:       /languages
 * License:           GPLv2 or later
 */

// Prevent direct access to the file
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/* -------------------------------------------------------------------------
 * CONSTANTS
 * ------------------------------------------------------------------------- */
if ( ! defined( 'ZUTALW_VERSION' ) ) {
    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
        define( 'ZUTALW_VERSION', time() ); 
    } else {
        define( 'ZUTALW_VERSION', '1.0.2' );
    }
}

if ( ! defined( 'ZUTALW_DIR' ) ) {
    define( 'ZUTALW_DIR', plugin_dir_path( __FILE__ ) );
}

if ( ! defined( 'ZUTALW_URL' ) ) {
    define( 'ZUTALW_URL', plugin_dir_url( __FILE__ ) );
}

if ( ! defined( 'ZUTALW_ASSETS_URL' ) ) {
    define( 'ZUTALW_ASSETS_URL', ZUTALW_URL . 'assets/' );
}

/* -------------------------------------------------------------------------
 * API & SECURITY CONFIGURATION
 * ------------------------------------------------------------------------- */
// Set the base URL for the Django API. Change to HTTPS production URL when deploying.
if ( ! defined( 'ZUTALW_API_BASE_URL' ) ) {
    // define( 'ZUTALW_API_BASE_URL', 'http://localhost:8001/api/' ); // Local Development
    define( 'ZUTALW_API_BASE_URL', 'https://zutathemes.com/api/' ); // Production Server
}
// Shared secret key for HMAC validation. MUST match WP_PLUGIN_SECRET_KEY in Django.
if ( ! defined( 'ZUTALW_SHARED_SECRET' ) ) {
    define( 'ZUTALW_SHARED_SECRET', 'your_shared_secret_key_here' );
}

/* -------------------------------------------------------------------------
 * REQUIRE ACTIVATOR (CENTRALIZED DB SETUP)
 * ------------------------------------------------------------------------- */
require_once ZUTALW_DIR . 'includes/class-zutalw-activator.php';
require_once ZUTALW_DIR . 'includes/class-zutalw-license.php';
/* -------------------------------------------------------------------------
 * ACTIVATION HOOK
 * ------------------------------------------------------------------------- */
register_activation_hook( __FILE__, 'zutalw_run_activation_hook' );
register_deactivation_hook( __FILE__, array( 'ZUTALW_License_Manager', 'clear_cron' ) );

/**
 * Executes plugin activation logic, centralized in the Activator class.
 */
function zutalw_run_activation_hook() {
    if ( class_exists( 'ZUTALW_Activator' ) ) {
        ZUTALW_Activator::activate();
    }
}

/* -------------------------------------------------------------------------
 * LOAD PLUGIN TEXT DOMAIN (MULTI-LANGUAGE SUPPORT)
 * ------------------------------------------------------------------------- */
function zutalw_load_textdomain() {
    load_plugin_textdomain(
        'zuta-lucky-wheel', 
        false,
        dirname( plugin_basename( __FILE__ ) ) . '/languages'
    );
}

add_action( 'plugins_loaded', 'zutalw_load_textdomain', 1 );

/* -------------------------------------------------------------------------
 * REQUIRE INCLUDES
 * ------------------------------------------------------------------------- */

require_once ZUTALW_DIR . 'includes/class-zutalw-core.php';
require_once ZUTALW_DIR . 'includes/class-zutalw-admin.php';
require_once ZUTALW_DIR . 'includes/class-zutalw-frontend.php';
require_once ZUTALW_DIR . 'includes/class-zutalw-ajax.php';
require_once ZUTALW_DIR . 'includes/class-zutalw-shortcode.php';
require_once ZUTALW_DIR . 'includes/models/model-zutalw-customer.php';
require_once ZUTALW_DIR . 'includes/models/model-zutalw-configgame.php'; 
require_once ZUTALW_DIR . 'includes/admin/class-zutalw-admin-customers.php';
require_once ZUTALW_DIR . 'includes/class-zutalw-updater.php';
require_once ZUTALW_DIR . 'includes/zutalw_check_folder_permissions.php';

/* -------------------------------------------------------------------------
 * INITIALIZE ADMIN FEATURES
 * ------------------------------------------------------------------------- */
function zutalw_init_admin_features() {
    if ( is_admin() ) {
        if ( class_exists( 'ZUTALW_Admin_Customers' ) ) {
            new ZUTALW_Admin_Customers(); 
        }
    }
}
add_action( 'plugins_loaded', 'zutalw_init_admin_features' );

/* -------------------------------------------------------------------------
 * INITIALIZE CORE INSTANCE
 * ------------------------------------------------------------------------- */
add_action( 'plugins_loaded', function() {
    if ( class_exists( 'ZUTALW_Core' ) ) {
        ZUTALW_Core::get_instance();
    }
    // Updater should be initialized after Core to ensure license data is available
    if ( class_exists( 'ZUTALW_Plugin_Updater' ) && is_admin() ) {
        new ZUTALW_Plugin_Updater();
    }
} );

// Tối ưu hóa: Chỉ chạy trên trang cập nhật hoặc trang cài đặt của Zuta
add_action( 'current_screen', function( $screen ) {
    $allowed_screens = array( 'update-core', 'plugins', 'toplevel_page_zuta-lucky-wheel' ); // Thay toplevel_... bằng slug thật trang settings của bạn
    
    if ( in_array( $screen->id, $allowed_screens ) ) {
        zutalw_check_folder_permissions();
    }
} );