<?php
/**
 * Email Scheduler
 *
 * @package PixelioCartAbandonRecoveryMore
 * @since 0.1
 */

namespace PixelioCartAbandonRecoveryMore\Admin\Services;

use PixelioCartAbandonRecoveryMore\Admin\Wrapper\Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Cart_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Email_Logs_Repository;
use PixelioCartAbandonRecoveryMore\Frontend\Repositories\Pixelio_Cart_Abandon_Recovery_More_Email_Templates_Repository;

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


class Pixelio_Cart_Abandon_Recovery_More_Email_Scheduler_Service
{


	private Pixelio_Cart_Abandon_Recovery_More_Wordpress_Wrapper $pixelio_wordpress_wrapper;
	private Pixelio_Cart_Abandon_Recovery_More_Cart_Repository $pixelio_cart_repo;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Logs_Repository $pixelio_cart_abandon_and_more_emails_logs_model;
	private Pixelio_Cart_Abandon_Recovery_More_Email_Templates_Repository $pixelio_cart_abandon_and_more_emails_to_send_email_template;

	public function __construct($pixelio_wordpress_wrapper, $pixelio_cart_repo, $pixelio_cart_abandon_and_more_emails_logs_model, $pixelio_cart_abandon_and_more_emails_to_send_email_template)
	{
		$this->pixelio_wordpress_wrapper = $pixelio_wordpress_wrapper;
		$this->pixelio_cart_repo = $pixelio_cart_repo;
		$this->pixelio_cart_abandon_and_more_emails_logs_model = $pixelio_cart_abandon_and_more_emails_logs_model;
		$this->pixelio_cart_abandon_and_more_emails_to_send_email_template = $pixelio_cart_abandon_and_more_emails_to_send_email_template;
	}

	/**
	 * Récupère les emails à envoyer
	 *
	 * @param int $pixelio_cart_abandon_and_more_type
	 * @param string $pixelio_cart_abandon_and_more_cart_status CSV des statuts de panier
	 * @return array
	 */
	public function pixelio_get_emails_to_send($pixelio_cart_abandon_and_more_type, $pixelio_cart_abandon_and_more_cart_status)
	{
		$pixelio_cart_abandon_and_more_now = $this->pixelio_wordpress_wrapper->pixelio_current_time();


		$pixelio_cart_abandon_and_more_all_templates = $this->pixelio_cart_abandon_and_more_emails_to_send_email_template->pixelio_get_all_non_deleted_emails_templates($pixelio_cart_abandon_and_more_type);


		if (empty($pixelio_cart_abandon_and_more_all_templates)) {

			return [];
		}
		if ((int) $pixelio_cart_abandon_and_more_type === 2) {
			// Review emails => commandes WooCommerce
			$pixelio_cart_abandon_and_more_carts = wc_get_orders([
				'status' => ['completed'],
				'limit'  => -1,
				'return' => 'objects',
			]);
		} else {
			// Abandon emails => carts internes
			$pixelio_cart_abandon_and_more_carts = $this->pixelio_cart_repo
				->pixelio_cart_abandon_recovery_more_get_cart_by_status($pixelio_cart_abandon_and_more_cart_status);
		}

		if (empty($pixelio_cart_abandon_and_more_carts)) {
			return [];
		}


		$pixelio_cart_abandon_and_more_email_logs = $this->pixelio_cart_abandon_and_more_emails_logs_model->pixelio_get_all_emails();

		$pixelio_cart_abandon_and_more_sent_keys = [];
		foreach ($pixelio_cart_abandon_and_more_email_logs as $pixelio_cart_abandon_and_more_email_log) {
			$pixelio_cart_abandon_and_more_sent_keys[$pixelio_cart_abandon_and_more_email_log->template_id . '-' . $pixelio_cart_abandon_and_more_email_log->cart_id] = true;
		}

		$pixelio_cart_abandon_and_more_emails_to_send = [];

		foreach ($pixelio_cart_abandon_and_more_all_templates as $pixelio_cart_abandon_and_more_all_template) {
			$pixelio_cart_abandon_and_more_send_after_seconds = intval($pixelio_cart_abandon_and_more_all_template['send_after']) * 60;

			foreach ($pixelio_cart_abandon_and_more_carts as $pixelio_cart_abandon_and_more_cart) {

				if ((int) $pixelio_cart_abandon_and_more_type === 2) {


					$pixelio_reference_date = ! empty( $pixelio_cart_abandon_and_more_cart->date_completed )
						? $pixelio_cart_abandon_and_more_cart->date_completed
						: $pixelio_cart_abandon_and_more_cart->date_created;

					$pixelio_item_id = $pixelio_cart_abandon_and_more_cart->get_id();
					$pixelio_order_id = $pixelio_cart_abandon_and_more_cart->get_id();
					$pixelio_uuid = '';

				} else {
					$pixelio_reference_date = ! empty($pixelio_cart_abandon_and_more_cart->updated_at)
						? $pixelio_cart_abandon_and_more_cart->updated_at
						: $pixelio_cart_abandon_and_more_cart->created_at;

					$pixelio_item_id = $pixelio_cart_abandon_and_more_cart->id;
					$pixelio_order_id = $pixelio_cart_abandon_and_more_cart->woo_order_id ?? '';
					$pixelio_uuid = $pixelio_cart_abandon_and_more_cart->uuid ?? '';
				}
				$pixelio_cart_abandon_and_more_unique_key = $pixelio_cart_abandon_and_more_all_template['id'] . '-' . $pixelio_cart_abandon_and_more_cart->id;


				if (isset($pixelio_cart_abandon_and_more_sent_keys[$pixelio_cart_abandon_and_more_unique_key])) {
					continue;
				}

				$pixelio_reference_ts = strtotime($pixelio_reference_date);

				if (false === $pixelio_reference_ts) {
					continue;
				}

				if (time() < $pixelio_reference_ts + $pixelio_cart_abandon_and_more_send_after_seconds) {
					continue;
				}

				$pixelio_cart_abandon_and_more_emails_to_send[] = [
					'subject' => $pixelio_cart_abandon_and_more_all_template['subject'] ?? '',
					'order_id' => $pixelio_order_id,
					'content' => $pixelio_cart_abandon_and_more_all_template['body_html'] ?? '',
					'email_id' => $pixelio_cart_abandon_and_more_all_template['id'],
					'uuid' => $pixelio_uuid,
					'language' => $pixelio_cart_abandon_and_more_all_template['language'] ?? 'en',
					'coupon_value' => $pixelio_cart_abandon_and_more_all_template['discount'] ?? '',
					'coupon_type' => $pixelio_cart_abandon_and_more_all_template['discount_type'] ?? 0,
					'coupon_expiration' => $pixelio_cart_abandon_and_more_all_template['discount_expiration_time'] ?? 0,
					'cart_id' => $pixelio_item_id, // in case of review, it contains the order_id of woocommerce
					'template_id' => $pixelio_cart_abandon_and_more_all_template['id'],
					'type_of_email' => $pixelio_cart_abandon_and_more_all_template['type_of_email'],
					'is_ia' => $pixelio_cart_abandon_and_more_all_template['generated_by_ia'] ?? 0,
					'sent_at' => $pixelio_cart_abandon_and_more_now,
				];

			}
		}


		if (!empty($pixelio_cart_abandon_and_more_emails_to_send)) {
			$this->pixelio_mark_emails_as_sent($pixelio_cart_abandon_and_more_emails_to_send);
		}

		return $pixelio_cart_abandon_and_more_emails_to_send;
	}


	/**
	 * Crée les logs et incrémente totalSent
	 */
	public function pixelio_mark_emails_as_sent($pixelio_cart_abandon_and_more_emails_to_send)
	{
		$this->pixelio_cart_abandon_and_more_emails_to_send_email_template->pixelio_increment_sent_stats_by_email($pixelio_cart_abandon_and_more_emails_to_send);
	}
}
