<?php
/**
 * WooCommerce product editor fields.
 *
 * @package EyewearVirtualTryOn
 */

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

/**
 * Product data fields — Tryonixs Virtual Try-On panel.
 *
 * @return void
 */
function eyewvitr_product_options() {
    if (!function_exists('woocommerce_wp_checkbox') || !function_exists('woocommerce_wp_text_input')) {
        return;
    }

    global $post;

    if (!$post || empty($post->ID)) {
        return;
    }

    echo '<div class="options_group eyewvitr-product-panel">';
    echo '<p class="form-field"><strong>' . esc_html__('Tryonixs Virtual Try-On', 'tryonixs-virtual-try-on-for-eyewear-stores') . '</strong></p>';

    $enabled = get_post_meta($post->ID, '_eyewvitr_enabled', true);
    if ($enabled === '') {
        $enabled = 'yes';
    }

    woocommerce_wp_checkbox([
        'id'          => '_eyewvitr_enabled',
        'label'       => __('Enable Virtual Try-On', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'description' => __('Show the virtual try-on button on this product.', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'value'       => $enabled,
    ]);

    woocommerce_wp_text_input([
        'id'          => '_eyewvitr_product_id',
        'label'       => __('Tryonixs Product ID', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'description' => __('Optional. Must match the product ID in your Tryonixs account. Defaults to SKU or wc_{id}.', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'desc_tip'    => true,
        'placeholder' => 'frame_001',
    ]);

    woocommerce_wp_text_input([
        'id'          => '_eyewvitr_image_url',
        'label'       => __('Transparent Frame PNG URL', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'description' => __('Optional. Transparent PNG frame URL for AR. Defaults to the product featured image.', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'desc_tip'    => true,
        'placeholder' => 'https://example.com/frames/product.png',
    ]);

    woocommerce_wp_text_input([
        'id'                => '_eyewvitr_frame_width',
        'label'             => __('Frame Width (mm)', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'description'       => __('Optional frame width in millimeters.', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'desc_tip'          => true,
        'type'              => 'number',
        'custom_attributes' => [
            'step' => '0.1',
            'min'  => '0',
        ],
    ]);

    woocommerce_wp_text_input([
        'id'                => '_eyewvitr_lens_height',
        'label'             => __('Lens Height (mm)', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'description'       => __('Optional lens height in millimeters.', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'desc_tip'          => true,
        'type'              => 'number',
        'custom_attributes' => [
            'step' => '0.1',
            'min'  => '0',
        ],
    ]);

    woocommerce_wp_text_input([
        'id'                => '_eyewvitr_temple_length',
        'label'             => __('Temple Length (mm)', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'description'       => __('Optional temple length in millimeters.', 'tryonixs-virtual-try-on-for-eyewear-stores'),
        'desc_tip'          => true,
        'type'              => 'number',
        'custom_attributes' => [
            'step' => '0.1',
            'min'  => '0',
        ],
    ]);

    echo '</div>';
}

/**
 * Save product meta with nonce verification.
 *
 * @param int $post_id Post ID.
 * @return void
 */
function eyewvitr_save_product_meta($post_id) {
    if (!eyewvitr_is_woocommerce_active()) {
        return;
    }

    if (!isset($_POST['woocommerce_meta_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['woocommerce_meta_nonce'])), 'woocommerce_save_data')) {
        return;
    }

    if (!current_user_can('edit_post', $post_id)) {
        return;
    }

    $enabled = isset($_POST['_eyewvitr_enabled']) ? 'yes' : 'no';
    update_post_meta($post_id, '_eyewvitr_enabled', $enabled);

    if (isset($_POST['_eyewvitr_product_id'])) {
        update_post_meta(
            $post_id,
            '_eyewvitr_product_id',
            sanitize_text_field(wp_unslash($_POST['_eyewvitr_product_id']))
        );
    }

    if (isset($_POST['_eyewvitr_image_url'])) {
        update_post_meta(
            $post_id,
            '_eyewvitr_image_url',
            esc_url_raw(wp_unslash($_POST['_eyewvitr_image_url']))
        );
    }

    $numeric_fields = [
        '_eyewvitr_frame_width',
        '_eyewvitr_lens_height',
        '_eyewvitr_temple_length',
    ];

    foreach ($numeric_fields as $field) {
        if (!isset($_POST[$field])) {
            continue;
        }

        $raw = sanitize_text_field(wp_unslash($_POST[$field]));

        if ($raw === '') {
            delete_post_meta($post_id, $field);
            continue;
        }

        update_post_meta($post_id, $field, (string) floatval($raw));
    }
}
