<?php

/**
 * Subscription Cancelled Email
 *
 * Sent to customers when their subscription is cancelled.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Subscription Cancelled Email class
 */
class SubscriptionCancelledEmail extends BaseSubscriptionEmail
{
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_subscription_cancelled';
        $this->customer_email = true;
        $this->title          = __('[ArraySubs] Subscription Cancelled', 'arraysubs');
        $this->description    = __('Subscription cancelled emails are sent to customers when their subscription is cancelled.', 'arraysubs');

        $this->heading        = __('Your subscription has been cancelled', 'arraysubs');
        $this->subject        = __('[{site_title}] Your subscription #{subscription_id} has been cancelled', 'arraysubs');

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

        // Add cancellation placeholders with sample values for preview
        $this->placeholders['{cancellation_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);

        $this->placeholders['{cancellation_date}'] = \arraysubs_format_date_local(
            get_post_meta($this->subscription_id, '_cancelled_date', true) ?: \current_time('mysql', true)
        );
        $this->placeholders['{cancellation_reason}'] = get_post_meta(
            $this->subscription_id,
            '_cancellation_reason',
            true
        ) ?: __('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);
        $args['arraysubs_cancellation_reason'] = get_post_meta($this->subscription_id, '_cancellation_reason', true);
        return $args;
    }

    /**
     * Get default additional content
     *
     * @return string
     */
    public function get_default_additional_content(): string
    {
        return __('We are sorry to see you go. If you change your mind, you can always subscribe again.', 'arraysubs');
    }
}
