<?php

/**
 * Admin Payment Failed Email
 *
 * Sent to admin when a subscription payment fails.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Admin Payment Failed Email class
 */
class AdminPaymentFailedEmail extends BaseSubscriptionEmail
{
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_admin_payment_failed';
        $this->customer_email = false;
        $this->title          = __('[ArraySubs] Payment Failed (Admin)', 'arraysubs');
        $this->description    = __('Payment failed emails are sent to the admin when a renewal payment fails.', 'arraysubs');

        $this->heading        = __('Subscription payment failed', 'arraysubs');
        $this->subject        = __('[{site_title}] Payment failed for subscription #{subscription_id}', 'arraysubs');

        $this->template_html  = 'admin-payment-failed.php';
        $this->template_plain = 'plain/admin-payment-failed.php';

        // Add order placeholders with sample values for preview
        $this->placeholders['{order_id}'] = '67890';
        $this->placeholders['{order_total}'] = wc_price(29.99);

        // Set recipient to admin
        $this->recipient = $this->get_admin_email_recipient();

        parent::__construct();
    }

    /**
     * Trigger the email
     *
     * @param int   $subscription_id Subscription ID
     * @param array $args            Additional arguments
     */
    public function trigger(int $subscription_id, array $args = []): void
    {
        $this->subscription_id = $subscription_id;
        $this->subscription = arraysubs_get_subscription($subscription_id);

        if (!$this->subscription instanceof \WP_Post) {
            return;
        }

        // Set order if provided
        if (!empty($args['order_id'])) {
            $this->order = wc_get_order($args['order_id']);
        }

        $this->setup_locale();
        $this->recipient = $this->get_admin_email_recipient();
        $this->populate_placeholders($args);

        if ($this->is_enabled() && $this->get_recipient()) {
            $this->send(
                $this->get_recipient(),
                $this->get_subject(),
                $this->get_content(),
                $this->get_headers(),
                $this->get_attachments()
            );
        }

        $this->restore_locale();
    }

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

        if ($this->order) {
            $this->placeholders['{order_id}'] = $this->order->get_id();
            $this->placeholders['{order_total}'] = $this->order->get_formatted_order_total();
        }
    }

    /**
     * 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_sent_to_admin'] = true;
        return $args;
    }

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

        $this->form_fields = array_merge(
            [
                'recipient' => [
                    'title'       => __('Recipient(s)', 'arraysubs'),
                    'type'        => 'text',
                    'description' => sprintf(
                        /* translators: %s: default admin email */
                        __('Enter recipients (comma separated) for this email. Defaults to %s.', 'arraysubs'),
                        '<code>' . esc_attr($this->get_default_admin_email()) . '</code>'
                    ),
                    'placeholder' => '',
                    'default'     => '',
                    'desc_tip'    => true,
                ],
            ],
            $this->form_fields
        );
    }

    /**
     * Get default additional content
     *
     * @return string
     */
    public function get_default_additional_content(): string
    {
        return '';
    }
}
