<?php
/**
 * A class that sends email through Postmark.
 *
 * @since 3.0.0
 */

declare( strict_types=1 );
namespace FeData\FastEvents\Mail\Provider;

use Exception;
use FeData\FastEvents\Mail\AbstractMailer;
use FeData\FastEvents\Utilities\JsonUtil;
use FeData\FastEvents\Utilities\PluginUtil;
use stdClass;
use WP_Error;
use WP_REST_Request;
use WP_REST_Response;

defined( 'ABSPATH' ) || exit;

/**
 * A class that sends email through Postmark.
 */
final class PostmarkMailer extends AbstractMailer {

	/**
	 * Send mail using the Postmark API.
	 *
	 * @since 1.0
	 * @since 1.9 Added event_id and order_id to the message.
	 * @since 2.5.0 Set provider id.
	 *
	 * @param stdClass $order   Order information.
	 * @param string   $bcc     Emailaddress of BBC receiver.
	 * @param string   $subject Subject of the email.
	 * @param string   $msg     HTML mail message.
	 * @param int      $em_type 0=Normal mail, 1=Confirmation email.
	 * @throws Exception If mail can't be sent.
	 */
	protected function provider_send( stdClass $order, string $bcc, string $subject, string $msg, int $em_type ): void {
		$this->provider_id = 'postmark';

		// Prepare the body.
		$data = array(
			'From'     => $this->get_sender_email( $order ),
			'To'       => $order->email,
			'Subject'  => $subject,
			'HtmlBody' => $msg,
			'TextBody' => wp_strip_all_tags( $msg ),
			'Metadata' => array(
				'Event' => absint( $order->event_id ),
				'Order' => absint( $order->id ),
				'Name'  => $order->name,
			),
		);

		// Is there a BCC?
		if ( ! empty( $bcc ) ) {
			$data['Bcc'] = $bcc;
		}

		// Send the message.
		$response = wp_safe_remote_post(
			$this->settings->mailer_property( 'server' ),
			array(
				'user-agent' => PluginUtil::get_user_agent(),
				'timeout'    => self::CONNECTION_TIMEOUT,
				'headers'    => array(
					'Accept'                  => 'application/json',
					'Content-Type'            => 'application/json',
					'X-Postmark-Server-Token' => $this->settings->mailer_property( 'api_key' ),
				),
				'body'       => JsonUtil::fast_events_json_encode( $data ),
			)
		);

		// Check for errors.
		$this->check_email_provider_api_error( $response, $order, $em_type );
	}

	/**
	 * Extract the email_id used by Postmark.
	 *
	 * @since 2.5.0
	 *
	 * @param array|WP_Error $response API response from provider.
	 */
	protected function set_email_id( WP_Error|array $response ): void {
		$json           = json_decode( wp_remote_retrieve_body( $response ) );
		$this->email_id = $json->{'MessageID'};
	}

	/**
	 * Process the Postmark webhook events.
	 *
	 * @since 1.9
	 * @since 1.9.1 Added spam complaints and manual suppression events
	 * @since 2.3.0 Use fast_events_json_encode.
	 * @since 2.5.0 All events are added to the email log, and error events are added to the errorlog as well.
	 *
	 * @param WP_REST_Request $data Request instance.
	 *
	 * @return WP_REST_Response
	 */
	public function webhook( WP_REST_Request $data ): WP_REST_Response {
		static $error_events = array( 'Bounce', 'SpamComplaint', 'SubscriptionChange', 'ManualSuppression' );
		$this->provider_id   = 'postmark';

		$json = json_decode( $data->get_body() );
		if ( json_last_error() !== JSON_ERROR_NONE ) {
			$this->logger->error(
				'JSON error: ' . json_last_error_msg() . PHP_EOL . PHP_EOL . $data->get_body(),
				array( 'type' => 'Email-' . $this->provider_id )
			);
			return rest_ensure_response( 'Ok' );
		}
		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
		$event_id = absint( $json->Metadata->Event ?? 0 );
		$order_id = absint( $json->Metadata->Order ?? 0 );
		unset( $json->Content ); // Remove the message content, it can be huge!
		if ( in_array( $json->RecordType, $error_events, true ) ) {
			$this->logger->info(
			/* translators: %s is the email provider event name, and the last %s is the provider email id */
				sprintf( __( 'Adding the "%1$s" event to email-id "%1$s": ', 'fast-events' ), $json->RecordType, $json->MessageID ),
				array(
					'type'     => 'Email-' . $this->provider_id,
					'event_id' => $event_id,
					'order_id' => $order_id,
					'event'    => $json,
				)
			);
		}

		$this->update_email_log( $json->MessageID, $json, $event_id, $order_id );
		// phpcs:enable

		return rest_ensure_response( 'Ok' );
	}

}
