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

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

/**
 * Class implementation.
 */
class Noesia_CS_Dashboard_Module extends Noesia_CS_Admin_Module {
	/**
	 * Handles the related plugin operation.
	 */
	public function __construct() {
		$this->key         = 'dashboard';
		$this->label       = __( 'Dashboard', 'noesia-commerce-suite' );
		$this->icon        = '⌂';
		$this->description = __( 'Store performance and after-sales activity.', 'noesia-commerce-suite' );
		$this->view        = 'modules/dashboard/views/admin.php';
		$this->primary     = false;
	}

	/**
	 * Handles the related plugin operation.
	 *
	 * @param mixed $context Context.
	 */
	public function prepare_data( $context = array() ) {
		$stats                = isset( $context['stats'] ) && is_array( $context['stats'] ) ? $context['stats'] : array();
		$doctor_service       = $this->service( 'doctor_service' );
		$impact_map_service   = $this->service( 'impact_map_service' );
		$reorder_service      = $this->service( 'reorder_service' );
		$care_action_service  = $this->service( 'care_action_service' );
		$weather_service      = $this->service( 'weather_service' );
		$analytics_service    = $this->service( 'analytics_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(
			'doctor'                    => $doctor_service && method_exists( $doctor_service, 'get_last_report' ) ? $doctor_service->get_last_report() : array(),
			'snapshot'                  => $impact_map_service && method_exists( $impact_map_service, 'get_snapshot' ) ? $impact_map_service->get_snapshot() : array(),
			'reorder_counts'            => $reorder_service && method_exists( $reorder_service, 'get_queue_counts' ) ? $reorder_service->get_queue_counts() : array( 'pending' => 0 ),
			'care_action_summary'       => $care_action_service && method_exists( $care_action_service, 'get_summary' ) ? $care_action_service->get_summary( $stats['range_start_ts'] ?? 0, $stats['range_end_ts'] ?? 0 ) : array( 'range_count' => 0 ),
			'range_label'               => isset( $stats['range_label'] ) ? $stats['range_label'] : sprintf( /* translators: %d: numeric value. */ __( 'last %d days', 'noesia-commerce-suite' ), absint( $stats['date_range_days'] ?? 30 ) ),
			'weather_forecast'          => $weather_service && method_exists( $weather_service, 'get_forecast' ) ? $weather_service->get_forecast() : array( 'enabled' => false, 'status' => 'unavailable', 'days' => 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,
		);
	}

}
