<?php

/**
 * Checkout Display Trait
 *
 * Renders subscription summaries at checkout and within order details.
 *
 * @package ArraySubs\Features\SubscriptionCheckout\Services\Traits
 */

namespace ArraySubs\Features\SubscriptionCheckout\Services\Traits;

defined('ABSPATH') || exit;

/**
 * Checkout display methods
 */
trait CheckoutDisplayTrait
{
    /**
     * Display subscription summary in checkout order review table.
     *
     * @return void
     */
    public function displaySubscriptionSummaryInTable(): void
    {
        $cart = WC()->cart;

        if (!$cart) {
            return;
        }

        $subscriptions = [];

        foreach ($cart->get_cart() as $cart_item) {
            $product_id = $cart_item['product_id'];
            $variation_id = $cart_item['variation_id'] ?? 0;
            $check_id = $variation_id > 0 ? $variation_id : $product_id;

            if (!arraysubs_is_subscription_product($check_id)) {
                continue;
            }

            $product = $cart_item['data'];
            $subscription_data = arraysubs_get_product_subscription_data($check_id);

            if (!$subscription_data) {
                continue;
            }

            $subscriptions[] = [
                'product'    => $product,
                'data'       => $subscription_data,
                'quantity'   => $cart_item['quantity'],
                'migration'  => $cart_item['arraysubs_checkout_migration'] ?? null,
            ];
        }

        if (empty($subscriptions)) {
            return;
        }

        // Output subscription details as table rows
        foreach ($subscriptions as $subscription) {
            $data = $subscription['data'];
            $quantity = $subscription['quantity'];
            $migration = $subscription['migration'];

            $billing_schedule = arraysubs_get_billing_schedule_string($data['interval'], $data['period']);

            echo '<tr class="subscription-recurring-info">';
            echo '<th>' . (is_array($migration) && !empty($migration['eligible']) ? esc_html__('Due today', 'arraysubs') : esc_html__('Recurring', 'arraysubs')) . '</th>';
            echo '<td>';
            if (is_array($migration) && !empty($migration['eligible'])) {
                echo wp_kses_post($migration['proration']['formatted']['net'] ?? wc_price($migration['proration']['net_amount'] ?? 0));
                echo '<br><small>' . sprintf(
                    /* translators: 1: switch type label, 2: current product name */
                    esc_html__('This checkout replaces the current %1$s plan from %2$s.', 'arraysubs'),
                    esc_html($migration['switch_type_label']),
                    esc_html($migration['current_product_name'])
                ) . '</small>';
                echo '<br><small>' . sprintf(
                    /* translators: 1: recurring amount, 2: billing schedule */
                    esc_html__('After checkout: %1$s %2$s', 'arraysubs'),
                    wp_kses_post(wc_price($data['price'] * $quantity)),
                    esc_html($billing_schedule)
                ) . '</small>';
            } else {
                echo wp_kses_post(wc_price($data['price'] * $quantity)) . ' ' . esc_html($billing_schedule);
            }

            // Different renewal price
            if ($data['enable_different_renewal_price'] && !empty($data['different_renewal_price'])) {
                if ($data['different_renewal_price_after'] > 1) {
                    echo '<br><small>' . sprintf(
                        /* translators: 1: renewal price amount, 2: payment cycle count */
                        esc_html__('then %1$s after %2$d payments', 'arraysubs'),
                        wp_kses_post(wc_price($data['different_renewal_price'] * $quantity)),
                        (int) $data['different_renewal_price_after']
                    ) . '</small>';
                } else {
                    echo '<br><small>' . sprintf(
                        /* translators: %s: renewal price amount after initial payment */
                        esc_html__('then %s', 'arraysubs'),
                        wp_kses_post(wc_price($data['different_renewal_price'] * $quantity))
                    ) . '</small>';
                }
            }
            echo '</td>';
            echo '</tr>';

            // Signup fee row (if applicable)
            if ($data['signup_fee'] > 0) {
                echo '<tr class="subscription-signup-fee-info">';
                echo '<th>' . esc_html__('Signup Fee', 'arraysubs') . '</th>';
                echo '<td>' . wp_kses_post(wc_price($data['signup_fee'])) . ' <small>(' . esc_html__('one-time', 'arraysubs') . ')</small></td>';
                echo '</tr>';
            }

            // Trial info
            if (!empty($data['trial_length']) && $data['trial_length'] > 0) {
                $trial_schedule = arraysubs_get_duration_string($data['trial_length'], $data['trial_period']);
                echo '<tr class="subscription-trial-info">';
                echo '<th>' . esc_html__('Free Trial', 'arraysubs') . '</th>';
                echo '<td>' . esc_html($trial_schedule) . '</td>';
                echo '</tr>';
            }
        }
    }

    /**
     * Display subscriptions in order details
     *
     * @param \WC_Order $order Order object
     */
    public function displayOrderSubscriptions($order): void
    {
        if (!$order) {
            return;
        }

        $arraysubs_subscription_ids = get_post_meta($order->get_id(), '_subscription_ids', true);

        if (empty($arraysubs_subscription_ids) || !is_array($arraysubs_subscription_ids)) {
            return;
        }

        include dirname(dirname(__DIR__)) . '/views/order-subscriptions.php';
    }
}
