<?php


namespace Elex\MultiVendor\Model;

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

class WoocommerceReportAdmin {


	public function get_order_report_on_sales( $from_date, $to_date ) {
	
		global $wpdb;

		new \WpFluent\Connection( $wpdb, array(), 'DB' );

		$results_com = $wpdb->get_results(
			$wpdb->prepare(
				"
			SELECT DATE(orders.date_created_gmt) as order_date, SUM(admin_commission.meta_value) as total_admin_commission
			FROM {$wpdb->prefix}wc_order_stats as orders
			INNER JOIN {$wpdb->prefix}wc_orders_meta as admin_commission ON orders.order_id = admin_commission.order_id AND admin_commission.meta_key = 'elex_mv_admin_commission_from_product'
			WHERE orders.status IN ('wc-processing', 'wc-completed') 
			AND orders.parent_id = 0
			AND DATE(orders.date_created_gmt) BETWEEN %s AND %s
			GROUP BY DATE(orders.date_created)
			",
				$from_date,
				$to_date
			)
		);

		$results_sales = $wpdb->get_results(
			$wpdb->prepare(
				"
			SELECT DATE(orders.date_created_gmt) as order_date, SUM(orders.total_sales) as total_sales_product
			FROM {$wpdb->prefix}wc_order_stats as orders
			WHERE orders.status IN ('wc-processing', 'wc-completed')
			AND orders.parent_id = 0
			AND DATE(orders.date_created_gmt) BETWEEN %s AND %s
			GROUP BY DATE(orders.date_created)
			",
				$from_date,
				$to_date
			)
		);    


		$date_range       = array();
		$date_range_sales = array();
		$from_date        = new \DateTime( $from_date );
		$end_date         = new \DateTime( $to_date );

		while ( $from_date <= $end_date ) {
			$date                      = $from_date->format( 'Y-m-d' );
			$date_range[ $date ]       = 0; // Initialize total_admin_commission to 0
			$date_range_sales[ $date ] = 0;
			$from_date->modify( '+1 day' );
		}


		foreach ( $results_com as $item ) {
			$date_range[ $item->order_date ] = $item;
		}

		foreach ( $results_sales as $item ) {
			$date_range_sales[ $item->order_date ] = $item;
		}


		// Join arrays a and b based on order_date
		$result = array();
		foreach ( $date_range_sales as $key => $item ) {
			$order_date = $key;
			if ( isset( $date_range[ $order_date ] ) ) {

				$total_admin_commission = 0;
				$total_sales_product    = 0;
				$total_vendor_earning   = 0;
				if ( is_object( $date_range[ $order_date ] ) ) {
					$total_admin_commission = $date_range[ $order_date ]->total_admin_commission ? $date_range[ $order_date ]->total_admin_commission : 0 ;
					$total_sales_product    = $item->total_sales_product ? $item->total_sales_product : 0;
					$total_vendor_earning   = $item->total_sales_product - $date_range[ $order_date ]->total_admin_commission ? $item->total_sales_product - $date_range[ $order_date ]->total_admin_commission : 0 ;
				} else {
					$total_admin_commission = 0 ;
					$total_sales_product    = isset( $item->total_sales_product ) ? $item->total_sales_product : 0;
					$total_vendor_earning   = isset( $item->total_sales_product ) ? $item->total_sales_product : 0;
				
				}
				$combinedItem = (object) array(
					'order_date'             => $order_date,
					'total_admin_commission' => $total_admin_commission,
					'total_sales_product'    => $total_sales_product,
					'total_vendor_earning'   => $total_vendor_earning,

				);
				$result[] = $combinedItem;
			}
		}



		foreach ( $result as &$item ) {
			$item = (array) $item;
		}
	
		return $result;
	}
}
