<?php

/**
 * Renders the plugin's Dashboard page - offer counts, quick links, and (Pro) co-purchase offer suggestions.
 *
 * @link       https://nudgesells.com
 * @since      1.0.0
 *
 * @package    NudgeSells
 * @subpackage NudgeSells/admin
 */

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

/**
 * NudgeSells_Admin_Dashboard
 *
 * Renders the NudgeSells Dashboard page, summarizing offer activity and surfacing quick links and suggestions.
 *
 * @since      1.0.0
 * @package    NudgeSells
 * @subpackage NudgeSells/admin
 * @author     NudgeSells <support@nudgesells.com>
 */
class NudgeSells_Admin_Dashboard
{

	/**
	 * Render.
	 *
	 * @return void
	 * @since 1.0.0
	 */
	public static function render()
	{
		if (! current_user_can(NudgeSells_Admin::CAPABILITY)) {
			return;
		}

		$summary      = NudgeSells_Analytics_Data::get_summary(30);
		$active       = NudgeSells_Analytics_Data::get_offer_count('publish');
		$drafts       = NudgeSells_Analytics_Data::get_offer_count('draft');
		$new_url      = admin_url('post-new.php?post_type=' . NudgeSells_CPT::POST_TYPE);
		$list_url     = admin_url('edit.php?post_type=' . NudgeSells_CPT::POST_TYPE);
		$suggestions  = NudgeSells_Pro_Loader::is_active() ? NudgeSells_Pro_Suggestions_Data::get_suggestions(90, 5) : array();
?>
		<div class="wrap nudgesells-wrap">
			<?php
			NudgeSells_Admin::render_header(
				__('Dashboard', 'nudgesells'),
				'<a href="' . esc_url($new_url) . '" class="button button-primary">' . esc_html__('New Offer', 'nudgesells') . '</a>'
			);
			?>
			<p class="nudgesells-subtitle"><?php esc_html_e('A quick look at how your inline upsells are performing (last 30 days).', 'nudgesells'); ?></p>

			<div class="nudgesells-stat-grid">
				<div class="nudgesells-stat-card">
					<span class="nudgesells-stat-value"><?php echo wp_kses_post(wc_price($summary['total_extra_revenue'])); ?></span>
					<span class="nudgesells-stat-label"><?php esc_html_e('Extra revenue from add-ons & upgrades', 'nudgesells'); ?></span>
				</div>
				<div class="nudgesells-stat-card">
					<span class="nudgesells-stat-value"><?php echo esc_html($summary['orders_with_upsell']); ?></span>
					<span class="nudgesells-stat-label"><?php esc_html_e('Orders including an upsell', 'nudgesells'); ?></span>
				</div>
				<div class="nudgesells-stat-card">
					<span class="nudgesells-stat-value"><?php echo wp_kses_post(wc_price($summary['total_saved'])); ?></span>
					<span class="nudgesells-stat-label"><?php esc_html_e('Total customer savings given', 'nudgesells'); ?></span>
				</div>
				<div class="nudgesells-stat-card">
					<span class="nudgesells-stat-value"><?php echo esc_html($active); ?></span>
					<span class="nudgesells-stat-label"><?php esc_html_e('Active offers', 'nudgesells'); ?></span>
				</div>
			</div>

			<?php if (NudgeSells_Pro_Loader::is_active()) : ?>
				<div class="nudgesells-card">
					<h2><?php esc_html_e('Offer suggestions', 'nudgesells'); ?></h2>
					<p class="description"><?php esc_html_e('Product pairs your customers already buy together (last 90 days) that don\'t have a offer connecting them yet.', 'nudgesells'); ?></p>
					<?php if (! empty($suggestions)) : ?>
						<ul class="nudgesells-suggestion-list">
							<?php foreach ($suggestions as $suggestion) :
								$create_url = add_query_arg(
									array(
										'post_type'         => NudgeSells_CPT::POST_TYPE,
										'nudgesells_suggest_host'  => $suggestion['product_a_id'],
										'nudgesells_suggest_addon' => $suggestion['product_b_id'],
									),
									admin_url('post-new.php')
								);
							?>
								<li class="nudgesells-suggestion-row">
									<div class="nudgesells-suggestion-text">
										<strong><?php echo esc_html($suggestion['product_a_name']); ?></strong>
										<span class="nudgesells-suggestion-plus">+</span>
										<strong><?php echo esc_html($suggestion['product_b_name']); ?></strong>
										<span class="description">
											<?php
											printf(
												/* translators: %d: number of orders these products were bought together in */
												esc_html(_n('bought together in %d order', 'bought together in %d orders', $suggestion['order_count'], 'nudgesells')),
												(int) $suggestion['order_count']
											);
											?>
										</span>
									</div>
									<a href="<?php echo esc_url($create_url); ?>" class="button button-secondary"><?php esc_html_e('Create Offer', 'nudgesells'); ?></a>
								</li>
							<?php endforeach; ?>
						</ul>
					<?php else : ?>
						<p class="description">
							<?php
							printf(
								/* translators: %d: number of required orders that contain similar products */
								esc_html__('No frequently bought product sets found yet. The current setting requires products to have been bought together in at least %d order(s) - adjust this in Settings → Advanced if you\'d like more (or fewer) suggestions to show up here.', 'nudgesells'),
								(int) NudgeSells_Admin_Settings::get('suggestion_min_orders', 5)
							);
							?>
						</p>
					<?php endif; ?>
				</div>
			<?php endif; ?>

			<?php if (0 === $active && 0 === $drafts) : ?>
				<div class="nudgesells-empty-state">
					<span class="dashicons dashicons-megaphone"></span>
					<h2><?php esc_html_e('Create your first offer', 'nudgesells'); ?></h2>
					<p><?php esc_html_e('Offer add-ons, bundle discounts, tiered quantity discounts, or paid upgrades directly above the Add to Cart button.', 'nudgesells'); ?></p>
					<a href="<?php echo esc_url($new_url); ?>" class="button button-primary button-hero"><?php esc_html_e('New Offer', 'nudgesells'); ?></a>
				</div>
			<?php else : ?>
				<div class="nudgesells-card">
					<h2><?php esc_html_e('Quick links', 'nudgesells'); ?></h2>
					<ul class="nudgesells-quicklinks">
						<li>
							<a href="<?php echo esc_url($list_url); ?>"><?php esc_html_e('Manage Offers', 'nudgesells'); ?></a>
							<span class="description">
								<?php
								printf(
									/* translators: 1: number of active offers, 2: number of draft offers */
									esc_html__('%1$d active, %2$d draft', 'nudgesells'),
									(int) $active,
									(int) $drafts
								);
								?>
							</span>
						</li>
						<li>
							<a href="<?php echo esc_url(admin_url('admin.php?page=' . NudgeSells_Admin::SLUG_ANALYTICS)); ?>"><?php esc_html_e('View full Analytics', 'nudgesells'); ?></a>
							<span class="description"><?php esc_html_e('Revenue & savings breakdown', 'nudgesells'); ?></span>
						</li>
						<li>
							<a href="<?php echo esc_url(admin_url('admin.php?page=' . NudgeSells_Admin::SLUG_SETTINGS)); ?>"><?php esc_html_e('Plugin Settings', 'nudgesells'); ?></a>
							<span class="description"><?php esc_html_e('Global on/off, defaults', 'nudgesells'); ?></span>
						</li>
						<?php if (NudgeSells_Pro_Loader::is_active()) : ?>
							<li>
								<a href="<?php echo esc_url(admin_url('edit.php?post_type=nudgesells_ab_test')); ?>"><?php esc_html_e('A/B Tests', 'nudgesells'); ?></a>
								<span class="description"><?php esc_html_e('Compare two offer variants', 'nudgesells'); ?></span>
							</li>
							<li>
								<a href="<?php echo esc_url(admin_url('admin.php?page=' . NudgeSells_Admin::SLUG_TOOLS)); ?>"><?php esc_html_e('Tools', 'nudgesells'); ?></a>
								<span class="description"><?php esc_html_e('Import/export offers', 'nudgesells'); ?></span>
							</li>
						<?php endif; ?>
					</ul>
				</div>
			<?php endif; ?>
		</div>
<?php
	}
}
