<?php
/**
 * Admin.
 *
 * @package Noesia_Commerce_Suite
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View templates receive local variables from their renderer.
$doctor    = isset( $doctor ) && is_array( $doctor ) ? $doctor : array();
$snapshot  = isset( $snapshot ) && is_array( $snapshot ) ? $snapshot : array();
$reorder_counts          = isset( $reorder_counts ) && is_array( $reorder_counts ) ? $reorder_counts : array( 'pending'                     => 0 );
$care_action_summary     = isset( $care_action_summary ) && is_array( $care_action_summary ) ? $care_action_summary : array( 'range_count'  => 0 );
$range_label              = isset( $range_label ) ? $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         = isset( $weather_forecast ) && is_array( $weather_forecast ) ? $weather_forecast : array();
$dashboard_top_products   = isset( $dashboard_top_products ) && is_array( $dashboard_top_products ) ? $dashboard_top_products : array();
$dashboard_product_range  = isset( $dashboard_product_range ) ? sanitize_key( $dashboard_product_range ) : 'all';
$recent_orders_preview    = isset( $recent_orders_preview ) && is_array( $recent_orders_preview ) ? $recent_orders_preview : array();
$customer_retention       = isset( $customer_retention ) && is_array( $customer_retention ) ? $customer_retention : array();
$format_money            = function ( $amount ) {
	return function_exists( 'wc_price' ) ? wp_strip_all_tags( wc_price( $amount ) ) : number_format_i18n( (float) $amount, 2 );
};
?>


<?php if ( function_exists( 'noesia_cs_is_pro_active' ) && ! noesia_cs_is_pro_active() ) : ?>
<section class="noesia-cs-dashboard-pro-unlock" aria-label="<?php esc_attr_e( 'Explore Noesia Commerce Suite Pro Edition home page', 'noesia-commerce-suite' ); ?>">
	<div>
		<span><?php esc_html_e( 'Explore Pro Edition Home', 'noesia-commerce-suite' ); ?></span>
		<strong><?php esc_html_e( 'See the separate Live Checkout Command Center experience.', 'noesia-commerce-suite' ); ?></strong>
		<p><?php esc_html_e( 'Free Core is active. The separate Pro Edition adds advanced live checkout workflows, AI-assisted suggestions, QR customer care tools, warranty rules, Doctor workflows, Customer QR Studio and extended WooCommerce operations.', 'noesia-commerce-suite' ); ?></p>
	</div>
	<a class="noesia-cs-primary-button" href="<?php echo esc_url( function_exists( 'noesia_cs_feature_service' ) ? noesia_cs_feature_service()->get_upgrade_url() : admin_url( 'admin.php?page=noesia-commerce-suite&tab=upgrade-pro' ) ); ?>"><?php esc_html_e( 'View Pro Edition', 'noesia-commerce-suite' ); ?></a>
</section>
<?php endif; ?>

<section class="noesia-cs-dashboard-toolbar">
	<div>
		<span><?php esc_html_e( 'Commerce overview', 'noesia-commerce-suite' ); ?></span>
		<h2><?php esc_html_e( 'Store performance and after-sales activity', 'noesia-commerce-suite' ); ?></h2>
		<p><?php esc_html_e( "Here's what's happening across Woo orders, passports, warranties, payments and care actions.", 'noesia-commerce-suite' ); ?></p>
	</div>
	<?php $admin->render_dashboard_date_filter( $stats ); ?>
</section>

<section class="noesia-cs-metrics noesia-cs-metrics--six">
	<?php $admin->revenue_metric_card( $stats, $format_money( $stats['total_revenue'] ), $range_label ); ?>
	<?php $admin->metric_card( __( 'Orders', 'noesia-commerce-suite' ), number_format_i18n( $stats['total_orders'] ), '●', $range_label, 'bag' ); ?>
	<?php $admin->metric_card( __( 'Average Order Value', 'noesia-commerce-suite' ), $format_money( $stats['aov'] ), '●', __( 'revenue divided by orders', 'noesia-commerce-suite' ), 'trend' ); ?>
	<?php $admin->metric_card( __( 'Order Care Hub Links', 'noesia-commerce-suite' ), number_format_i18n( $stats['passports'] ), '●', __( 'secure links in selected orders', 'noesia-commerce-suite' ), 'target' ); ?>
	<?php $admin->metric_card( __( 'Active Warranties', 'noesia-commerce-suite' ), number_format_i18n( $stats['active_warranties'] ), '●', __( 'active in selected orders', 'noesia-commerce-suite' ), 'shield' ); ?>
	<?php $admin->metric_card( __( 'Care Actions', 'noesia-commerce-suite' ), number_format_i18n( $care_action_summary['range_count'] ?? 0 ), '●', sprintf( /* translators: %s: dynamic value. */ __( '%s · care page clicks', 'noesia-commerce-suite' ), $range_label ), 'target' ); ?>
</section>

<section class="noesia-cs-grid noesia-cs-grid--main">
	<div class="noesia-cs-panel noesia-cs-panel--wide">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Revenue Overview', 'noesia-commerce-suite' ); ?></h2><span><?php echo esc_html( $range_label ); ?></span></div>
		<?php $admin->render_revenue_svg( $stats['revenue_series'] ); ?>
	</div>
	<div class="noesia-cs-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Order Status Overview', 'noesia-commerce-suite' ); ?></h2><span><?php echo esc_html( $range_label ); ?></span></div>
		<?php $admin->render_order_health( $stats ); ?>
	</div>
	<div class="noesia-cs-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Payment Intelligence', 'noesia-commerce-suite' ); ?></h2><a href="<?php echo esc_url( $admin->tab_url( 'intelligence-analytics' ) ); ?>"><?php esc_html_e( 'Open Analytics', 'noesia-commerce-suite' ); ?></a></div>
		<?php $admin->render_payment_preview( $stats['payment_methods_by_revenue'], $range_label ); ?>
	</div>
</section>

<section class="noesia-cs-grid noesia-cs-grid--cards">
	<?php $admin->module_panel( 'qr', __( 'Order Care Hub', 'noesia-commerce-suite' ), __( 'Branded post-purchase hub with review, reorder, support and secure QR access.', 'noesia-commerce-suite' ), number_format_i18n( $stats['passports'] ), __( 'secure links in range', 'noesia-commerce-suite' ), 'order-care-hub', __( 'Give every customer a clean after-sales destination after checkout.', 'noesia-commerce-suite' ) ); ?>
	<?php $admin->module_panel( 'reorder', __( 'Reorder Reminders', 'noesia-commerce-suite' ), __( 'Controlled reminder queue with opt-out protection and idempotent processing.', 'noesia-commerce-suite' ), number_format_i18n( $reorder_counts['pending'] ), __( 'pending reminders', 'noesia-commerce-suite' ), 'reorder-reminders', __( 'Bring customers back at the right replacement moment with controlled reminders.', 'noesia-commerce-suite' ) ); ?>
	<?php $admin->module_panel( 'email', __( 'Emails', 'noesia-commerce-suite' ), __( 'Order Care Hub email blocks and reorder reminders use WordPress mail only when enabled.', 'noesia-commerce-suite' ), ( 'yes' === $settings['email_integration'] ? __( 'Enabled', 'noesia-commerce-suite' ) : __( 'Disabled', 'noesia-commerce-suite' ) ), __( 'email integration', 'noesia-commerce-suite' ), 'emails', __( 'Control the professional image of customer communication from one place.', 'noesia-commerce-suite' ) ); ?>
</section>

<section class="noesia-cs-grid noesia-cs-grid--dashboard-addons">
	<div class="noesia-cs-panel noesia-cs-panel--dashboard-products">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Top Products', 'noesia-commerce-suite' ); ?></h2><a data-noesia-cs-tab-link="intelligence-analytics" href="<?php echo esc_url( $admin->tab_url( 'intelligence-analytics' ) ); ?>"><?php esc_html_e( 'View report', 'noesia-commerce-suite' ); ?></a></div>
		<?php $admin->render_dashboard_top_products( $dashboard_top_products, $dashboard_product_range, 'dashboard' ); ?>
	</div>
	<div class="noesia-cs-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Recent Orders', 'noesia-commerce-suite' ); ?></h2><a href="<?php echo esc_url( admin_url( 'edit.php?post_type=shop_order' ) ); ?>"><?php esc_html_e( 'View all', 'noesia-commerce-suite' ); ?></a></div>
		<?php $admin->render_recent_orders_preview( $recent_orders_preview ); ?>
	</div>
	<div class="noesia-cs-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Order Health', 'noesia-commerce-suite' ); ?></h2><span><?php echo esc_html( $range_label ); ?></span></div>
		<?php $admin->render_dashboard_order_health( $stats ); ?>
	</div>
	<div class="noesia-cs-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Customer Retention', 'noesia-commerce-suite' ); ?></h2><span><?php echo esc_html( $range_label ); ?></span></div>
		<?php $admin->render_customer_retention( $customer_retention, $range_label ); ?>
	</div>
</section>

<section class="noesia-cs-grid noesia-cs-grid--bottom">
	<div class="noesia-cs-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Intelligence & Analytics', 'noesia-commerce-suite' ); ?></h2><a href="<?php echo esc_url( $admin->tab_url( 'intelligence-analytics' ) ); ?>"><?php esc_html_e( 'View Report', 'noesia-commerce-suite' ); ?></a></div>
		<?php $admin->render_top_products( $stats['top_products_by_quantity'] ); ?>
	</div>
	<div class="noesia-cs-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Impact Map Snapshot', 'noesia-commerce-suite' ); ?></h2><a href="<?php echo esc_url( $admin->tab_url( 'impact-map' ) ); ?>"><?php esc_html_e( 'Open Map', 'noesia-commerce-suite' ); ?></a></div>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="noesia-cs-dashboard-impact-form" data-noesia-cs-impact-refresh="1">
			<?php wp_nonce_field( 'noesia_cs_refresh_impact_map' ); ?>
			<input type="hidden" name="action" value="noesia_cs_refresh_impact_map">
			<input type="hidden" name="noesia_cs_return_tab" value="dashboard">
			<input type="hidden" name="noesia_cs_impact_context" value="compact">
			<label><span><?php esc_html_e( 'Show by', 'noesia-commerce-suite' ); ?></span><select name="noesia_cs_impact_group_by"><option value="country" <?php selected( $snapshot['group_by'] ?? 'country', 'country' ); ?>><?php esc_html_e( 'Country', 'noesia-commerce-suite' ); ?></option><option value="city" <?php selected( $snapshot['group_by'] ?? 'country', 'city' ); ?>><?php esc_html_e( 'City', 'noesia-commerce-suite' ); ?></option><option value="region" <?php selected( $snapshot['group_by'] ?? 'country', 'region' ); ?>><?php esc_html_e( 'Region / State', 'noesia-commerce-suite' ); ?></option></select></label>
			<button class="noesia-cs-secondary-button" type="submit"><?php esc_html_e( 'Refresh', 'noesia-commerce-suite' ); ?></button>
		</form>
		<div data-noesia-cs-impact-target="1">
			<?php $admin->render_map_preview( $snapshot, $snapshot['group_by'] ?? 'country' ); ?>
		</div>
	</div>
	<div class="noesia-cs-panel">
		<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Customer Care Activity', 'noesia-commerce-suite' ); ?></h2><span><?php echo esc_html( $range_label ); ?></span><a href="<?php echo esc_url( $admin->tab_url( 'reviews-support' ) ); ?>"><?php esc_html_e( 'Manage', 'noesia-commerce-suite' ); ?></a></div>
		<?php $admin->render_support_preview( $care_action_summary, $range_label ); ?>
	</div>
</section>

<section class="noesia-cs-dashboard-weather-section">
	<?php $admin->render_weather_forecast( $weather_forecast ); ?>
</section>
