/*
Theme Name: Flatsome Child
Template: flatsome
Version: 1.0.0
*/

<?php
/**
 * Prevent quantity increase for personalized letter products in cart
 * Category slug: gramma-agio-vasili
 */

// Set maximum quantity to 1 for products in specific category
add_filter('woocommerce_cart_item_quantity', 'limit_personalized_letter_quantity', 10, 3);
function limit_personalized_letter_quantity($product_quantity, $cart_item_key, $cart_item) {
    // Check if product belongs to 'gramma-agio-vasili' category
    if (has_term('gramma-agio-vasili', 'product_cat', $cart_item['product_id'])) {
        // Replace quantity input with modified one (max=1)
        $product_quantity = sprintf(
            '<div class="ux-quantity quantity buttons_added">
                <input type="button" value="-" class="ux-quantity__button ux-quantity__button--minus button minus is-form" aria-label="%s">
                <label class="screen-reader-text" for="quantity_%s">%s</label>
                <input type="number" id="quantity_%s" class="input-text qty text" name="cart[%s][qty]" value="%s" aria-label="%s" min="0" max="1" step="1" placeholder="" inputmode="numeric" autocomplete="off">
                <input type="button" value="+" class="ux-quantity__button ux-quantity__button--plus button plus is-form personalized-letter-plus-disabled" aria-label="%s" data-letter-product="true">
            </div>',
            __('Μειώστε την ποσότητα', 'woocommerce'),
            esc_attr($cart_item_key),
            esc_html($cart_item['data']->get_name()) . ' ' . __('ποσότητα', 'woocommerce'),
            esc_attr($cart_item_key),
            esc_attr($cart_item_key),
            esc_attr($cart_item['quantity']),
            __('Ποσότητα προϊόντος', 'woocommerce'),
            __('Αυξήστε την ποσότητα', 'woocommerce')
        );
    }
    return $product_quantity;
}

// Add custom CSS to gray out the plus button
add_action('wp_head', 'personalized_letter_cart_styles');
function personalized_letter_cart_styles() {
    if (is_cart()) {
        ?>
        <style>
            .personalized-letter-plus-disabled {
                opacity: 0.4 !important;
                cursor: not-allowed !important;
                pointer-events: none;
            }
            
            /* Toast notification styling */
            .personalized-letter-notice {
                position: fixed;
                top: 50%;
                left: 50%;
                transform: translate(-50%, -50%);
                background: #fff;
                border: 2px solid #e74c3c;
                border-radius: 8px;
                padding: 20px 30px;
                box-shadow: 0 4px 20px rgba(0,0,0,0.2);
                z-index: 99999;
                text-align: center;
                max-width: 400px;
                animation: fadeIn 0.3s ease-in-out;
            }
            
            .personalized-letter-notice p {
                margin: 0;
                font-size: 16px;
                color: #333;
                line-height: 1.5;
            }
            
            .personalized-letter-notice strong {
                color: #e74c3c;
                display: block;
                margin-bottom: 8px;
                font-size: 18px;
            }
            
            @keyframes fadeIn {
                from {
                    opacity: 0;
                    transform: translate(-50%, -60%);
                }
                to {
                    opacity: 1;
                    transform: translate(-50%, -50%);
                }
            }
            
            .personalized-letter-overlay {
                position: fixed;
                top: 0;
                left: 0;
                width: 100%;
                height: 100%;
                background: rgba(0,0,0,0.5);
                z-index: 99998;
                animation: fadeInOverlay 0.3s ease-in-out;
            }
            
            @keyframes fadeInOverlay {
                from { opacity: 0; }
                to { opacity: 1; }
            }
        </style>
        <?php
    }
}

// Add JavaScript to handle click events and show message
add_action('wp_footer', 'personalized_letter_cart_script');
function personalized_letter_cart_script() {
    if (is_cart()) {
        ?>
        <script>
        jQuery(document).ready(function($) {
            // Add click handler to the parent div to catch clicks even with pointer-events:none
            $('.woocommerce-cart-form').on('click', '.ux-quantity__button--plus', function(e) {
                if ($(this).hasClass('personalized-letter-plus-disabled')) {
                    e.preventDefault();
                    e.stopPropagation();
                    showPersonalizedLetterNotice();
                    return false;
                }
            });
            
            // Also prevent any manual input changes above 1
            $(document).on('change', '.ux-quantity input[type="number"]', function() {
                var $input = $(this);
                var $plusBtn = $input.siblings('.ux-quantity__button--plus');
                
                if ($plusBtn.data('letter-product') && parseInt($input.val()) > 1) {
                    $input.val(1);
                    showPersonalizedLetterNotice();
                }
            });
            
            function showPersonalizedLetterNotice() {
                // Remove existing notices
                $('.personalized-letter-notice, .personalized-letter-overlay').remove();
                
                // Create overlay
                var $overlay = $('<div class="personalized-letter-overlay"></div>');
                
                // Create notice
                var $notice = $('<div class="personalized-letter-notice">' +
                    '<p><strong>Προσωποποιημένα Γράμματα</strong>' +
                    'Τα γράμματα είναι προσωποποιημένα και μπορείτε να παραγγείλετε μόνο 1 γράμμα ανά παιδί.</p>' +
                '</div>');
                
                $('body').append($overlay).append($notice);
                
                // Remove after 3 seconds
                setTimeout(function() {
                    $overlay.fadeOut(300, function() { $(this).remove(); });
                    $notice.fadeOut(300, function() { $(this).remove(); });
                }, 3000);
                
                // Also remove on click
                $overlay.on('click', function() {
                    $overlay.fadeOut(300, function() { $(this).remove(); });
                    $notice.fadeOut(300, function() { $(this).remove(); });
                });
            }
        });
        </script>
        <?php
    }
}

// Ensure quantity never exceeds 1 when updating cart
add_filter('woocommerce_cart_item_quantity', 'enforce_max_quantity_on_update', 20, 3);
function enforce_max_quantity_on_update($product_quantity, $cart_item_key, $cart_item) {
    if (has_term('gramma-agio-vasili', 'product_cat', $cart_item['product_id'])) {
        if ($cart_item['quantity'] > 1) {
            WC()->cart->set_quantity($cart_item_key, 1);
        }
    }
    return $product_quantity;
}

// Validate cart quantities before checkout
add_action('woocommerce_check_cart_items', 'validate_personalized_letter_quantity');
function validate_personalized_letter_quantity() {
    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
        if (has_term('gramma-agio-vasili', 'product_cat', $cart_item['product_id'])) {
            if ($cart_item['quantity'] > 1) {
                WC()->cart->set_quantity($cart_item_key, 1);
                wc_add_notice(
                    __('Τα προσωποποιημένα γράμματα μπορούν να παραγγελθούν μόνο 1 ανά παιδί. Η ποσότητα ενημερώθηκε αυτόματα.', 'woocommerce'),
                    'notice'
                );
            }
        }
    }
}