<?php

/**
 * Subscription Pending Cancellation Email
 *
 * Sent to customers when their subscription is scheduled to cancel at the
 * end of the current billing period. Distinct from
 * SubscriptionCancelledEmail, which is sent when the cancellation actually
 * takes effect.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Subscription Pending Cancellation Email class
 */
class SubscriptionPendingCancellationEmail extends BaseSubscriptionEmail
{
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_subscription_pending_cancellation';
        $this->customer_email = true;
        $this->title          = __('[ArraySubs] Subscription Scheduled to Cancel', 'arraysubs');
        $this->description    = __('Sent to customers when their subscription is scheduled to cancel at the end of the current billing period.', 'arraysubs');

        $this->heading        = __('Your subscription is scheduled to cancel', 'arraysubs');
        $this->subject        = __('[{site_title}] Subscription #{subscription_id} scheduled to cancel on {scheduled_cancel_date}', 'arraysubs');

        $this->template_html  = 'customer-subscription-pending-cancellation.php';
        $this->template_plain = 'plain/customer-subscription-pending-cancellation.php';

        $this->placeholders['{scheduled_cancel_date}'] = \arraysubs_format_timestamp_local(
            \current_time('timestamp', true),
            \get_option('date_format')
        );
        $this->placeholders['{cancellation_reason}'] = __('Customer requested cancellation', 'arraysubs');

        parent::__construct();
    }

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

        $scheduled_date = \get_post_meta($this->subscription_id, '_cancellation_scheduled_date', true);
        $this->placeholders['{scheduled_cancel_date}'] = !empty($scheduled_date)
            ? \arraysubs_format_date_local($scheduled_date)
            : __('end of billing period', 'arraysubs');

        $reason_key = (string) \get_post_meta($this->subscription_id, '_cancellation_reason', true);
        $reason_label = '' !== $reason_key && \function_exists('arraysubs_get_cancellation_reason_label')
            ? \arraysubs_get_cancellation_reason_label($reason_key)
            : $reason_key;

        $this->placeholders['{cancellation_reason}'] = $reason_label !== ''
            ? $reason_label
            : __('Not specified', 'arraysubs');
    }

    /**
     * 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);

        $scheduled_date = \get_post_meta($this->subscription_id, '_cancellation_scheduled_date', true);
        $args['arraysubs_scheduled_cancel_date'] = !empty($scheduled_date)
            ? \arraysubs_format_date_local($scheduled_date)
            : '';
        $args['arraysubs_scheduled_cancel_date_raw'] = $scheduled_date;

        $reason_key = (string) \get_post_meta($this->subscription_id, '_cancellation_reason', true);
        $args['arraysubs_cancellation_reason'] = '' !== $reason_key && \function_exists('arraysubs_get_cancellation_reason_label')
            ? \arraysubs_get_cancellation_reason_label($reason_key)
            : $reason_key;

        // Build URL to the customer-facing view-subscription endpoint so
        // they can undo the cancellation. wc_get_endpoint_url returns the
        // myaccount permalink; falls back to my-account if WC pages aren't
        // configured.
        $args['arraysubs_view_subscription_url'] = \function_exists('wc_get_endpoint_url')
            ? \wc_get_endpoint_url('view-subscription', $this->subscription_id, \wc_get_page_permalink('myaccount'))
            : '';

        return $args;
    }

    /**
     * Get default additional content
     *
     * @return string
     */
    public function get_default_additional_content(): string
    {
        return __('You will continue to have access until the cancellation date. If you change your mind, you can undo the scheduled cancellation from your account.', 'arraysubs');
    }
}
