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

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- View templates receive local variables from their renderer.
$snapshot     = isset( $snapshot ) && is_array( $snapshot ) ? $snapshot : array();
$group_by     = isset( $group_by ) ? sanitize_key( $group_by ) : 'country';
$group_by     = in_array( $group_by, array( 'city', 'region', 'country' ), true ) ? $group_by : 'country';
$group_label  = isset( $group_label ) ? $group_label : $admin->impact_group_label( $group_by );
$areas        = isset( $areas ) && is_array( $areas ) ? $areas : $admin->prepare_impact_areas_for_display( $snapshot, $group_by, 10 );
$range_mode   = isset( $snapshot['range_mode'] ) ? sanitize_key( $snapshot['range_mode'] ) : 'all';
$range_from   = isset( $snapshot['range_from'] ) ? sanitize_text_field( $snapshot['range_from'] ) : '';
$range_to     = isset( $snapshot['range_to'] ) ? sanitize_text_field( $snapshot['range_to'] ) : '';
$map_asset  = isset( $map_asset ) && is_array( $map_asset ) ? $map_asset : array(
	'found'  => false,
	'path'   => 'assets/images/impact-map/world-map-dark.jpg',
	'file'   => '',
);
?>
<section class="noesia-cs-feature-hero noesia-cs-feature-hero--impact">
	<div>
		<span><?php esc_html_e( 'Free Core', 'noesia-commerce-suite' ); ?></span>
		<h2><?php esc_html_e( 'Cached Privacy-Safe Impact Map', 'noesia-commerce-suite' ); ?></h2>
		<p><?php esc_html_e( 'The public shortcode reads this cached snapshot. It does not scan WooCommerce orders on public page load.', 'noesia-commerce-suite' ); ?></p>
	</div>
	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="noesia-cs-impact-refresh-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_impact_context" value="admin-table">
		<div class="noesia-cs-impact-refresh-controls">
			<label>
				<span><?php esc_html_e( 'Group snapshot by', 'noesia-commerce-suite' ); ?></span>
				<select name="noesia_cs_impact_group_by">
					<option value="city" <?php selected( $group_by, 'city' ); ?>><?php esc_html_e( 'City', 'noesia-commerce-suite' ); ?></option>
					<option value="region" <?php selected( $group_by, 'region' ); ?>><?php esc_html_e( 'Region / State', 'noesia-commerce-suite' ); ?></option>
					<option value="country" <?php selected( $group_by, 'country' ); ?>><?php esc_html_e( 'Country', 'noesia-commerce-suite' ); ?></option>
				</select>
			</label>
			<label>
				<span><?php esc_html_e( 'Date range', 'noesia-commerce-suite' ); ?></span>
				<select name="noesia_cs_impact_date_mode">
					<option value="all" <?php selected( $range_mode, 'all' ); ?>><?php esc_html_e( 'All available orders', 'noesia-commerce-suite' ); ?></option>
					<option value="custom" <?php selected( $range_mode, 'custom' ); ?>><?php esc_html_e( 'Custom dates', 'noesia-commerce-suite' ); ?></option>
				</select>
			</label>
			<label>
				<span><?php esc_html_e( 'From', 'noesia-commerce-suite' ); ?></span>
				<input type="date" name="noesia_cs_impact_date_from" value="<?php echo esc_attr( $range_from ); ?>">
			</label>
			<label>
				<span><?php esc_html_e( 'To', 'noesia-commerce-suite' ); ?></span>
				<input type="date" name="noesia_cs_impact_date_to" value="<?php echo esc_attr( $range_to ); ?>">
			</label>
			<button class="noesia-cs-primary-button" type="submit"><?php esc_html_e( 'Refresh Snapshot', 'noesia-commerce-suite' ); ?></button>
		</div>
	</form>
</section>

<section class="noesia-cs-metrics noesia-cs-metrics--four">
	<?php $admin->metric_card( __( 'Visible Areas', 'noesia-commerce-suite' ), number_format_i18n( $snapshot['visible_areas'] ), '●', __( 'after privacy threshold', 'noesia-commerce-suite' ), 'target' ); ?>
	<?php $admin->metric_card( __( 'Orders Checked', 'noesia-commerce-suite' ), number_format_i18n( $snapshot['total_orders_considered'] ), '●', __( 'last snapshot', 'noesia-commerce-suite' ), 'bag' ); ?>
	<?php $admin->metric_card( __( 'Privacy Threshold', 'noesia-commerce-suite' ), number_format_i18n( $snapshot['threshold'] ), '●', __( 'minimum orders per area', 'noesia-commerce-suite' ), 'heart' ); ?>
	<?php $admin->metric_card( __( 'Grouping', 'noesia-commerce-suite' ), $group_label, '●', __( 'current snapshot mode', 'noesia-commerce-suite' ), 'trend' ); ?>
</section>

<div class="noesia-cs-panel noesia-cs-impact-shortcodes">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Public Shortcode', 'noesia-commerce-suite' ); ?></h2><span><?php esc_html_e( 'cached output only', 'noesia-commerce-suite' ); ?></span></div>
	<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Place this shortcode on the public page where you want to show the cached, privacy-safe Impact Map. The shortcode reads the saved snapshot and never scans WooCommerce orders on public page load.', 'noesia-commerce-suite' ); ?></p>
	<div class="noesia-cs-mini-grid">
		<div><strong>[noesia_impact_map]</strong><span><?php esc_html_e( 'Primary public shortcode', 'noesia-commerce-suite' ); ?></span></div>
	</div>
</div>

<div class="noesia-cs-panel noesia-cs-map-asset-note">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Map Visual Style', 'noesia-commerce-suite' ); ?></h2><span><?php echo ! empty( $map_asset['found'] ) ? esc_html__( 'local map asset available', 'noesia-commerce-suite' ) : esc_html__( 'local map asset optional', 'noesia-commerce-suite' ); ?></span></div>
	<p class="noesia-cs-panel__body-note"><?php esc_html_e( 'Choose the public Impact Map visual style in Settings. Dark World Map mode uses local plugin assets only; the plugin does not load external map APIs, map tiles or CDNs.', 'noesia-commerce-suite' ); ?></p>
	<div class="noesia-cs-mini-grid noesia-cs-map-asset-grid">
		<div><strong><?php echo ! empty( $map_asset['found'] ) ? esc_html__( 'Ready', 'noesia-commerce-suite' ) : esc_html__( 'Optional', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Dark World Map local asset status', 'noesia-commerce-suite' ); ?></span></div>
		<div><strong><?php echo esc_html( $map_asset['path'] ?? 'assets/images/impact-map/world-map-dark.jpg' ); ?></strong><span><?php esc_html_e( 'Local asset path used by Dark World Map mode', 'noesia-commerce-suite' ); ?></span></div>
		<div><strong><?php esc_html_e( '2400 × 1200 px', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Recommended size, 2:1 ratio', 'noesia-commerce-suite' ); ?></span></div>
		<div><strong><?php esc_html_e( 'JPG / WEBP / PNG', 'noesia-commerce-suite' ); ?></strong><span><?php esc_html_e( 'Supported local formats', 'noesia-commerce-suite' ); ?></span></div>
	</div>
</div>

<div class="noesia-cs-panel">
	<div class="noesia-cs-panel__head"><h2><?php esc_html_e( 'Top Impact Areas', 'noesia-commerce-suite' ); ?></h2><?php if ( $snapshot['generated_at'] ) : ?><span><?php echo esc_html( date_i18n( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), absint( $snapshot['generated_at'] ) ) ); ?> · <?php echo 'custom' === $range_mode && $range_from && $range_to ? esc_html( $range_from . ' → ' . $range_to ) : esc_html__( 'All available orders', 'noesia-commerce-suite' ); ?></span><?php endif; ?></div>
	<div data-noesia-cs-impact-target="1">
		<?php $admin->render_impact_admin_table( $snapshot, $group_by ); ?>
	</div>
</div>
<section class="noesia-cs-pro-grid">
	<?php $admin->pro_edition_card( __( '🔴 Live Radar Header Badge', 'noesia-commerce-suite' ), __( 'Show a visible live checkout badge in the Noesia header and jump straight to the active Radar.', 'noesia-commerce-suite' ), 'live_radar_header_badge' ); ?>
	<?php $admin->pro_edition_card( __( 'Operations Home', 'noesia-commerce-suite' ), __( 'Open one Pro command home for live checkout, orders, Doctor Pro, QR Studio, analytics and after-sales work.', 'noesia-commerce-suite' ), 'operations_home' ); ?>
	<?php $admin->pro_edition_card( __( 'Woo Admin Skin Pro', 'noesia-commerce-suite' ), __( 'Make selected WooCommerce admin pages cleaner, calmer and more premium for daily store work.', 'noesia-commerce-suite' ), 'woo_admin_skin' ); ?>
</section>
