<?php

/**
 * Auto-Downgrade Email
 *
 * Sent to customers when their subscription is automatically
 * switched to a fallback plan after expiration or cancellation.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.7.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Auto-Downgrade Email class
 */
class AutoDowngradeEmail extends BaseSubscriptionEmail
{
    /**
     * Old product ID (before downgrade)
     *
     * @var int
     */
    protected int $old_product_id = 0;

    /**
     * New product ID (after downgrade)
     *
     * @var int
     */
    protected int $new_product_id = 0;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_auto_downgrade';
        $this->customer_email = true;
        $this->title          = __('[ArraySubs] Subscription Auto-Downgraded', 'arraysubs');
        $this->description    = __(
            'Auto-downgrade emails are sent to customers when their subscription is automatically switched to a fallback plan.',
            'arraysubs'
        );

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

        $this->template_html  = 'customer-auto-downgrade.php';
        $this->template_plain = 'plain/customer-auto-downgrade.php';

        parent::__construct();

        $this->placeholders['{old_product_name}'] = __('Previous Plan', 'arraysubs');
        $this->placeholders['{new_product_name}'] = __('New Plan', 'arraysubs');
    }

    /**
     * Trigger the email
     *
     * @param int   $subscription_id Subscription ID.
     * @param array $args            Must include 'old_product_id' and 'new_product_id'.
     */
    public function trigger(int $subscription_id, array $args = []): void
    {
        $this->old_product_id = absint($args['old_product_id'] ?? 0);
        $this->new_product_id = absint($args['new_product_id'] ?? 0);

        parent::trigger($subscription_id, $args);
    }

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

        $old_product = $this->old_product_id ? wc_get_product($this->old_product_id) : null;
        $new_product = $this->new_product_id ? wc_get_product($this->new_product_id) : null;

        $this->placeholders['{old_product_name}'] = $old_product
            ? arraysubs_get_subscription_product_name($old_product)
            : '#' . $this->old_product_id;

        $this->placeholders['{new_product_name}'] = $new_product
            ? arraysubs_get_subscription_product_name($new_product)
            : '#' . $this->new_product_id;
    }

    /**
     * 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_old_product_id'] = $this->old_product_id;
        $args['arraysubs_new_product_id'] = $this->new_product_id;
        $args['arraysubs_old_product']    = $this->old_product_id ? wc_get_product($this->old_product_id) : null;
        $args['arraysubs_new_product']    = $this->new_product_id ? wc_get_product($this->new_product_id) : null;

        return $args;
    }

    /**
     * Get default additional content
     *
     * @return string
     */
    public function get_default_additional_content(): string
    {
        return __(
            'Your subscription has been automatically adjusted. You can view your subscription details in your account.',
            'arraysubs'
        );
    }
}
