<?php
/**
 * Pustaka Theme Functions (Optimized & Safe)
 */

/* ===============================
   THEME CONSTANTS (Safe Definitions)
================================= */
if ( ! defined('PUSTAKA_THEME_DIR') ) {
    define( 'PUSTAKA_THEME_DIR', get_template_directory() );
}
if ( ! defined('PUSTAKA_THEME_URI') ) {
    define( 'PUSTAKA_THEME_URI', get_template_directory_uri() );
}
if ( ! defined('PUSTAKA_THEME_APP_DIR') ) {
    define( 'PUSTAKA_THEME_APP_DIR', PUSTAKA_THEME_DIR . '/app' );
}
if ( ! defined('PUSTAKA_THEME_APP_URI') ) {
    define( 'PUSTAKA_THEME_APP_URI', PUSTAKA_THEME_URI . '/app' );
}
if ( ! defined('PUSTAKA_THEME_CORE_DIR') ) {
    define( 'PUSTAKA_THEME_CORE_DIR', PUSTAKA_THEME_DIR . '/bootstrap/core' );
}
if ( ! defined('PUSTAKA_THEME_CORE_URI') ) {
    define( 'PUSTAKA_THEME_CORE_URI', PUSTAKA_THEME_URI . '/bootstrap/core' );
}
if ( ! defined('PUSTAKA_THEME_ASSETS_DIR') ) {
    define( 'PUSTAKA_THEME_ASSETS_DIR', PUSTAKA_THEME_URI . '/assets' );
}
if ( ! defined('PUSTAKA_THEME_ASSETS_URI') ) {
    define( 'PUSTAKA_THEME_ASSETS_URI', PUSTAKA_THEME_URI . '/assets' );
}
if ( ! defined('PUSTAKA_THEME_VERSION') ) {
    define( 'PUSTAKA_THEME_VERSION', '3.0.0' );
}
if ( ! defined('PUSTAKA_OPTIMIZE_MODE') ) {
    define( 'PUSTAKA_OPTIMIZE_MODE', true );
}

/* ===============================
   REQUIRED FILES
================================= */
require_once( PUSTAKA_THEME_DIR . '/bootstrap/class-tgm-plugin-activation.php' );
require_once( PUSTAKA_THEME_DIR . '/bootstrap/plugins.php' );
require_once( PUSTAKA_THEME_DIR . '/bootstrap/class-autoloaders.php' );
require_once( PUSTAKA_THEME_DIR . '/bootstrap/setup.php' );
require_once( PUSTAKA_THEME_DIR . '/importer/config.php' );
require_once( PUSTAKA_THEME_DIR . '/importer/after-import.php' );

/* ===============================
   REMOVE DEFAULT ADD TO CART BUTTONS
================================= */
remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10);
add_filter('woocommerce_product_single_add_to_cart_text', '__return_empty_string'); // remove text

/* ===============================
   SHOP PAGE BUY NOW BUTTON
================================= */
function add_buy_now_button_shop() {
    global $product;
    if ( ! $product ) return;

    $product_id = $product->get_id();
    $checkout_url = wc_get_checkout_url() . '?add-to-cart=' . $product_id;

    echo '<a href="' . esc_url($checkout_url) . '" class="buy-now-button">
        Buy Now
    </a>';
}
add_action('woocommerce_after_shop_loop_item', 'add_buy_now_button_shop', 15);

/* ===============================
   SINGLE PRODUCT CUSTOM BUTTONS
================================= */
function custom_product_action_buttons() {
    global $product;
    if ( ! $product ) return;

    $product_id = $product->get_id();
    $checkout_url = wc_get_checkout_url() . '?add-to-cart=' . $product_id;
    $cart_url = '?add-to-cart=' . $product_id;

$price = $product->get_price();
$currency = get_woocommerce_currency_symbol();


$whatsapp = "https://wa.me/9203425548690?text=" . urlencode(
"Assalam o Alaikum 👋\n\n" .

"I want to order this product.\n\n" .

"🛍 Product Details:\n" .
"Product: " . get_the_title() . "\n" .
"Price: " . $currency . " " . $price . "\n\n" .

"📍 Please provide your complete delivery address below:\n" .
"Name:{Enter your name here }\n" .
"City:{Enter your city here }\n" .
"Full Address:{Enter your addres here }\n" .
"Phone Number:{Enter your Phone number here }\n\n" .

"💳 Prepaid Payment Details:\n" .
"Bank: Dubai Bank Islami\n" .
"Account Title:Emel Publications \n" .
"Account Number:PK79DUIB0000000054120002 \n\n" .

"After payment, kindly send the payment screenshot or transaction reference for order confirmation.\n\n" .

"Thank you for choosing Emel Books"
);



    echo '<div class="custom-product-buttons">';

    // Add to cart
    echo '<a href="'.$cart_url.'" class="product-action-btn add-cart-btn">
            Add to cart
          </a>';

    // Buy now
    echo '<a href="'.$checkout_url.'" class="product-action-btn buy-now-btn">
            Buy it now
          </a>';

    // WhatsApp
    echo '<a href="'.$whatsapp.'" target="_blank" class="product-action-btn whatsapp-order-btn">
            Order On WhatsApp
          </a>';

    echo '</div>';
}

remove_action('woocommerce_single_product_summary', 'custom_buy_now_button_single', 30);
remove_action('woocommerce_single_product_summary', 'add_whatsapp_next_to_socials', 35);

add_action('woocommerce_single_product_summary', 'custom_product_action_buttons', 30);
/* ===============================
   WHATSAPP SHARE BUTTON
================================= */
function add_whatsapp_next_to_socials() {
    if ( is_product() ) {
        $url = urlencode(get_permalink());
        $title = urlencode(get_the_title());

        echo '<div class="custom-whatsapp-inline">
            <a class="social-btn whatsapp-btn" href="https://wa.me/?text='.$title.' '.$url.'" target="_blank">
               <i class="fab fa-whatsapp"></i> WhatsApp
            </a>
        </div>';
    }
}
add_action('woocommerce_single_product_summary', 'add_whatsapp_next_to_socials', 35);

/* ===============================
   REDIRECT CART TO CHECKOUT
================================= */
add_filter('woocommerce_add_to_cart_redirect', function() {
    return wc_get_checkout_url();
});

/* ===============================
   MINIMAL CHECKOUT FIELDS
================================= */
function custom_minimal_checkout_fields( $fields ) {
    $fields['billing'] = array(
        'billing_first_name' => array(
            'label'    => 'First Name',
            'required' => true,
            'class'    => array('form-row-first'),
        ),
        'billing_last_name' => array(
            'label'    => 'Last Name',
            'required' => true,
            'class'    => array('form-row-last'),
        ),
        'billing_email' => array(
            'label'    => 'Email',
            'required' => true,
            'class'    => array('form-row-wide'),
        ),
        'billing_phone' => array(
            'label'    => 'Phone',
            'required' => true,
            'class'    => array('form-row-wide'),
        ),
        'billing_city' => array(
            'label'    => 'City',
            'required' => true,
            'class'    => array('form-row-wide'),
        ),
        'billing_address_1' => array(
            'label'    => 'Full Address',
            'required' => true,
            'class'    => array('form-row-wide'),
        ),
    );
    $fields['shipping'] = array(); // remove shipping fields
    return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'custom_minimal_checkout_fields' );

/* ===============================
   REMOVE PASSWORD STRENGTH METER
================================= */
add_action( 'wp_enqueue_scripts', function () {
    if ( ! is_admin() ) {
        wp_dequeue_script( 'zxcvbn' );
        wp_deregister_script( 'zxcvbn' );
    }
}, 100 );

/* ===============================
   PAYMENT ICONS ON CHECKOUT
================================= */
function add_payment_icons() {
    ?>
    <div class="checkout-payment-icons" style="margin-top: 10px;">
        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/img/jazzcash.png" alt="JazzCash" style="height:40px; margin-right:10px;">
        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/img/easypaisa.png" alt="Easypaisa" style="height:40px; margin-right:10px;">
        <img src="<?php echo esc_url(get_template_directory_uri()); ?>/assets/img/visa.png" alt="Visa" style="height:40px; margin-right:10px;">
    </div>
    <?php
}
add_action( 'woocommerce_review_order_after_payment', 'add_payment_icons' );