<?php
if (!defined('ABSPATH')) {
    exit;
}

$iapc_is_customizable = iapc_product_is_customizable($product->get_id());
$iapc_setup_gaps      = $iapc_is_customizable ? array() : iapc_get_product_customizer_setup_gaps($product->get_id());
$iapc_can_manage      = current_user_can('manage_woocommerce') || current_user_can('edit_product', $product->get_id());
?>
<div class="iapc-designer-wrapper<?php echo $iapc_is_customizable ? '' : ' iapc-designer-wrapper--unavailable'; ?>">

<?php if (!$iapc_is_customizable) : ?>
    <div class="iapc-designer-unavailable" role="status">
        <h2 class="iapc-designer-unavailable__title"><?php echo esc_html($product_name); ?></h2>
        <p class="iapc-designer-unavailable__lead">
            <?php esc_html_e('This product is not ready for customization yet.', 'idesoftbcn-ai-product-customizer'); ?>
        </p>
        <?php if ($iapc_can_manage && !empty($iapc_setup_gaps)) : ?>
            <div class="iapc-designer-unavailable__setup">
                <p class="iapc-designer-unavailable__setup-title">
                    <strong><?php esc_html_e('Required setup', 'idesoftbcn-ai-product-customizer'); ?></strong>
                </p>
                <ul class="iapc-designer-unavailable__list">
                    <?php foreach ($iapc_setup_gaps as $iapc_gap) : ?>
                        <li><?php echo esc_html($iapc_gap); ?></li>
                    <?php endforeach; ?>
                </ul>
                <p class="iapc-designer-unavailable__hint">
                    <?php esc_html_e('Complete these items in the product editor (Customizer tab), then reload this page.', 'idesoftbcn-ai-product-customizer'); ?>
                </p>
            </div>
        <?php else : ?>
            <p class="iapc-designer-unavailable__hint">
                <?php esc_html_e('Please check back later or contact the store if you need help.', 'idesoftbcn-ai-product-customizer'); ?>
            </p>
        <?php endif; ?>
    </div>
<?php else : ?>
    <div class="iapc-designer-layout">

    <div class="iapc-designer-left">

<div class="iapc-canva-menu">

<button type="button"
        class="iapc-canva-tab iapc-canva-tab-ai<?php echo $ai_premium_active ? ' iapc-active' : ''; ?>"
        data-panel="ai"
        <?php echo $ai_premium_active ? '' : ' disabled="disabled" aria-disabled="true"'; ?>>
    <?php esc_html_e('AI Images', 'idesoftbcn-ai-product-customizer'); ?>
</button>

<button type="button"
        class="iapc-canva-tab <?php echo $ai_premium_active ? '' : 'iapc-active'; ?>"
        data-panel="text">
    <?php esc_html_e('Text', 'idesoftbcn-ai-product-customizer'); ?>
</button>
<button type="button" class="iapc-canva-tab" data-panel="paint"><?php esc_html_e('Paint', 'idesoftbcn-ai-product-customizer'); ?></button>
<button type="button" class="iapc-canva-tab" data-panel="image"><?php esc_html_e('Image', 'idesoftbcn-ai-product-customizer'); ?></button>
<button type="button" class="iapc-canva-tab" data-panel="sticker"><?php esc_html_e('Sticker', 'idesoftbcn-ai-product-customizer'); ?></button>
<button type="button" class="iapc-canva-tab" data-panel="graphic"><?php esc_html_e('Elements', 'idesoftbcn-ai-product-customizer'); ?></button>
</div>

<div class="iapc-canva-content">
    <?php iapc_render_customizer(); ?>
</div>

</div>

<div class="iapc-designer-center">
    <h2 class="iapc-designer-title"><?php echo esc_html($product_name); ?></h2>

    <?php
    // Sanitized once when the full designer markup is returned (shortcode / product page).
    echo iapc_shortcode_canvas(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    ?>

    <div class="iapc-sides-card">
        <div class="iapc-designer-sides">
            <?php echo wp_kses_post(iapc_shortcode_sides()); ?>
        </div>
    </div>
</div>
<div class="iapc-designer-right">

    <div class="iapc-right-card iapc-actions-card">
        <?php echo wp_kses_post(iapc_shortcode_actions()); ?>
    </div>

    <?php
    $iapc_has_base_price = iapc_product_has_base_price($product);
    $iapc_precio_base    = $iapc_has_base_price ? (float) $product->get_price() : 0;
    ?>

    <div class="iapc-right-card iapc-price-card">

        <div
            class="iapc-product-price"
            data-base-price="<?php echo esc_attr($iapc_precio_base); ?>"
            <?php echo $iapc_has_base_price ? '' : ' data-no-base-price="1" style="display:none;"'; ?>
        >
            <?php if ($iapc_has_base_price) : ?>
                <?php echo wp_kses_post(wc_price($iapc_precio_base)); ?>
            <?php endif; ?>
        </div>

        <div class="iapc-add-to-cart-inside">
            <?php
            iapc_set_rendering_designer_cart(true);
            woocommerce_template_single_add_to_cart();
            iapc_set_rendering_designer_cart(false);
            ?>
        </div>

        <a href="<?php echo esc_url(wc_get_cart_url()); ?>" class="iapc-view-cart">
            <?php esc_html_e('View cart', 'idesoftbcn-ai-product-customizer'); ?>
        </a>

    </div>

</div>

</div>

<?php
$iapc_product_description_html = iapc_get_product_description_html($product);

if ($iapc_product_description_html !== '') :
?>
    <details class="iapc-product-description-panel" open>
        <summary class="iapc-product-description-summary">
            <span class="iapc-product-description-title"><?php esc_html_e('Product description', 'idesoftbcn-ai-product-customizer'); ?></span>
            <span class="iapc-product-description-toggle" aria-hidden="true"></span>
        </summary>
        <div class="iapc-product-description">
            <?php echo wp_kses_post($iapc_product_description_html); ?>
        </div>
    </details>
<?php endif; ?>

<?php if ($ai_premium_active) : ?>
    <?php iapc_render_ai_guided_modal(''); ?>
<?php endif; ?>

<?php iapc_preview_overlay(); ?>

<?php endif; ?>

</div>
