<?php

/**
 * Payment Failed Email
 *
 * Sent to customers when a renewal payment fails.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Payment Failed Email class
 */
class PaymentFailedEmail extends BaseSubscriptionEmail
{
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_payment_failed';
        $this->customer_email = true;
        $this->title          = __('[ArraySubs] Renewal Payment Failed', 'arraysubs');
        $this->description    = __('Payment failed emails are sent to customers when their renewal payment fails.', 'arraysubs');

        $this->heading        = __('Payment failed', 'arraysubs');
        $this->subject        = __('[{site_title}] Payment failed for subscription #{subscription_id}', 'arraysubs');

        $this->template_html  = 'customer-payment-failed.php';
        $this->template_plain = 'plain/customer-payment-failed.php';

        // Add order placeholders with sample values for preview
        $this->placeholders['{order_id}'] = '67890';
        $this->placeholders['{order_total}'] = wc_price(29.99);
        $this->placeholders['{order_pay_url}'] = wc_get_checkout_url();
        $this->placeholders['{retry_url}'] = wc_get_checkout_url();

        parent::__construct();
    }

    /**
     * Populate placeholders
     *
     * @param array $args Additional arguments
     */
    protected function populate_placeholders(array $args = []): void
    {
        parent::populate_placeholders($args);

        if ($this->order) {
            $this->placeholders['{order_id}'] = $this->order->get_id();
            $this->placeholders['{order_total}'] = $this->order->get_formatted_order_total();
            $this->placeholders['{order_pay_url}'] = $this->order->get_checkout_payment_url();
            $this->placeholders['{retry_url}'] = $this->order->get_checkout_payment_url();
        }
    }

    /**
     * Get template arguments
     *
     * @param bool $plain_text Whether this is plain text email
     * @return array
     */
    protected function get_template_args(bool $plain_text = false): array
    {
        $args = parent::get_template_args($plain_text);
        $args['arraysubs_retry_url'] = $this->order ? $this->order->get_checkout_payment_url() : '';

        // Surface the classified failure reason in customer-friendly language
        // so the email tells the customer *why* the charge failed rather than
        // just that it did.
        $args['arraysubs_failure_reason_text'] = '';
        if ($this->subscription_id) {
            $category = (string) get_post_meta($this->subscription_id, '_last_payment_failure_category', true);
            if ('' !== $category && 'unknown' !== $category && function_exists('arraysubs_describe_payment_failure_category')) {
                $args['arraysubs_failure_reason_text'] = sprintf(
                    /* translators: %s: failure reason e.g. "the card was declined by the issuer" */
                    __('Reason: %s.', 'arraysubs'),
                    arraysubs_describe_payment_failure_category($category)
                );
            }
        }

        return $args;
    }

    /**
     * Get default additional content
     *
     * @return string
     */
    public function get_default_additional_content(): string
    {
        return __('Please update your payment method to avoid service interruption.', 'arraysubs');
    }
}
