<?php

namespace WarrantyCart\Notifications\Emails;

use WC_Email;
use WC_Order;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Warranty Reminder Email
 *
 * Email sent to customer as a reminder about warranty expiration.
 */
class WarrantyReminder extends WC_Email {

	/**
	 * Order object
	 *
	 * @var WC_Order
	 */
	public WC_Order $order;

	/**
	 * Warranty expiration date
	 *
	 * @var string
	 */
	public string $expiration_date;

	/**
	 * Days until expiration
	 *
	 * @var int
	 */
	public int $days_remaining;

	/**
	 * Constructor
	 */
	public function __construct() {
		$this->id                     = 'warranty_reminder';
		$this->title                  = __( 'Warranty Expiration Reminder', 'warranty-cart' );
		$this->description            = __( 'Email sent to customer as a reminder before warranty expires.', 'warranty-cart' );
		$this->template_base          = WARRANTY_CART_PATH . 'templates/';
		$this->template_html          = 'emails/warranty-reminder.php';
		$this->template_plain         = 'emails/plain/warranty-reminder.php';
		$this->template_block_content = 'emails/block/warranty-reminder.php';
		$this->customer_email         = true;

		parent::__construct();

		$this->template_base = WARRANTY_CART_PATH . 'templates/';
		$this->placeholders  = array_merge(
			$this->placeholders,
			array(
				'{order_date}'   => '',
				'{order_number}' => '',
			),
		);
	}

	/**
	 * '{customer_name}'   => '',
	 * '{expiration_date}' => '',
	 * '{days_remaining}'  => '',
	 * )
	 * );
	 * }
	 *
	 * /**
	 * Get email subject
	 *
	 * @return string
	 */
	public function get_default_subject() {
		return __( 'Your warranty is expiring soon - Order #{order_number}', 'warranty-cart' );
	}

	/**
	 * Get email heading
	 *
	 * @return string
	 */
	public function get_default_heading() {
		return __( 'Warranty Expiration Reminder', 'warranty-cart' );
	}

	/**
	 * Trigger email
	 *
	 * @param int    $order_id        Order ID.
	 * @param string $expiration_date Expiration date.
	 * @param int    $days_remaining  Days until expiration.
	 */
	public function trigger( int $order_id, string $expiration_date = '', int $days_remaining = 30 ): void {
		$this->setup_locale();

		if ( $order_id ) {
			$order = wc_get_order( $order_id );

			if ( $order instanceof WC_Order ) {
				$this->order           = $order;
				$this->expiration_date = $expiration_date;
				$this->days_remaining  = $days_remaining;
				$this->recipient       = $this->order->get_billing_email();

				$this->placeholders['{order_date}']      = wc_format_datetime( $this->order->get_date_created() );
				$this->placeholders['{order_number}']    = $this->order->get_order_number();
				$this->placeholders['{customer_name}']   = $this->order->get_billing_first_name();
				$this->placeholders['{expiration_date}'] = $expiration_date ? date_i18n( get_option( 'date_format' ), strtotime( $expiration_date ) ) : '';
				$this->placeholders['{days_remaining}']  = $days_remaining;
			}
		}

		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 content html
	 *
	 * @return string
	 */
	public function get_content_html() {
		return wc_get_template_html(
			$this->template_html,
			array(
				'order'              => $this->order,
				'expiration_date'    => $this->expiration_date,
				'days_remaining'     => $this->days_remaining,
				'email_heading'      => $this->get_heading(),
				'additional_content' => $this->get_additional_content(),
				'sent_to_admin'      => false,
				'plain_text'         => false,
				'email'              => $this,
			),
			'',
			$this->template_base
		);
	}

	/**
	 * Get content plain
	 *
	 * @return string
	 */
	public function get_content_plain() {
		return wc_get_template_html(
			$this->template_plain,
			array(
				'order'              => $this->order,
				'expiration_date'    => $this->expiration_date,
				'days_remaining'     => $this->days_remaining,
				'email_heading'      => $this->get_heading(),
				'additional_content' => $this->get_additional_content(),
				'sent_to_admin'      => false,
				'plain_text'         => true,
				'email'              => $this,
			),
			'',
			$this->template_base
		);
	}

	/**
	 * Get block editor email template content.
	 *
	 * @return string
	 */
	public function get_block_editor_email_template_content() {
		return wc_get_template_html(
			$this->template_block_content,
			array(
				'order'              => $this->order,
				'expiration_date'    => $this->expiration_date,
				'days_remaining'     => $this->days_remaining,
				'email_heading'      => $this->get_heading(),
				'additional_content' => $this->get_additional_content(),
				'sent_to_admin'      => false,
				'plain_text'         => true,
				'email'              => $this,
			),
			'',
			$this->template_base
		);
	}

	/**
	 * Get attachments
	 *
	 * Returns email attachments (none for reminder emails).
	 *
	 * @return array Array of attachment file paths.
	 */
	public function get_attachments() {
		return array(); // No attachments for reminder emails
	}

	/**
	 * Initialise settings form fields
	 *
	 * Sets up additional form fields for email settings.
	 */
	public function init_form_fields() {
		parent::init_form_fields();

		$this->form_fields['reminder_days'] = array(
			'title'             => __( 'Reminder Days', 'warranty-cart' ),
			'type'              => 'number',
			'desc_tip'          => true,
			'description'       => __( 'Number of days before expiration to send reminder.', 'warranty-cart' ),
			'default'           => 30,
			'custom_attributes' => array(
				'min' => 1,
				'max' => 365,
			),
		);

		$this->form_fields['enable_reminder'] = array(
			'title'   => __( 'Enable Reminder', 'warranty-cart' ),
			'type'    => 'checkbox',
			'label'   => __( 'Send reminder emails before warranty expiration', 'warranty-cart' ),
			'default' => 'yes',
		);
	}
}
