<?php
/**
 * Class Noesia Cs Order Care Hub Module.
 *
 * @package Noesia_Commerce_Suite
 */

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

/**
 * Class implementation.
 */
class Noesia_CS_Order_Care_Hub_Module extends Noesia_CS_Admin_Module {
	/**
	 * Handles the related plugin operation.
	 */
	public function __construct() {
		$this->key         = 'order-care-hub';
		$this->label       = __( 'Order Care Hub', 'noesia-commerce-suite' );
		$this->icon        = '◈';
		$this->description = __( 'Post-purchase customer hub, core actions, smart links and QR access.', 'noesia-commerce-suite' );
		$this->view        = 'modules/order-care-hub/views/admin.php';
		$this->primary     = true;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $context Context.
	 */
	public function prepare_data( $context = array() ) {
		$passport_service  = $this->service( 'passport_service' );
		$admin             = isset( $context['admin'] ) ? $context['admin'] : null;
		$orders            = $passport_service && method_exists( $passport_service, 'get_recent_orders' ) ? $passport_service->get_recent_orders( 12 ) : array();
		$rows              = array();

		foreach ( $orders as $order ) {
			if ( ! is_object( $order ) || ! method_exists( $order, 'get_id' ) ) {
				continue;
			}

			$order_id     = absint( $order->get_id() );
			$has_token    = $passport_service && method_exists( $passport_service, 'order_has_active_token' ) ? $passport_service->order_has_active_token( $order ) : false;
			$url          = ( $has_token && $passport_service && method_exists( $passport_service, 'get_existing_care_url_for_order' ) ) ? $passport_service->get_existing_care_url_for_order( $order ) : '';
			$review_ready = $this->order_has_public_review_box( $order );

			$rows[] = array(
				'order_id'        => $order_id,
				'reference'       => $passport_service && method_exists( $passport_service, 'get_safe_order_reference' ) ? $passport_service->get_safe_order_reference( $order ) : '#' . $order_id,
				'date'            => $order->get_date_created() ? $order->get_date_created()->date_i18n( get_option( 'date_format' ) ) : '',
				'customer_label'  => $admin && method_exists( $admin, 'safe_customer_label' ) ? $admin->safe_customer_label( $order ) : __( 'Customer', 'noesia-commerce-suite' ),
				'has_token'       => (bool) $has_token,
				'review_ready'    => (bool) $review_ready,
				'url'             => $url,
				'open_url'        => $admin && method_exists( $admin, 'open_care_page_action_url' ) ? $admin->open_care_page_action_url( $order_id ) : '',
				'generate_url'    => wp_nonce_url( admin_url( 'admin-post.php?action=noesia_cs_generate_token&order_id=' . $order_id ), 'noesia_cs_generate_token_' . $order_id ),
				'print_url'       => $admin && method_exists( $admin, 'printable_qr_url' ) ? $admin->printable_qr_url( $order_id ) : '',
			);
		}

		$smart_links = array(
			'warranty'   => $this->smart_link_is_active( 'order_care_warranty' ),
			'documents'  => $this->smart_link_is_active( 'order_care_documents' ),
			'video'      => $this->smart_link_is_active( 'order_care_video' ),
			'promo'      => 'yes' === $this->setting( 'order_care_promo_enabled', 'no' ) && ( '' !== trim( (string) $this->setting( 'order_care_promo_text', '' ) ) || '' !== trim( (string) $this->setting( 'order_care_promo_code', '' ) ) ),
		);

		return array(
			'passport_rows'  => $rows,
			'qr_settings'    => array(
				'size'           => absint( $this->setting( 'qr_size', 160 ) ),
				'foreground'     => sanitize_hex_color( $this->setting( 'qr_foreground_color', '#111827' ) ) ?: '#111827',
				'background'     => sanitize_hex_color( $this->setting( 'qr_background_color', '#ffffff' ) ) ?: '#ffffff',
				'enabled'        => 'yes' === $this->setting( 'enable_product_passport_qr', 'yes' ),
				'printable'      => 'yes' === $this->setting( 'allow_printable_qr', 'no' ),
			),
			'hub_status'   => array(
				'enabled'       => 'yes' === $this->setting( 'enable_care_page', 'yes' ),
				'thankyou'      => 'yes' === $this->setting( 'show_qr_thankyou', 'yes' ),
				'email'         => 'yes' === $this->setting( 'show_qr_emails', 'no' ) || 'yes' === $this->setting( 'passport_email_enabled', 'no' ),
				'my_account'    => 'yes' === $this->setting( 'show_qr_my_account', 'yes' ),
				'package_qr'    => 'yes' === $this->setting( 'allow_printable_qr', 'no' ),
				'smart_links'   => count( array_filter( $smart_links ) ),
				'recent_links'  => count( $rows ),
			),
			'smart_links'   => $smart_links,
		);
	}


	/**
	 * Checks whether a public Care Hub for an order can show the review box.
	 *
	 * @param mixed $order Order.
	 */
	private function order_has_public_review_box( $order ) {
		if ( ! $order || ! is_a( $order, 'WC_Order' ) ) {
			return false;
		}

		if ( 'yes' !== $this->setting( 'show_review_button', 'yes' ) || 'yes' !== $this->setting( 'enable_review_block', 'yes' ) ) {
			return false;
		}

		if ( in_array( $order->get_status(), array( 'cancelled', 'refunded', 'failed', 'checkout-draft' ), true ) ) {
			return false;
		}

		return '' !== trim( (string) $this->setting( 'google_review_url', '' ) )
			|| '' !== trim( (string) $this->setting( 'trustpilot_url', '' ) )
			|| '' !== trim( (string) $this->setting( 'store_review_url', '' ) );
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $prefix Prefix.
	 */
	private function smart_link_is_active( $prefix ) {
		return 'yes' === $this->setting( $prefix . '_enabled', 'no' ) && '' !== trim( (string) $this->setting( $prefix . '_url', '' ) );
	}
}
