<?php

/**
 * Renewal Reminder Email
 *
 * Sent to customers X days before their next payment is due.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Renewal Reminder Email class
 */
class RenewalReminderEmail extends BaseSubscriptionEmail
{
    /**
     * Days before renewal
     *
     * @var int
     */
    protected int $days_before = 3;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_renewal_reminder';
        $this->customer_email = true;
        $this->title          = __('[ArraySubs] Renewal Reminder', 'arraysubs');
        $this->description    = __('Renewal reminder emails are sent to customers before their subscription payment is due.', 'arraysubs');

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

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

        // Add days_before placeholder with sample value for preview
        $this->placeholders['{days_before}'] = '3';

        parent::__construct();
    }

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

        $this->days_before = $args['days_before'] ?? 3;
        $this->placeholders['{days_before}'] = $this->days_before;
    }

    /**
     * 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;
        return $args;
    }

    /**
     * Get default additional content
     *
     * @return string
     */
    public function get_default_additional_content(): string
    {
        return __('If you wish to make any changes to your subscription, please visit your account page.', 'arraysubs');
    }

    /**
     * Initialize form fields
     */
    public function init_form_fields(): void
    {
        parent::init_form_fields();

        // Add days before field
        $this->form_fields['days_before'] = [
            'title'       => __('Days before renewal', 'arraysubs'),
            'type'        => 'number',
            'description' => __('Number of days before the renewal date to send this email.', 'arraysubs'),
            'default'     => '3',
            'desc_tip'    => true,
            'custom_attributes' => [
                'min' => '1',
                'max' => '30',
            ],
        ];
    }
}
