<?php
namespace PixelioCartAbandonRecoveryMore\Admin\Services;

use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper;
use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Cart_Repository;
use PixelioCartAbandonRecoveryMore\Includes\Utils\Pixelio_Cart_Abandon_Recovery_More_Util;

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

class Pixelio_Cart_Abandon_Recovery_More_After_Purchase_Service
{
	private Pixelio_Cart_Abandon_Recovery_More_Woocommerce_Wrapper $woocommerce_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_Util $pixelio_util;
	private Pixelio_Cart_Abandon_Recovery_More_Cart_Repository $cart_model;
	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $wordpress_wrapper;

	public function __construct($woocommerce_wrapper, $pixelio_util, $cart_model, $wordpress_wrapper)
	{
		$this->woocommerce_wrapper = $woocommerce_wrapper;
		$this->pixelio_util = $pixelio_util;
		$this->cart_model = $cart_model;
		$this->wordpress_wrapper = $wordpress_wrapper;
	}

	public function handle_after_purchase(int $order_id): void
	{
		if (!$order_id) {
			return;
		}

		if (!$this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked(
			'pixelio-cart-abandon-recovery-more-send-email-to-checkbox'
		)) {
			return;
		}

		$order = $this->woocommerce_wrapper->pixelio_wc_get_order($order_id);
		if (!$order) {
			return;
		}

		[$cart_uuid, $template_id] = $this->extract_uuid_and_template_from_order($order);

		if (!$cart_uuid) {
			return;
		}

		$cart_id = $this->cart_model->pixelio_find_by_uuid($cart_uuid);

		if ($cart_id) {
			$this->cart_model->pixelio_cart_abandon_recovery_more_update_cart_by_id(
				$cart_id,
				$order_id,
				22, // status ?
				$template_id
			);
			$this->notify_admin_cart_recovered($order_id);
		}
	}

	private function extract_uuid_and_template_from_order($order): array
	{
		$cart_uuid = null;
		$template_id = 0;

		foreach ($order->get_items() as $item) {
			$item_uuid = $item->get_meta('pixelio_uuid', true);
			if ($item_uuid) {
				$cart_uuid = $item_uuid;
			}

			$item_template = $item->get_meta('pixelio_email_template', true);
			if ($item_template) {
				$template_id = $item_template;
			}
		}

		return [$cart_uuid, $template_id];
	}

	private function notify_admin_cart_recovered(int $order_id): void
	{
		$admins = $this->wordpress_wrapper->pixelio_get_users(
			array(
				'role' => 'administrator',
			)
		);

		$subject = sprintf(
			$this->wordpress_wrapper->pixelio_esc_html('A new order has been recovered (#%d)'),
			$order_id
		);

		$message = sprintf(
			$this->wordpress_wrapper->pixelio_esc_html(
				'Hello,
				A new order (ID: #%d) has been successfully recovered on your website. Checkout how much you have recovered this month in the Rivio Dashboard.'
			),
			$order_id
		);
		if (!empty($admins) && $this->pixelio_util->pixelio_cart_abandon_recovery_more_dependency_is_checked('pixelio-cart-abandon-recovery-more-send-email-to-checkbox')) {

			$headers = array('Content-Type: text/html; charset=UTF-8');
			foreach ($admins as $admin) {
				$this->wordpress_wrapper->pixelio_wp_mail($admin->user_email, $subject, nl2br($message), $headers);
			}
		}
	}
}
