<?php

/**
 * Trial Converted Email
 *
 * Sent to customers when their trial subscription converts to a paid subscription.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Trial Converted Email class
 */
class TrialConvertedEmail extends BaseSubscriptionEmail
{
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_trial_converted';
        $this->customer_email = true;
        $this->title          = __('[ArraySubs] Trial Converted', 'arraysubs');
        $this->description    = __('Trial converted emails are sent to customers when their free trial converts to a paid subscription.', 'arraysubs');

        $this->heading        = __('Your trial has been converted to a paid subscription', 'arraysubs');
        $this->subject        = __('[{site_title}] Your trial for {product_name} has converted to a paid subscription', 'arraysubs');

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

        // Add trial placeholders with sample values for preview
        $this->placeholders['{next_payment_date}'] = \arraysubs_format_timestamp_local(
            strtotime('+1 month', \current_time('timestamp', true)),
            \get_option('date_format')
        );
        $this->placeholders['{subscription_price}'] = wc_price(19.99);

        parent::__construct();
    }

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

        $next_payment = get_post_meta($this->subscription_id, '_next_payment_date', true);
        $price = get_post_meta($this->subscription_id, '_subscription_price', true);

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

        $this->placeholders['{subscription_price}'] = !empty($price)
            ? wc_price($price)
            : wc_price(0);
    }

    /**
     * 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_next_payment_date'] = get_post_meta($this->subscription_id, '_next_payment_date', true);
        $args['arraysubs_subscription_price'] = get_post_meta($this->subscription_id, '_subscription_price', true);
        return $args;
    }

    /**
     * Get default additional content
     *
     * @return string
     */
    public function get_default_additional_content(): string
    {
        return __('Thank you for continuing with your subscription! Your payment method has been charged and your subscription is now active.', 'arraysubs');
    }
}
