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

/**
 * @param array $settings
 * @return bool
 */
function elementsell_upsell_is_enabled( $settings ) {
    return is_array( $settings )
        && ! empty( $settings['upsell_enable'] )
        && $settings['upsell_enable'] === 'yes'
        && elementsell_get_upsell_price( $settings ) > 0;
}

/**
 * @param array $settings
 * @return float
 */
function elementsell_get_upsell_price( $settings ) {
    if ( ! is_array( $settings ) ) {
        return 0.0;
    }

    return max( 0.0, (float) ( $settings['upsell_price'] ?? 0 ) );
}

/**
 * @param array $settings
 * @return string
 */
function elementsell_get_upsell_product_label( $settings ) {
    if ( ! is_array( $settings ) ) {
        return '';
    }

    if ( ! empty( $settings['upsell_product_name'] ) ) {
        return trim( (string) $settings['upsell_product_name'] );
    }

    if ( ! empty( $settings['upsell_title'] ) ) {
        return trim( (string) $settings['upsell_title'] );
    }

    return __( 'Doplněk', 'platebni-formular' );
}

/**
 * @param array $settings
 * @return string
 */
function elementsell_get_upsell_role_product_name( $settings ) {
    return elementsell_get_upsell_product_label( $settings );
}

/**
 * @param array  $settings
 * @param string $discount_code
 * @param bool   $upsell_accepted
 * @return float
 */
function elementsell_calculate_payment_amount( $settings, $discount_code = '', $upsell_accepted = false ) {
    $amount = elementsell_calculate_widget_amount( $settings, $discount_code );

    if ( $amount <= 0 ) {
        return 0.0;
    }

    if ( $upsell_accepted && elementsell_upsell_is_enabled( $settings ) ) {
        $amount += elementsell_get_upsell_price( $settings );
    }

    return (float) round( $amount );
}

/**
 * @param int $order_id
 * @return bool
 */
function elementsell_order_upsell_was_accepted( $order_id ) {
    $order_id = (int) $order_id;
    $flag     = (string) elementsell_get_order_field( $order_id, 'Upsell' );

    if ( $flag === 'Ne' ) {
        return false;
    }

    if ( $flag === 'Ano' ) {
        return true;
    }

    if ( trim( (string) elementsell_get_order_field( $order_id, 'Upsell produkt' ) ) !== '' ) {
        return true;
    }

    return (int) elementsell_get_order_field( $order_id, 'Upsell částka' ) > 0;
}

/**
 * @param int         $order_id
 * @param array|null  $settings
 * @return string
 */
function elementsell_get_order_upsell_delivery( $order_id, $settings = null ) {
    $order_id = (int) $order_id;
    $delivery = sanitize_key( (string) elementsell_get_order_field( $order_id, 'Upsell doručení' ) );

    if ( in_array( $delivery, [ 'role', 'link', 'info' ], true ) ) {
        return $delivery;
    }

    if ( is_array( $settings ) ) {
        if ( function_exists( 'elementsell_merge_payment_widget_defaults' ) ) {
            $settings = elementsell_merge_payment_widget_defaults( $settings );
        }

        $widget_delivery = ! empty( $settings['upsell_delivery'] )
            ? sanitize_key( (string) $settings['upsell_delivery'] )
            : '';

        if ( in_array( $widget_delivery, [ 'role', 'link', 'info' ], true ) ) {
            return $widget_delivery;
        }
    }

    return '';
}

/**
 * @param object $order
 * @return array|null
 */
function elementsell_get_order_widget_settings( $order ) {
    if ( ! $order ) {
        return null;
    }

    $post_id   = (int) elementsell_get_order_field( (int) $order->id, 'elementsell_post_id' );
    $widget_id = (string) elementsell_get_order_field( (int) $order->id, 'elementsell_widget_id' );
    $settings  = elementsell_find_elementor_widget_settings( $post_id, $widget_id );

    if ( ! is_array( $settings ) ) {
        return null;
    }

    if ( function_exists( 'elementsell_merge_payment_widget_defaults' ) ) {
        $settings = elementsell_merge_payment_widget_defaults( $settings );
    }

    return $settings;
}

/**
 * @param int         $order_id
 * @param array|null  $settings
 * @return void
 */
function elementsell_save_upsell_from_post( $order_id, $settings = null ) {
    $order_id = (int) $order_id;

    if ( $order_id <= 0 ) {
        return;
    }

    if ( ! is_array( $settings ) ) {
        $post_id   = isset( $_POST['elementsell_post_id'] ) ? (int) $_POST['elementsell_post_id'] : 0;
        $widget_id = isset( $_POST['elementsell_widget_id'] )
            ? sanitize_text_field( wp_unslash( (string) $_POST['elementsell_widget_id'] ) )
            : '';

        $settings = elementsell_find_elementor_widget_settings( $post_id, $widget_id );
    }

    if ( is_array( $settings ) && function_exists( 'elementsell_merge_payment_widget_defaults' ) ) {
        $settings = elementsell_merge_payment_widget_defaults( $settings );
    }

    if ( ! is_array( $settings ) || ! elementsell_upsell_is_enabled( $settings ) ) {
        return;
    }

    $accepted = ! empty( $_POST['upsell_accepted'] ) && (string) $_POST['upsell_accepted'] === '1';

    elementsell_save_order_label_field( $order_id, 'Upsell', $accepted ? 'Ano' : 'Ne' );

    if ( ! $accepted ) {
        return;
    }

    $delivery = ! empty( $settings['upsell_delivery'] )
        ? sanitize_key( (string) $settings['upsell_delivery'] )
        : 'info';

    if ( ! in_array( $delivery, [ 'role', 'link', 'info' ], true ) ) {
        $delivery = 'info';
    }

    elementsell_save_order_label_field( $order_id, 'Upsell produkt', elementsell_get_upsell_product_label( $settings ) );
    elementsell_save_order_label_field( $order_id, 'Upsell částka', (string) (int) elementsell_get_upsell_price( $settings ) );
    elementsell_save_order_label_field( $order_id, 'Upsell doručení', $delivery );

    if ( $delivery === 'role' ) {
        elementsell_save_order_label_field( $order_id, 'Upsell role', elementsell_get_upsell_role_product_name( $settings ) );
    }

    if ( $delivery === 'link' ) {
        $link_url = '';

        if ( ! empty( $settings['upsell_link_url'] ) && is_array( $settings['upsell_link_url'] ) ) {
            $link_url = ! empty( $settings['upsell_link_url']['url'] )
                ? (string) $settings['upsell_link_url']['url']
                : '';
        } elseif ( ! empty( $settings['upsell_link_url'] ) ) {
            $link_url = (string) $settings['upsell_link_url'];
        }

        if ( $link_url !== '' ) {
            elementsell_save_order_label_field( $order_id, 'Upsell odkaz', esc_url_raw( $link_url ) );
            elementsell_save_order_label_field(
                $order_id,
                'Upsell tlačítko',
                ! empty( $settings['upsell_link_label'] )
                    ? sanitize_text_field( (string) $settings['upsell_link_label'] )
                    : __( 'Získat bonus', 'platebni-formular' )
            );
        }
    }

    if ( $delivery === 'info' ) {
        $info_text = ! empty( $settings['upsell_info_text'] )
            ? trim( (string) $settings['upsell_info_text'] )
            : trim( (string) ( $settings['upsell_description'] ?? '' ) );

        if ( $info_text !== '' ) {
            elementsell_save_order_label_field( $order_id, 'Upsell informace', sanitize_textarea_field( $info_text ) );
        }
    }
}

/**
 * @param object $order
 * @return array<int, string>
 */
function elementsell_get_order_role_product_names( $order ) {
    if ( ! $order ) {
        return [];
    }

    $order_id        = (int) $order->id;
    $names           = [];
    $main            = trim( (string) $order->product_name );
    $settings        = elementsell_get_order_widget_settings( $order );
    $upsell_accepted = elementsell_order_upsell_was_accepted( $order_id );
    $upsell_delivery = $upsell_accepted ? elementsell_get_order_upsell_delivery( $order_id, $settings ) : '';

    if ( $main !== '' ) {
        $names[] = $main;
    }

    if ( $upsell_accepted && $upsell_delivery === 'role' ) {
        $upsell_name = trim( (string) elementsell_get_order_field( $order_id, 'Upsell role' ) );

        if ( $upsell_name === '' ) {
            $upsell_name = trim( (string) elementsell_get_order_field( $order_id, 'Upsell produkt' ) );
        }

        if ( $upsell_name === '' && is_array( $settings ) ) {
            $upsell_name = elementsell_get_upsell_role_product_name( $settings );
        }

        if ( $upsell_name !== '' && ! in_array( $upsell_name, $names, true ) ) {
            $names[] = $upsell_name;
        }
    }

    if ( empty( $names ) && is_array( $settings ) ) {
        $widget_main = trim( (string) ( $settings['product_name'] ?? '' ) );

        if ( $widget_main !== '' ) {
            $names[] = $widget_main;
        }
    }

    return $names;
}

/**
 * @param object $order
 * @return array<int, string>
 */
function elementsell_get_order_role_keys( $order ) {
    $keys     = [];
    $settings = elementsell_get_order_widget_settings( $order );

    if ( is_array( $settings ) && function_exists( 'elementsell_get_course_roles_from_widget_settings' ) ) {
        foreach ( elementsell_get_course_roles_from_widget_settings( $settings ) as $role_key ) {
            $resolved = '';

            if ( function_exists( 'elementsell_validate_registered_role_key' ) ) {
                $resolved = elementsell_validate_registered_role_key( $role_key );
            }

            if ( $resolved === '' && function_exists( 'elementsell_ensure_product_role' ) ) {
                $resolved = (string) elementsell_ensure_product_role( $role_key );
            }

            if ( $resolved !== '' && ! in_array( $resolved, $keys, true ) ) {
                $keys[] = $resolved;
            }
        }
    }

    if ( empty( $keys ) && $order ) {
        $main_name = trim( (string) $order->product_name );

        if ( $main_name === '' && is_array( $settings ) ) {
            $main_name = trim( (string) ( $settings['product_name'] ?? '' ) );
        }

        if ( $main_name !== '' && function_exists( 'elementsell_ensure_product_role' ) ) {
            $role_key = elementsell_ensure_product_role( $main_name );

            if ( $role_key && ! in_array( $role_key, $keys, true ) ) {
                $keys[] = $role_key;
            }
        }
    }

    if (
        $order
        && elementsell_order_upsell_was_accepted( (int) $order->id )
        && function_exists( 'elementsell_get_order_upsell_delivery' )
        && elementsell_get_order_upsell_delivery( (int) $order->id, $settings ) === 'role'
    ) {
        foreach ( elementsell_get_order_upsell_role_keys( $order, $settings ) as $role_key ) {
            if ( $role_key !== '' && ! in_array( $role_key, $keys, true ) ) {
                $keys[] = $role_key;
            }
        }
    }

    return $keys;
}

/**
 * Role z upsellu — explicitní upsell_course_role, jinak název bonusu (vytvoří roli).
 *
 * @param object     $order
 * @param array|null $settings
 * @return array<int, string>
 */
function elementsell_get_order_upsell_role_keys( $order, $settings = null ) {
    if ( ! $order ) {
        return [];
    }

    $order_id = (int) $order->id;
    $keys     = [];

    if ( ! is_array( $settings ) ) {
        $settings = elementsell_get_order_widget_settings( $order );
    }

    if ( is_array( $settings ) && function_exists( 'elementsell_normalize_course_roles_value' ) ) {
        foreach ( elementsell_normalize_course_roles_value( $settings['upsell_course_role'] ?? '' ) as $role_key ) {
            $resolved = '';

            if ( function_exists( 'get_role' ) && get_role( $role_key ) ) {
                $resolved = $role_key;
            } elseif ( function_exists( 'elementsell_ensure_product_role' ) ) {
                $resolved = (string) elementsell_ensure_product_role( $role_key );
            }

            if ( $resolved !== '' && ! in_array( $resolved, $keys, true ) ) {
                $keys[] = $resolved;
            }
        }
    }

    if ( ! empty( $keys ) ) {
        return $keys;
    }

    $upsell_name = trim( (string) elementsell_get_order_field( $order_id, 'Upsell role' ) );

    if ( $upsell_name === '' ) {
        $upsell_name = trim( (string) elementsell_get_order_field( $order_id, 'Upsell produkt' ) );
    }

    if ( $upsell_name === '' && is_array( $settings ) ) {
        $upsell_name = elementsell_get_upsell_role_product_name( $settings );
    }

    if ( $upsell_name === '' || ! function_exists( 'elementsell_ensure_product_role' ) ) {
        return [];
    }

    $role_key = elementsell_ensure_product_role( $upsell_name );

    return $role_key ? [ $role_key ] : [];
}

/**
 * @param WP_User $user
 * @param object  $order
 * @return bool
 */
function elementsell_assign_order_roles_to_user( $user, $order ) {
    if ( ! $user instanceof WP_User || ! $order ) {
        return false;
    }

    if ( ! elementsell_user_should_receive_product_role( $user ) ) {
        return false;
    }

    $role_keys = elementsell_get_order_role_keys( $order );

    if ( empty( $role_keys ) ) {
        return false;
    }

    foreach ( $role_keys as $role_key ) {
        if ( ! in_array( $role_key, (array) $user->roles, true ) ) {
            $user->add_role( $role_key );
        }
    }

    $login_url = (string) elementsell_get_order_field( (int) $order->id, 'login_url' );

    if ( $login_url !== '' ) {
        elementsell_save_customer_login_redirect( (int) $user->ID, $login_url );
    }

    return true;
}

/**
 * @param object $order
 * @return bool
 */
function elementsell_apply_upsell_access_for_order( $order ) {
    if ( ! $order ) {
        return false;
    }

    $user = get_user_by( 'email', sanitize_email( (string) $order->email ) );

    if ( ! $user ) {
        return false;
    }

    return elementsell_assign_order_roles_to_user( $user, $order );
}

/**
 * @param object $order
 * @return string
 */
function elementsell_get_invoice_product_description_for_order( $order ) {
    if ( ! $order ) {
        return '';
    }

    $description = trim( (string) $order->product_name );

    if ( elementsell_order_upsell_was_accepted( (int) $order->id ) ) {
        $upsell_label = trim( (string) elementsell_get_order_field( (int) $order->id, 'Upsell produkt' ) );

        if ( $upsell_label !== '' ) {
            $description = $description !== ''
                ? $description . ' + ' . $upsell_label
                : $upsell_label;
        }
    }

    return $description;
}

/**
 * @param object $order
 * @return string
 */
function elementsell_render_upsell_thank_you_block( $order ) {
    if ( ! $order || ! elementsell_order_upsell_was_accepted( (int) $order->id ) ) {
        return '';
    }

    $title    = (string) elementsell_get_order_field( (int) $order->id, 'Upsell produkt' );
    $delivery = sanitize_key( (string) elementsell_get_order_field( (int) $order->id, 'Upsell doručení' ) );

    if ( $title === '' ) {
        return '';
    }

    $html  = '<div class="elementsell-thank-you-upsell">';
    $html .= '<p class="elementsell-thank-you-upsell__title">Přidali jste si: <strong>' . esc_html( $title ) . '</strong></p>';

    if ( $delivery === 'link' ) {
        $url   = (string) elementsell_get_order_field( (int) $order->id, 'Upsell odkaz' );
        $label = (string) elementsell_get_order_field( (int) $order->id, 'Upsell tlačítko' );

        if ( $label === '' ) {
            $label = __( 'Získat bonus', 'platebni-formular' );
        }

        if ( $url !== '' ) {
            $html .= '<p><a class="elementsell-thank-you-button elementsell-thank-you-button--upsell" href="' . esc_url( $url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $label ) . '</a></p>';
        }
    } elseif ( $delivery === 'role' ) {
        $roles = elementsell_get_order_role_product_names( $order );
        $html .= '<p class="elementsell-thank-you-upsell__note">Bonusový přístup je připravený na vašem účtu.</p>';
        if ( count( $roles ) > 1 ) {
            $html .= '<p class="elementsell-thank-you-upsell__note">' . esc_html( implode( ' + ', $roles ) ) . '</p>';
        }
    } else {
        $info_text = trim( (string) elementsell_get_order_field( (int) $order->id, 'Upsell informace' ) );

        if ( $info_text !== '' ) {
            $html .= '<div class="elementsell-thank-you-upsell__note">' . wpautop( esc_html( $info_text ) ) . '</div>';
        } else {
            $html .= '<p class="elementsell-thank-you-upsell__note">Podrobnosti k bonusu najdete v e-mailu.</p>';
        }
    }

    $html .= '</div>';

    return $html;
}

/**
 * @param object $order
 * @return string
 */
function elementsell_build_upsell_email_html( $order ) {
    if ( ! $order || ! elementsell_order_upsell_was_accepted( (int) $order->id ) ) {
        return '';
    }

    $order_id = (int) $order->id;
    $title    = trim( (string) elementsell_get_order_field( $order_id, 'Upsell produkt' ) );
    $settings = elementsell_get_order_widget_settings( $order );
    $delivery = elementsell_get_order_upsell_delivery( $order_id, $settings );

    if ( $title === '' && is_array( $settings ) ) {
        $title = elementsell_get_upsell_product_label( $settings );
    }

    if ( $title === '' ) {
        return '';
    }

    $html  = '<div style="margin:24px 0 0;padding:16px 0 0;border-top:1px solid #e5e7eb;">';
    $html .= '<p style="margin:0 0 12px;"><strong>Bonus:</strong> ' . esc_html( $title ) . '</p>';

    if ( $delivery === 'link' ) {
        $url   = (string) elementsell_get_order_field( $order_id, 'Upsell odkaz' );
        $label = (string) elementsell_get_order_field( $order_id, 'Upsell tlačítko' );

        if ( $label === '' ) {
            $label = __( 'Získat bonus', 'platebni-formular' );
        }

        if ( $url !== '' ) {
            $html .= elementsell_get_email_button_html( $url, $label );
        }
    } elseif ( $delivery === 'role' ) {
        $html .= '<p style="margin:0;">Bonusový přístup je připravený na vašem účtu.</p>';
    } elseif ( $delivery === 'info' ) {
        $info_text = trim( (string) elementsell_get_order_field( $order_id, 'Upsell informace' ) );

        if ( $info_text === '' && is_array( $settings ) ) {
            $info_text = trim( (string) ( $settings['upsell_info_text'] ?? '' ) );

            if ( $info_text === '' ) {
                $info_text = trim( (string) ( $settings['upsell_description'] ?? '' ) );
            }
        }

        if ( $info_text !== '' ) {
            $html .= elementsell_format_email_body_with_blocks( $info_text );
        }
    }

    $html .= '</div>';

    return $html;
}

/**
 * @param array  $settings
 * @return array{title: string, description: string, price: int, crossed: int, image_url: string, button_color: string}
 */
function elementsell_get_payment_upsell_display_data( $settings ) {
    $button_color = ! empty( $settings['button_color'] ) ? (string) $settings['button_color'] : '#22c55e';
    $image_url    = '';

    if ( ! empty( $settings['upsell_image']['url'] ) ) {
        $image_url = (string) $settings['upsell_image']['url'];
    }

    return [
        'title'        => ! empty( $settings['upsell_title'] )
            ? (string) $settings['upsell_title']
            : elementsell_get_upsell_product_label( $settings ),
        'description'  => ! empty( $settings['upsell_description'] ) ? (string) $settings['upsell_description'] : '',
        'price'        => (int) elementsell_get_upsell_price( $settings ),
        'crossed'      => ! empty( $settings['upsell_crossed_price'] ) ? (int) $settings['upsell_crossed_price'] : 0,
        'image_url'    => $image_url,
        'button_color' => $button_color,
    ];
}

/**
 * Náhled upsellu v Elementor editoru (inline pod formulářem, ne skrytý modal).
 *
 * @param array  $settings
 * @param string $wrapper_id
 * @param string $widget_id
 * @return void
 */
function elementsell_render_payment_upsell_editor_preview( $settings, $wrapper_id, $widget_id ) {
    if ( ! elementsell_upsell_is_enabled( $settings ) ) {
        return;
    }

    $upsell_id = 'elementsell-upsell-preview-' . $widget_id;
    $data      = elementsell_get_payment_upsell_display_data( $settings );
    $price_fmt = number_format( $data['price'], 0, ',', ' ' );

    ?>
    <div id="<?php echo esc_attr( $upsell_id ); ?>" class="elementsell-upsell-editor-preview">
        <span class="elementsell-upsell-editor-preview__badge"><?php esc_html_e( 'Náhled upsell', 'platebni-formular' ); ?></span>
        <p class="elementsell-upsell-editor-preview__hint"><?php esc_html_e( 'Zobrazí se zákazníkovi po kliknutí na tlačítko platby.', 'platebni-formular' ); ?></p>
        <div class="elementsell-upsell-modal__card" role="presentation">
            <?php if ( $data['image_url'] !== '' ) : ?>
                <div class="elementsell-upsell-modal__image-wrap">
                    <img class="elementsell-upsell-modal__image" src="<?php echo esc_url( $data['image_url'] ); ?>" alt="">
                </div>
            <?php endif; ?>
            <h2 class="elementsell-upsell-modal__title"><?php echo esc_html( $data['title'] ); ?></h2>
            <?php if ( $data['description'] !== '' ) : ?>
                <p class="elementsell-upsell-modal__text"><?php echo esc_html( $data['description'] ); ?></p>
            <?php endif; ?>
            <div class="elementsell-upsell-modal__price">
                <?php if ( $data['crossed'] > 0 ) : ?>
                    <span class="elementsell-upsell-modal__price-old"><?php echo esc_html( number_format( $data['crossed'], 0, ',', ' ' ) ); ?> Kč</span>
                <?php endif; ?>
                <span class="elementsell-upsell-modal__price-main"><?php echo esc_html( $price_fmt ); ?> Kč</span>
            </div>
            <button type="button" class="elementsell-upsell-modal__accept platebni-button" disabled>
                <?php echo esc_html( sprintf( __( 'Ano, přidat za %s Kč', 'platebni-formular' ), $price_fmt ) ); ?>
            </button>
            <button type="button" class="elementsell-upsell-modal__decline" disabled>
                <?php esc_html_e( 'Ne díky, pokračovat k platbě', 'platebni-formular' ); ?>
            </button>
        </div>
    </div>
    <?php
    elementsell_render_payment_upsell_styles( $wrapper_id, $data['button_color'], true );
}

/**
 * @param string $wrapper_id
 * @param string $button_color
 * @param bool   $editor_preview
 * @return void
 */
function elementsell_render_payment_upsell_styles( $wrapper_id, $button_color, $editor_preview = false ) {
    ?>
    <style>
        <?php if ( $editor_preview ) : ?>
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-editor-preview {
            margin: 24px auto 0;
            max-width: 420px;
            text-align: center;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-editor-preview__badge {
            display: inline-block;
            margin: 0 0 8px;
            padding: 4px 10px;
            border-radius: 999px;
            background: #eef2ff;
            color: #4338ca;
            font-size: 12px;
            font-weight: 700;
            letter-spacing: 0.02em;
            text-transform: uppercase;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-editor-preview__hint {
            margin: 0 0 14px;
            font-size: 13px;
            line-height: 1.45;
            color: #6b7280;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-editor-preview .elementsell-upsell-modal__card {
            border: 1px dashed #c7d2fe;
            box-shadow: none;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-editor-preview .elementsell-upsell-modal__accept,
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-editor-preview .elementsell-upsell-modal__decline {
            cursor: default;
            opacity: 0.92;
        }
        <?php else : ?>
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal {
            position: fixed;
            inset: 0;
            z-index: 99999;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 16px;
            box-sizing: border-box;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal[hidden] {
            display: none !important;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__backdrop {
            position: absolute;
            inset: 0;
            background: rgba(15, 23, 42, 0.55);
        }
        <?php endif; ?>
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__card {
            position: relative;
            width: 100%;
            max-width: 420px;
            background: #fff;
            border-radius: 16px;
            padding: 24px 22px;
            box-shadow: 0 20px 50px rgba(15, 23, 42, 0.2);
            text-align: center;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__image-wrap {
            margin: 0 0 16px;
            border-radius: 12px;
            overflow: hidden;
            background: #f3f4f6;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__image {
            display: block;
            width: 100%;
            height: auto;
            max-height: 320px;
            object-fit: contain;
            object-position: center;
            margin: 0;
            border-radius: 0;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__title {
            margin: 0 0 10px;
            font-size: 22px;
            line-height: 1.3;
            color: #111827;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__text {
            margin: 0 0 16px;
            font-size: 15px;
            line-height: 1.55;
            color: #4b5563;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__price {
            margin: 0 0 18px;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__price-old {
            display: block;
            text-decoration: line-through;
            color: #9ca3af;
            font-size: 14px;
            margin-bottom: 4px;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__price-main {
            font-size: 28px;
            font-weight: 700;
            color: #111827;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__accept {
            width: 100%;
            margin: 0 0 10px;
            background-color: <?php echo esc_attr( $button_color ); ?>;
        }
        #<?php echo esc_attr( $wrapper_id ); ?> .elementsell-upsell-modal__decline {
            width: 100%;
            border: 0;
            background: transparent;
            color: #6b7280;
            font-size: 14px;
            padding: 8px;
            cursor: pointer;
            text-decoration: underline;
        }
    </style>
    <?php
}

/**
 * @param array  $settings
 * @param string $wrapper_id
 * @param string $widget_id
 * @param bool   $is_editor
 * @return void
 */
function elementsell_render_payment_upsell_modal( $settings, $wrapper_id, $widget_id, $is_editor = false ) {
    if ( $is_editor ) {
        elementsell_render_payment_upsell_editor_preview( $settings, $wrapper_id, $widget_id );
        return;
    }

    if ( ! elementsell_upsell_is_enabled( $settings ) ) {
        return;
    }

    $upsell_id = 'elementsell-upsell-' . $widget_id;
    $data      = elementsell_get_payment_upsell_display_data( $settings );
    $price_fmt = number_format( $data['price'], 0, ',', ' ' );

    ?>
    <div id="<?php echo esc_attr( $upsell_id ); ?>" class="elementsell-upsell-modal" hidden>
        <div class="elementsell-upsell-modal__backdrop" data-upsell-close></div>
        <div class="elementsell-upsell-modal__card" role="dialog" aria-modal="true" aria-labelledby="<?php echo esc_attr( $upsell_id ); ?>-title">
            <?php if ( $data['image_url'] !== '' ) : ?>
                <div class="elementsell-upsell-modal__image-wrap">
                    <img class="elementsell-upsell-modal__image" src="<?php echo esc_url( $data['image_url'] ); ?>" alt="">
                </div>
            <?php endif; ?>
            <h2 class="elementsell-upsell-modal__title" id="<?php echo esc_attr( $upsell_id ); ?>-title"><?php echo esc_html( $data['title'] ); ?></h2>
            <?php if ( $data['description'] !== '' ) : ?>
                <p class="elementsell-upsell-modal__text"><?php echo esc_html( $data['description'] ); ?></p>
            <?php endif; ?>
            <div class="elementsell-upsell-modal__price">
                <?php if ( $data['crossed'] > 0 ) : ?>
                    <span class="elementsell-upsell-modal__price-old"><?php echo esc_html( number_format( $data['crossed'], 0, ',', ' ' ) ); ?> Kč</span>
                <?php endif; ?>
                <span class="elementsell-upsell-modal__price-main"><?php echo esc_html( $price_fmt ); ?> Kč</span>
            </div>
            <button type="button" class="elementsell-upsell-modal__accept platebni-button" data-upsell-accept="1">
                <?php echo esc_html( sprintf( __( 'Ano, přidat za %s Kč', 'platebni-formular' ), $price_fmt ) ); ?>
            </button>
            <button type="button" class="elementsell-upsell-modal__decline" data-upsell-accept="0">
                <?php esc_html_e( 'Ne díky, pokračovat k platbě', 'platebni-formular' ); ?>
            </button>
        </div>
    </div>
    <?php
    elementsell_render_payment_upsell_styles( $wrapper_id, $data['button_color'], false );
}
