<?php

/**
 * Admin Subscription Cancelled Email
 *
 * Sent to admin when a customer cancels their subscription.
 *
 * @package ArraySubs\Features\Emails\Emails
 * @since 1.0.0
 */

namespace ArraySubs\Features\Emails\Emails;

defined('ABSPATH') || exit;

use ArraySubs\Features\Emails\Abstracts\BaseSubscriptionEmail;

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

        $this->heading        = __('Subscription cancelled', 'arraysubs');
        $this->subject        = __('[{site_title}] Subscription #{subscription_id} cancelled by {customer_name}', 'arraysubs');

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

        // Add cancellation placeholders with sample value for preview
        $this->placeholders['{cancellation_reason}'] = __('Customer requested cancellation', 'arraysubs');

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

        $this->placeholders['{cancellation_reason}'] = get_post_meta(
            $this->subscription_id,
            '_cancellation_reason',
            true
        ) ?: __('Not specified', '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_sent_to_admin'] = true;
        $args['arraysubs_cancellation_reason'] = get_post_meta($this->subscription_id, '_cancellation_reason', 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 '';
    }
}
