<?php

/**
 * Product Subscription Info Display
 *
 * @package ArraySubs\Features\SubscriptionProducts
 * @var \WC_Product $product Current product
 */

defined('ABSPATH') || exit;

global $product;

if (!$product) {
    return;
}

$arraysubs_product_id = $product->get_id();

if (!arraysubs_is_subscription_product($product)) {
    return;
}

// Get subscription data using helper function (uses get_price() which respects sale dates)
$arraysubs_data = arraysubs_get_product_subscription_data($product);

if (!$arraysubs_data) {
    return;
}

// Get pricing info
$arraysubs_recurring_price = $arraysubs_data['price']; // This is already the sale price if on sale, or regular price
$arraysubs_regular_price = $product->get_regular_price();
$arraysubs_is_on_sale = $product->is_on_sale();
$arraysubs_signup_fee = $arraysubs_data['signup_fee'];
$arraysubs_enable_different_renewal = $arraysubs_data['enable_different_renewal_price'];
$arraysubs_different_renewal_price = $arraysubs_data['different_renewal_price'];
$arraysubs_different_renewal_after = $arraysubs_data['different_renewal_price_after'];

// Get billing schedule string
$arraysubs_billing_schedule = arraysubs_get_billing_schedule_string($arraysubs_data['interval'], $arraysubs_data['period']);
?>

<div class="arraysubs-product-info">
    <div class="subscription-terms">
        <?php if ($arraysubs_enable_different_renewal && !empty($arraysubs_different_renewal_price)) : ?>
            <p class="subscription-pricing">
                <?php
                if ($arraysubs_different_renewal_after > 1) {
                    printf(
                        /* translators: %1$s: price, %2$d: number of periods, %3$s: renewal price, %4$s: billing schedule */
                        esc_html__('%1$s %4$s for the first %2$d payments, then %3$s %4$s', 'arraysubs'),
                        wp_kses_post(wc_price($arraysubs_recurring_price)),
                        (int) $arraysubs_different_renewal_after,
                        wp_kses_post(wc_price($arraysubs_different_renewal_price)),
                        esc_html($arraysubs_billing_schedule)
                    );
                } else {
                    printf(
                        /* translators: 1: initial payment amount, 2: renewal price amount, 3: billing cycle frequency */
                        esc_html__('First payment: %1$s, then %2$s %3$s', 'arraysubs'),
                        wp_kses_post(wc_price($arraysubs_recurring_price)),
                        wp_kses_post(wc_price($arraysubs_different_renewal_price)),
                        esc_html($arraysubs_billing_schedule)
                    );
                }
                ?>
            </p>
        <?php else : ?>
            <p class="subscription-pricing">
                <?php
                if ($arraysubs_is_on_sale) {
                    printf(
                        /* translators: %1$s: sale price, %2$s: billing schedule, %3$s: regular price */
                        esc_html__('%1$s %2$s (was %3$s)', 'arraysubs'),
                        wp_kses_post(wc_price($arraysubs_recurring_price)),
                        esc_html($arraysubs_billing_schedule),
                        wp_kses_post(wc_price($arraysubs_regular_price))
                    );
                } else {
                    /* translators: 1: subscription price amount, 2: billing cycle frequency */
                    printf(
                        /* translators: 1: subscription price amount, 2: billing cycle frequency */
                        esc_html__('%1$s %2$s', 'arraysubs'),
                        wp_kses_post(wc_price($arraysubs_recurring_price)),
                        esc_html($arraysubs_billing_schedule)
                    );
                }
                ?>
            </p>
        <?php endif; ?>

        <?php if ($arraysubs_signup_fee > 0) : ?>
            <p class="subscription-signup-fee">
                <?php
                /* translators: %s: one-time signup fee amount charged at purchase */
                printf(
                    /* translators: %s: one-time signup fee amount charged at purchase */
                    esc_html__('+ %s signup fee', 'arraysubs'),
                    wp_kses_post(wc_price($arraysubs_signup_fee))
                );
                ?>
            </p>
        <?php endif; ?>

        <?php if ($arraysubs_data['trial_length'] > 0) : ?>
            <p class="subscription-trial">
                <?php
                $arraysubs_trial_schedule = arraysubs_get_duration_string($arraysubs_data['trial_length'], $arraysubs_data['trial_period']);
                /* translators: %s: trial period duration (e.g., "7 days", "1 month") */
                printf(
                    /* translators: %s: trial period duration (e.g., "7 days", "1 month") */
                    esc_html__('%s free trial', 'arraysubs'),
                    esc_html($arraysubs_trial_schedule)
                );
                ?>
            </p>
        <?php endif; ?>

        <?php if ($arraysubs_data['length'] > 0) : ?>
            <p class="subscription-length">
                <?php
                /* translators: %d: total number of billing cycles for subscription duration */
                echo esc_html(
                    sprintf(
                        /* translators: %d: total number of billing cycles for subscription duration */
                        _n('%d billing cycle', '%d billing cycles', $arraysubs_data['length'], 'arraysubs'),
                        (int) $arraysubs_data['length']
                    )
                );
                ?>
            </p>
        <?php endif; ?>
    </div>
</div>