<?php

/**
 * Customer Card Expiring Email
 *
 * Sent to customers when Stripe reports a saved card is expiring soon.
 *
 * @package ArraySubs\Features\Emails\Emails
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Customer card expiring email class.
 */
class CustomerCardExpiringEmail extends BaseSubscriptionEmail
{
    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_card_expiring';
        $this->customer_email = true;
        $this->title          = __('[ArraySubs] Card Expiring', 'arraysubs');
        $this->description    = __('Card expiring emails are sent when the saved payment card is expiring soon.', 'arraysubs');
        $this->heading        = __('Your card is expiring soon', 'arraysubs');
        $this->subject        = __('[{site_title}] Update the card for subscription #{subscription_id}', 'arraysubs');
        $this->template_html  = 'customer-card-expiring.php';
        $this->template_plain = 'plain/customer-card-expiring.php';

        parent::__construct();
    }

    /**
     * Get template arguments.
     *
     * @param bool $plain_text Whether this is plain text.
     * @return array
     */
    protected function get_template_args(bool $plain_text = false): array
    {
        $args   = parent::get_template_args($plain_text);
        $month  = (int) get_post_meta($this->subscription_id, '_payment_method_expiry_month', true);
        $year   = (int) get_post_meta($this->subscription_id, '_payment_method_expiry_year', true);
        $expiry = $month > 0 && $year > 0 ? sprintf('%02d/%d', $month, $year) : __('soon', 'arraysubs');

        $args['arraysubs_card_last4']          = (string) get_post_meta($this->subscription_id, '_payment_method_last4', true);
        $args['arraysubs_card_expiry']         = $expiry;
        $args['arraysubs_update_payment_url']  = wc_get_account_endpoint_url('subscriptions');

        return $args;
    }

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