/**
 * WooCommerce Equipment Rental - Product Admin Script
 *
 * Runs on the product edit page (post-type=product).
 * Depends on: wp-admin, woocommerce product data tabs.
 * No jQuery dependency in logic; jQuery is available in WP admin and used
 * only where WooCommerce already expects it for tab switching.
 */
(function ($) {
    'use strict';

    if (typeof $ === 'undefined') {
        return;
    }

    if ( typeof wcRentAdmin === 'undefined' ) {
        return;
    }

    // -------------------------------------------------------------------------
    // Selectors
    // -------------------------------------------------------------------------
    var ENABLE_CHECKBOX   = '#_rental_enabled';
    var RENTAL_FIELDS     = '.wcrent-fields';
    var PRICE_PER_DAY     = '#_rental_price_day';
    var RENTAL_TAB_LI     = 'li.wcrent_rental_tab';

    // -------------------------------------------------------------------------
    // Toggle visibility of rental-specific fields
    // -------------------------------------------------------------------------
    function toggleRentalFields() {
        var enabled = $(ENABLE_CHECKBOX).is(':checked');

        if (enabled) {
            $(RENTAL_FIELDS).slideDown(150);
        } else {
            $(RENTAL_FIELDS).slideUp(150);
        }
    }

    // -------------------------------------------------------------------------
    // Validate before save (publish / update)
    // -------------------------------------------------------------------------
    function showAdminError( msg ) {
        var panel = document.getElementById('wcrent_rental_product_data');
        if ( ! panel ) { return; }
        var existing = panel.querySelector('.wcrent-admin-validation-error');
        if ( existing ) { existing.remove(); }
        var notice = document.createElement('div');
        notice.className = 'notice notice-error wcrent-admin-notice wcrent-admin-validation-error';
        notice.setAttribute('role', 'alert');
        var p = document.createElement('p');
        p.textContent = msg;
        notice.appendChild(p);
        panel.insertBefore(notice, panel.firstChild);
    }

    function validateOnSave(e) {
        var enabled = $(ENABLE_CHECKBOX).is(':checked');
        if (!enabled) {
            return true;
        }

        var pricePerDay  = parseFloat($('#_rental_price_day').val())  || 0;
        var pricePerHour = parseFloat($('#_rental_price_hour').val()) || 0;
        var pricePerWeek = parseFloat($('#_rental_price_week').val()) || 0;

        if (pricePerDay <= 0 && pricePerHour <= 0 && pricePerWeek <= 0) {
            e.preventDefault();
            showAdminError( wcRentAdmin.i18n.priceRequired );
            $(RENTAL_TAB_LI).find('a').trigger('click');
            $(PRICE_PER_DAY).focus();
            return false;
        }

        return true;
    }

    // -------------------------------------------------------------------------
    // Init
    // -------------------------------------------------------------------------
    function init() {
        // Initial state
        toggleRentalFields();

        // Toggle on checkbox change
        $(document).on('change', ENABLE_CHECKBOX, function () {
            toggleRentalFields();
        });

        // Validate on publish / update.
        // WooCommerce product data tabs only render in the classic product editor
        // where #post is always present. The else branch is a defensive fallback
        // for non-standard configurations where the tabs appear outside that context.
        if ($('#post').length) {
            $('#post').on('submit', validateOnSave);
        } else {
            $('#publishing-action #publish, #save-action #save-post').on('click', validateOnSave);
        }
    }

    $(init);

}(window.jQuery));
