<?php

/**
 * Trial Started Email
 *
 * Sent to customers when their free trial subscription begins.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Trial Started Email class
 */
class TrialStartedEmail extends BaseSubscriptionEmail
{
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_trial_started';
        $this->customer_email = true;
        $this->title          = __('[ArraySubs] Trial Started', 'arraysubs');
        $this->description    = __('Trial started emails are sent to customers when their free trial begins.', 'arraysubs');

        $this->heading        = __('Your free trial has started!', 'arraysubs');
        $this->subject        = __('[{site_title}] Your free trial for {product_name} has started', 'arraysubs');

        $this->template_html  = 'customer-trial-started.php';
        $this->template_plain = 'plain/customer-trial-started.php';

        // Add trial placeholders with sample values for preview
        $this->placeholders['{trial_end_date}'] = \arraysubs_format_timestamp_local(
            strtotime('+7 days', \current_time('timestamp', true)),
            \get_option('date_format')
        );
        $this->placeholders['{trial_length}'] = __('7 days', 'arraysubs');

        parent::__construct();
    }

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

        $trial_end = get_post_meta($this->subscription_id, '_trial_end_date', true);
        $trial_length = get_post_meta($this->subscription_id, '_trial_length', true);
        $trial_period = get_post_meta($this->subscription_id, '_trial_period', true);

        $this->placeholders['{trial_end_date}'] = !empty($trial_end)
            ? \arraysubs_format_date_local($trial_end)
            : __('N/A', 'arraysubs');

        $this->placeholders['{trial_length}'] = !empty($trial_length)
            ? \arraysubs_format_date_interval((int) $trial_length, $trial_period ?: 'day')
            : __('N/A', '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_trial_end_date'] = $this->subscription_id
            ? get_post_meta($this->subscription_id, '_trial_end_date', true)
            : gmdate('Y-m-d H:i:s', strtotime('+7 days', \current_time('timestamp', true)));
        $args['arraysubs_trial_length'] = $this->subscription_id
            ? get_post_meta($this->subscription_id, '_trial_length', true)
            : 7;
        $args['arraysubs_trial_period'] = $this->subscription_id
            ? get_post_meta($this->subscription_id, '_trial_period', true)
            : 'day';
        return $args;
    }

    /**
     * Get default additional content
     *
     * @return string
     */
    public function get_default_additional_content(): string
    {
        return __('Enjoy your free trial! You will be automatically charged when the trial period ends unless you cancel before then.', 'arraysubs');
    }
}
