<?php
/**
 * Class Noesia Cs Intelligence Analytics Module.
 *
 * @package Noesia_Commerce_Suite
 */

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

/**
 * Class implementation.
 */
class Noesia_CS_Intelligence_Analytics_Module extends Noesia_CS_Admin_Module {
	/**
	 * Handles the related plugin operation.
	 */
	public function __construct() {
		$this->key         = 'intelligence-analytics';
		$this->label       = __( 'Intelligence & Analytics', 'noesia-commerce-suite' );
		$this->icon        = '╋';
		$this->description = __( 'Reliable WooCommerce intelligence from real orders.', 'noesia-commerce-suite' );
		$this->view        = 'modules/intelligence-analytics/views/admin.php';
		$this->primary     = false;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $context Context.
	 */
	public function prepare_data( $context = array() ) {
		$analytics_service   = $this->service( 'analytics_service' );
		$reorder_service     = $this->service( 'reorder_service' );
		$product_range       = $analytics_service && method_exists( $analytics_service, 'get_dashboard_product_range' ) ? $analytics_service->get_dashboard_product_range() : 'all';
		$customer_retention  = $analytics_service && method_exists( $analytics_service, 'get_customer_retention_summary' ) ? $analytics_service->get_customer_retention_summary() : array();

		if ( is_array( $customer_retention ) && $reorder_service && method_exists( $reorder_service, 'get_unique_pending_customer_count' ) ) {
			$customer_retention['reorder_candidates'] = $reorder_service->get_unique_pending_customer_count();
		}

		return array(
			'dashboard_product_range'  => $product_range,
			'dashboard_top_products'   => $analytics_service && method_exists( $analytics_service, 'get_dashboard_top_products' ) ? $analytics_service->get_dashboard_top_products( $product_range ) : array(),
			'recent_orders_preview'    => $analytics_service && method_exists( $analytics_service, 'get_recent_order_preview' ) ? $analytics_service->get_recent_order_preview( 5 ) : array(),
			'customer_retention'       => $customer_retention,
		);
	}
}
