<?php
/**
 * Dashboard admin view.
 *
 * Displays stats cards, most wanted products, and recent activity.
 *
 * @package Waitlytics
 */

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

$wtly_dashboard = new WTLY_Dashboard();
$wtly_stats     = $wtly_dashboard->get_stats();
$wtly_products  = $wtly_dashboard->get_most_wanted_products( 10 );
$wtly_activity  = $wtly_dashboard->get_recent_activity( 10 );
?>

<div class="wtly-dashboard">

	<?php
	/**
	 * Fires at the top of the dashboard. The Pro build uses this to
	 * render the Pro welcome card.
	 */
	do_action( 'wtly_dashboard_top' );
	?>

	<!-- Quick Stats -->
	<div class="wtly-stats-grid">
		<div class="wtly-card wtly-stat-card">
			<h3><?php esc_html_e( 'Active Subscribers', 'waitlytics' ); ?></h3>
			<div class="wtly-stat-number"><?php echo esc_html( number_format_i18n( $wtly_stats['active_subscribers'] ) ); ?></div>
		</div>

		<div class="wtly-card wtly-stat-card">
			<h3><?php esc_html_e( 'Products Watched', 'waitlytics' ); ?></h3>
			<div class="wtly-stat-number"><?php echo esc_html( number_format_i18n( $wtly_stats['products_watched'] ) ); ?></div>
			<div class="wtly-stat-sub">
				<?php
				printf(
					/* translators: %d: number of out-of-stock products */
					esc_html__( 'Out of stock: %d', 'waitlytics' ),
					absint( $wtly_stats['out_of_stock_watched'] )
				);
				?>
			</div>
		</div>

		<div class="wtly-card wtly-stat-card">
			<h3><?php esc_html_e( 'Emails Sent', 'waitlytics' ); ?></h3>
			<div class="wtly-stat-number"><?php echo esc_html( number_format_i18n( $wtly_stats['emails_sent_this_month'] ) ); ?></div>
			<div class="wtly-stat-sub"><?php esc_html_e( 'This month', 'waitlytics' ); ?></div>
		</div>

		<div class="wtly-card wtly-stat-card">
			<h3><?php esc_html_e( 'Conversion Rate', 'waitlytics' ); ?></h3>
			<div class="wtly-stat-number"><?php echo esc_html( $wtly_stats['conversion_rate'] . '%' ); ?></div>
		</div>
	</div>

	<!-- Most Wanted Products -->
	<div class="wtly-card">
		<h2><?php esc_html_e( 'Most Wanted Products', 'waitlytics' ); ?></h2>
		<?php if ( ! empty( $wtly_products ) ) : ?>
			<table class="wp-list-table widefat fixed striped">
				<thead>
					<tr>
						<th class="wtly-col-rank">#</th>
						<th><?php esc_html_e( 'Product', 'waitlytics' ); ?></th>
						<th><?php esc_html_e( 'Subscribers', 'waitlytics' ); ?></th>
						<th><?php esc_html_e( 'Status', 'waitlytics' ); ?></th>
						<th><?php esc_html_e( 'Action', 'waitlytics' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php
					$wtly_rank = 1;
					foreach ( $wtly_products as $wtly_product ) :
						$wtly_status_label = 'outofstock' === $wtly_product->stock_status
							? __( 'Out of Stock', 'waitlytics' )
							: ( 'onbackorder' === $wtly_product->stock_status
								? __( 'On Backorder', 'waitlytics' )
								: sprintf(
									/* translators: %d: stock quantity */
									__( 'In Stock (%d)', 'waitlytics' ),
									(int) $wtly_product->stock_quantity
								)
							);

						$wtly_status_class = 'outofstock' === $wtly_product->stock_status ? 'wtly-status-oos' : 'wtly-status-instock';
						?>
						<tr>
							<td class="wtly-col-rank"><?php echo esc_html( $wtly_rank ); ?></td>
							<td><?php echo esc_html( $wtly_product->product_name ); ?></td>
							<td><?php echo esc_html( number_format_i18n( $wtly_product->sub_count ) ); ?></td>
							<td><span class="<?php echo esc_attr( $wtly_status_class ); ?>"><?php echo esc_html( $wtly_status_label ); ?></span></td>
							<td>
								<?php if ( $wtly_product->edit_link ) : ?>
									<a href="<?php echo esc_url( $wtly_product->edit_link ); ?>" class="button button-small">
										<?php esc_html_e( 'View', 'waitlytics' ); ?>
									</a>
								<?php endif; ?>
							</td>
						</tr>
						<?php
						$wtly_rank++;
					endforeach;
					?>
				</tbody>
			</table>
		<?php else : ?>
			<p class="wtly-empty-state"><?php esc_html_e( 'No subscriber data yet. Subscribers will appear once customers sign up for back-in-stock notifications.', 'waitlytics' ); ?></p>
		<?php endif; ?>
	</div>

	<!-- Recent Activity -->
	<div class="wtly-card">
		<h2><?php esc_html_e( 'Recent Activity', 'waitlytics' ); ?></h2>
		<?php if ( ! empty( $wtly_activity ) ) : ?>
			<ul class="wtly-activity-list">
				<?php foreach ( $wtly_activity as $wtly_item ) : ?>
					<li class="wtly-activity-item wtly-activity-<?php echo esc_attr( $wtly_item->type ); ?>">
						<span class="wtly-activity-message"><?php echo esc_html( $wtly_item->message ); ?></span>
						<span class="wtly-activity-time"><?php echo esc_html( human_time_diff( strtotime( $wtly_item->time ), current_time( 'timestamp' ) ) ); ?> <?php esc_html_e( 'ago', 'waitlytics' ); ?></span>
					</li>
				<?php endforeach; ?>
			</ul>
		<?php else : ?>
			<p class="wtly-empty-state"><?php esc_html_e( 'No recent activity.', 'waitlytics' ); ?></p>
		<?php endif; ?>
	</div>

</div>
