<?php

/**
 * Admin New Subscription Email
 *
 * Sent to admin when a new subscription is created.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

/**
 * Admin New Subscription Email class
 */
class AdminNewSubscriptionEmail extends BaseSubscriptionEmail
{
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->id             = 'arraysubs_admin_new_subscription';
        $this->customer_email = false;
        $this->title          = __('[ArraySubs] New Subscription (Admin)', 'arraysubs');
        $this->description    = __('New subscription emails are sent to the admin when a customer subscribes.', 'arraysubs');

        $this->heading        = __('New subscription received', 'arraysubs');
        $this->subject        = __('[{site_title}] New subscription #{subscription_id} from {customer_name}', 'arraysubs');

        $this->template_html  = 'admin-new-subscription.php';
        $this->template_plain = 'plain/admin-new-subscription.php';

        // 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;
        }

        $this->setup_locale();
        // Admin emails go to admin, not customer
        $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();
    }

    /**
     * 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();

        // Add recipient field for admin
        $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 '';
    }
}
