<?php

/**
 * Subscription Expiring Soon Email
 *
 * Sent to customers before a fixed-length subscription reaches its final cycle.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Subscription Expiring Soon Email class
 */
class SubscriptionExpiringSoonEmail extends BaseSubscriptionEmail
{
    /**
     * Days before expiration.
     *
     * @var int
     */
    protected int $days_before = 7;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_expiring_soon';
        $this->customer_email = true;
        $this->title          = __('[ArraySubs] Subscription Expiring Soon', 'arraysubs');
        $this->description    = __('Subscription expiring soon emails are sent before a fixed-length subscription reaches its final cycle.', 'arraysubs');

        $this->heading        = __('Your subscription is ending soon', 'arraysubs');
        $this->subject        = __('[{site_title}] Your subscription #{subscription_id} is ending soon', 'arraysubs');

        $this->template_html  = 'customer-renewal-reminder.php';
        $this->template_plain = 'plain/customer-renewal-reminder.php';

        $this->placeholders['{days_before}'] = '7';
        $this->placeholders['{subscription_end_date}'] = \arraysubs_format_timestamp_local(
            strtotime('+7 days', time()),
            \get_option('date_format')
        );

        parent::__construct();
    }

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

        $this->days_before = absint($args['days_before'] ?? \arraysubs_get_setting('emails.expiring_soon.days_before', 7)) ?: 7;

        $this->placeholders['{days_before}'] = (string) $this->days_before;
        $this->placeholders['{subscription_end_date}'] = $this->getExpirationDateDisplay();
    }

    /**
     * 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_days_before'] = $this->days_before;
        $args['arraysubs_reminder_context'] = 'expiring';
        $args['arraysubs_context_date'] = $this->getExpirationDateDisplay();
        $args['arraysubs_context_date_label'] = __('End Date', 'arraysubs');

        return $args;
    }

    /**
     * Get the best available expiration date display.
     *
     * @return string
     */
    private function getExpirationDateDisplay(): string
    {
        $end_date = (string) get_post_meta($this->subscription_id, '_end_date', true);

        if (!$this->isValidStoredDate($end_date)) {
            $end_date = (string) get_post_meta($this->subscription_id, '_next_payment_date', true);
        }

        return $this->isValidStoredDate($end_date) ? \arraysubs_format_date_local($end_date) : __('N/A', 'arraysubs');
    }

    /**
     * Check whether a stored MySQL date is usable.
     *
     * @param string $date Date value.
     * @return bool
     */
    private function isValidStoredDate(string $date): bool
    {
        return '' !== $date && '0000-00-00 00:00:00' !== $date && false !== strtotime($date);
    }

    /**
     * Get default additional content.
     *
     * @return string
     */
    public function get_default_additional_content(): string
    {
        return __('No action is required unless you want to renew, change, or extend the subscription before it ends.', 'arraysubs');
    }
}
