<?php
/**
 * Dashboard Template
 *
 * Builds the dashboard configuration and renders it through the shared devdiggers-framework
 * dashboard builder ( DDFW_Dashboard ). The plugin only supplies data + config.
 *
 * @package MultiPOS - Point of Sale for WooCommerce
 * @version 1.0.0
 */

namespace DDWCMultiPOS\Templates\Admin\Dashboard;

use DDWCMultiPOS\Helper\Dashboard\DDWCPOS_Dashboard_Helper;
use DevDiggers\Framework\Includes\DDFW_Dashboard;

defined( 'ABSPATH' ) || exit();

if ( ! class_exists( 'DDWCPOS_Dashboard_Template' ) ) {
	/**
	 * Dashboard template class
	 */
	class DDWCPOS_Dashboard_Template {
		/**
		 * Configuration Variable
		 *
		 * @var array
		 */
		protected $ddwcpos_configuration;

		/**
		 * Dashboard Helper Variable
		 *
		 * @var DDWCPOS_Dashboard_Helper
		 */
		protected $dashboard_helper;

		/**
		 * Dashboard Data Variable
		 *
		 * @var array
		 */
		protected $dashboard_data;

		/**
		 * Construct
		 *
		 * @param array $ddwcpos_configuration Plugin configuration.
		 */
		public function __construct( $ddwcpos_configuration ) {
			$this->ddwcpos_configuration = $ddwcpos_configuration;

			if ( ! class_exists( '\\DevDiggers\\Framework\\Includes\\DDFW_Dashboard' ) ) {
				return;
			}

			$this->dashboard_helper = new DDWCPOS_Dashboard_Helper( $ddwcpos_configuration );
			$this->dashboard_data   = $this->dashboard_helper->get_dashboard_data();

			$this->render();
		}

		/**
		 * Render dashboard via the shared framework builder.
		 *
		 * @return void
		 */
		protected function render() {
			$data       = $this->dashboard_data;
			$summary    = $data['summary'];
			$date_label = $data['date_range']['label'];

			$top_payment = ! empty( $data['charts']['payment_methods'] )
				? $data['charts']['payment_methods'][0]['payment_method']
				: esc_html__( 'N/A', 'devdiggers-multipos-for-woocommerce' );

			new DDFW_Dashboard(
				[
					'columns' => 4,
					'header'  => [
						/* translators: %s: current user display name. */
						'welcome'  => esc_html__( 'Welcome back, %s! 👋🏻', 'devdiggers-multipos-for-woocommerce' ),
						'subtitle' => esc_html__( 'Here\'s what\'s happening with your POS stores.', 'devdiggers-multipos-for-woocommerce' ),
					],
					'summary_cards' => [
						[
							'title'       => esc_html__( 'Total Orders', 'devdiggers-multipos-for-woocommerce' ),
							'value'       => $summary['total_orders']['value'],
							'change'      => $summary['total_orders']['change'],
							'is_positive' => $summary['total_orders']['is_positive'],
							'icon'        => '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M6 2L3 6v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2V6l-3-4H6Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M3 6h18M16 10a4 4 0 0 1-8 0" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
						],
						[
							'title'       => esc_html__( 'Total Revenue', 'devdiggers-multipos-for-woocommerce' ),
							'value'       => wc_price( $summary['total_revenue']['value'] ),
							'change'      => $summary['total_revenue']['change'],
							'is_positive' => $summary['total_revenue']['is_positive'],
							'value_type'  => 'html',
							'icon'        => '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M12 2v20M17 5H9.5a3.5 3.5 0 0 0 0 7h5a3.5 3.5 0 0 1 0 7H6" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
						],
						[
							'title' => esc_html__( 'Cashiers', 'devdiggers-multipos-for-woocommerce' ),
							'value' => $summary['total_cashiers'],
							'icon'  => '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><circle cx="9" cy="7" r="4" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M23 21v-2a4 4 0 0 0-3-3.87M16 3.13a4 4 0 0 1 0 7.75" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
						],
						[
							'title'      => esc_html__( 'Top Payment', 'devdiggers-multipos-for-woocommerce' ),
							'value'      => $top_payment,
							'value_type' => 'text',
							'icon'       => '<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><rect x="2" y="5" width="20" height="14" rx="2" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/><path d="M2 10h20" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>',
						],
					],
					'charts' => [
						[
							'id'           => 'revenue',
							'title'        => esc_html__( 'Revenue Overview', 'devdiggers-multipos-for-woocommerce' ),
							'date_label'   => $date_label,
							'full_width'   => true,
							'type'         => 'line',
							'value_format' => 'currency',
							'data'         => $data['charts']['revenue_chart'],
							'x_key'        => 'date',
							'series'       => [
								[ 'key' => 'revenue', 'label' => esc_html__( 'Revenue', 'devdiggers-multipos-for-woocommerce' ), 'color' => '#0256ff' ],
							],
							'empty'        => [
								'title' => esc_html__( 'No revenue data', 'devdiggers-multipos-for-woocommerce' ),
								'desc'  => esc_html__( 'Revenue will appear here once POS orders are placed.', 'devdiggers-multipos-for-woocommerce' ),
							],
						],
					],
					'widgets' => [
						[
							'title' => esc_html__( 'Payment Methods', 'devdiggers-multipos-for-woocommerce' ),
							'width' => 'half',
							'chart' => [
								'id'        => 'payment-methods',
								'type'      => 'doughnut',
								'data'      => $data['charts']['payment_methods'],
								'label_key' => 'payment_method',
								'value_key' => 'amount',
								'empty'     => [
									'title' => esc_html__( 'No payment data', 'devdiggers-multipos-for-woocommerce' ),
									'desc'  => esc_html__( 'Payment method split will appear here.', 'devdiggers-multipos-for-woocommerce' ),
								],
							],
						],
						[
							'title'  => esc_html__( 'Recent POS Orders', 'devdiggers-multipos-for-woocommerce' ),
							'width'  => 'half',
							'render' => [ $this, 'render_recent_orders' ],
						],
					],
				]
			);
		}

		/**
		 * Render the recent POS orders widget body.
		 *
		 * @return void
		 */
		public function render_recent_orders() {
			$orders = $this->dashboard_data['recent_orders'];

			if ( empty( $orders ) ) {
				?>
				<div class="ddfw-dash-no-data"><?php esc_html_e( 'No recent orders found.', 'devdiggers-multipos-for-woocommerce' ); ?></div>
				<?php
				return;
			}
			?>
			<div class="ddfw-dash-list">
				<?php foreach ( $orders as $order ) : ?>
					<div class="ddfw-dash-list-item">
						<div class="ddfw-dash-list-info">
							<div class="ddfw-dash-list-title">
								<a href="<?php echo esc_url( admin_url( 'post.php?post=' . $order['id'] . '&action=edit' ) ); ?>"><strong>#<?php echo esc_html( $order['number'] ); ?></strong></a>
								<span class="ddfw-dash-status-badge ddfw-dash-status-<?php echo esc_attr( strtolower( $order['status'] ) ); ?>"><?php echo esc_html( $order['status'] ); ?></span>
							</div>
							<div class="ddfw-dash-list-meta"><?php echo esc_html( $order['customer'] . ' • ' . $order['date'] ); ?></div>
						</div>
						<div class="ddfw-dash-list-value"><?php echo wp_kses_post( $order['total'] ); ?></div>
					</div>
				<?php endforeach; ?>
			</div>
			<?php
		}
	}
}
