<?php
// If this file is called directly, abort.
if (! defined('ABSPATH')) {
    exit;
}

/**
 * Main render entry point for the Product Details Link widget.
 *
 * @param \GGM_Commerce_Base_Widget $widget The widget instance.
 */
function ggmc_product_details_link_render( $widget ) {
    $settings = $widget->get_settings_for_display();

    // Get product data (includes preview product support)
    $product_data = $widget->get_product_data();

    // Check if we're in an editor context (Elementor or Gutenberg)
    $is_editor = ggmc_is_editor_mode();

    // Build the product details URL
    $product_url = '#';
    $product_name = '';

    if ($product_data) {
        $product_name = $product_data->name ?? '';

        // Get the product details page URL
        $product_details_page_id = get_option('ggm_commerce_product_details_page_id');

        if ($product_details_page_id) {
            $base_url = get_permalink($product_details_page_id);
            if ($base_url) {
                // Use slug if available, otherwise use ID
                if (!empty($product_data->slug)) {
                    $product_url = trailingslashit($base_url) . sanitize_title($product_data->slug) . '/';
                } else {
                    $product_url = add_query_arg('id', $product_data->id, $base_url);
                }
            }
        }
    }

    // Link text
    $link_text = !empty($settings['link_text']) ? $settings['link_text'] : __('View Full Details →', 'ggm-commerce');

    // CSS classes
    $link_classes = ['ggmc-product-details-link'];

    switch ($settings['link_style']) {
        case 'button':
            $link_classes[] = 'ggmc-link-button';
            break;
        case 'outline':
            $link_classes[] = 'ggmc-link-outline';
            break;
        default:
            $link_classes[] = 'ggmc-link-text';
            break;
    }

    // Add close popup class if enabled
    if ($settings['close_popup'] === 'yes') {
        $link_classes[] = 'ggmc-close-quick-view';
    }

    // Render wrapper
    echo '<div class="ggmc-product-details-link-wrapper">';

    if ($product_data && $product_url !== '#') {
        /* translators: %s: product name */
        $title_attr = sprintf(__('View full details for %s', 'ggm-commerce'), $product_name);
        printf(
            '<a href="%s" class="%s" title="%s">%s</a>',
            esc_url($product_url),
            esc_attr(implode(' ', $link_classes)),
            esc_attr($title_attr),
            esc_html($link_text)
        );
    } elseif ($is_editor) {
        // Show placeholder in editor
        printf(
            '<a href="#" class="%s" style="cursor: pointer;">%s</a>',
            esc_attr(implode(' ', $link_classes)),
            esc_html($link_text)
        );
        echo '<p style="font-size: 11px; color: #64748b; margin-top: 8px; font-style: italic;">';
        echo esc_html__('Link will point to product details page on frontend', 'ggm-commerce');
        echo '</p>';
    }

    echo '</div>';
}

