<?php
// Exit if accessed directly.
if (! defined('ABSPATH')) {
    exit;
}

?>

<div class="integrazo-wz-setup-accordion" id="step3">
    <div class="integrazo-wz-setup-accordion-header">
        <span style="display:flex;align-items:center;gap:9px;">
            <?php if ($integrazo_wz_current_step > 3) : ?>
                <span style="display:inline-flex;align-items:center;justify-content:center;width:20px;height:20px;border-radius:50%;background:#16a34a;color:#fff;font-size:12px;font-weight:700;">3</span>
            <?php elseif ($integrazo_wz_current_step === 3) : ?>
                <span style="display:inline-flex;align-items:center;justify-content:center;width:20px;height:20px;border-radius:50%;background:#2271b1;color:#fff;font-size:12px;font-weight:700;">3</span>
            <?php else : ?>
                <span style="display:inline-flex;align-items:center;justify-content:center;width:20px;height:20px;border-radius:50%;background:#e0e0e0;color:#888;font-size:12px;font-weight:700;">3</span>
            <?php endif; ?>
            <?php esc_html_e('Map Fields', 'integrazo-integration-for-zoho-apps'); ?>
            <?php if ($integrazo_wz_current_step > 3) : ?>
                <span style="color:#16a34a;font-size:14px;font-weight:700;">&#10003;</span>
            <?php endif; ?>
        </span>
        <span style="display:flex;align-items:center;gap:6px;">
            <span class="integrazo-wz-setup-toggle-hint"><?php echo ($integrazo_wz_open_step === 3) ? esc_html__('Click to close', 'integrazo-integration-for-zoho-apps') : esc_html__('Click to expand', 'integrazo-integration-for-zoho-apps'); ?></span>
            <span class="integrazo-wz-setup-toggle-icon" title="<?php echo ($integrazo_wz_open_step === 3) ? esc_attr__('Click to close', 'integrazo-integration-for-zoho-apps') : esc_attr__('Click to expand', 'integrazo-integration-for-zoho-apps'); ?>"><?php echo ($integrazo_wz_open_step === 3) ? '−' : '+'; ?></span>
        </span>
    </div>

    <div class="integrazo-wz-setup-accordion-content">
        <!-- ✅ Header Section -->
        <div id="integrazo-wz-header-section">
            <?php
            // Block field mapping until Step 2 (Sign In / Authenticate) is actually
            // complete — don't just infer it from "something was picked".
            if (empty($integrazo_wz_account_id)) {
                echo '<p style="text-align: center;">' . esc_html__('Please complete Step 2 (Sign In / Authenticate) before mapping fields.', 'integrazo-integration-for-zoho-apps') . '</p>';
                return;
            }

            global $integrazo_wz_integration_sources;
            // Sanitize the product name.
            $integrazo_wz_safe_product = sanitize_key($integrazo_wz_to_product);
            $integrazo_wz_safe_plugin = sanitize_key($integrazo_wz_from_plugin);
            $integrazo_wz_pro_product_ids = integrazo_wz_get_pro_product_ids($integrazo_wz_integration_sources['products']);
            $integrazo_wz_pro_plugin_ids = integrazo_wz_get_pro_plugin_ids($integrazo_wz_integration_sources['plugins']);
           if ( ! $session ) {
                if ( in_array( $integrazo_wz_safe_product, $integrazo_wz_pro_product_ids, true ) || in_array( $integrazo_wz_safe_plugin, $integrazo_wz_pro_plugin_ids, true ) ) {
                    // Pro product → load from Pro plugin directory
                    echo '<p style="text-align: center;">' . esc_html__( 'This integration is available only in the Pro version. Please make sure you have activated the Pro plugin and a valid license.', 'integrazo-integration-for-zoho-apps' ) . '</p>';
                    return;
                }
            }

            if (defined('INTEGRAZO_WZ_PRO_PLUGIN_DIR') && in_array($integrazo_wz_safe_product, $integrazo_wz_pro_product_ids, true)) {
                // Pro product → load from Pro plugin dir
                $integrazo_wz_page_path = trailingslashit(INTEGRAZO_WZ_PRO_PLUGIN_DIR) . 'src/products/' . $integrazo_wz_safe_product . '/page.php';
            } else {
                // Free/default → load from Free plugin dir
                $integrazo_wz_page_path = trailingslashit(INTEGRAZO_WZ_PLUGIN_DIR) . 'src/products/' . $integrazo_wz_safe_product . '/page.php';
            }

            // Include the file if it exists.
            if (file_exists($integrazo_wz_page_path)) {
                $integrazo_wz_event =  INTEGRAZO_WZ_Plugin_Input_Renderer::render($integrazo_wz_integration_details);

                if (is_wp_error($integrazo_wz_event)) {
                    echo '<p style="text-align: center;">' . esc_html($integrazo_wz_event->get_error_message()) . '</p>';
                    return;
                }
                if ($integrazo_wz_event === null) {
                    echo '<p style="text-align: center;">' . esc_html__('The selected plugin is not supported in the Free version. Please upgrade to Pro to use this integration.', 'integrazo-integration-for-zoho-apps') . '</p>';
                    return;
                }
                // Load additional fields
                require_once INTEGRAZO_WZ_PLUGIN_DIR . 'src/util/additional-fields.php';

                // Decode integration data
                $integrazo_wz_integration_data    = json_decode($integrazo_wz_integration_details['integration_data'] ?? '', true);
                require_once $integrazo_wz_page_path;
            } else if (in_array($integrazo_wz_safe_product, $integrazo_wz_pro_product_ids, true)) {
                echo '<p style="text-align: center;">' . esc_html__('The selected service is not supported in the Free version. Please upgrade to Pro to use this integration.', 'integrazo-integration-for-zoho-apps') . '</p>';
                return;
            } else {
                echo '<p>' . esc_html__('Please complete Step 1 (Connect Service) and Step 2 (Sign In / Authenticate) before mapping fields.', 'integrazo-integration-for-zoho-apps') . '</p>';
            }
            ?>
        </div>
    </div>
</div>