<?php
/**
 * Answyr — Insights (read-only metrics), split into Tickets + Chat.
 *
 * Always renders both sections; a surface whose module is off is shown dimmed
 * with an enable hint rather than hidden.
 *
 * @package Answyr
 */

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

$ss_desk_on = Answyr_Settings::module_enabled( 'desk' );
$ss_chat_on = Answyr_Settings::module_enabled( 'chat' );

$ss_by_status = Answyr_Desk_Store::count_by_status();
$ss_statuses  = Answyr_Desk_Store::statuses();
$ss_total     = array_sum( $ss_by_status );
$ss_open      = ( $ss_by_status['open'] ?? 0 ) + ( $ss_by_status['pending'] ?? 0 );
$ss_7d        = Answyr_Desk_Store::created_since( 7 );
$ss_30d       = Answyr_Desk_Store::created_since( 30 );
$ss_frt       = Answyr_Desk_Store::avg_first_response_minutes();
$ss_csat      = Answyr_Desk_Store::rating_summary();

// Chat metrics we can derive today: conversations that escalated into a ticket.
$ss_chat_total = Answyr_Desk_Store::count_by_channel( 'chat' );
$ss_chat_7d    = Answyr_Desk_Store::created_since( 7, 'chat' );
$ss_chat_30d   = Answyr_Desk_Store::created_since( 30, 'chat' );

$ss_settings_url = admin_url( 'admin.php?page=answyr-settings' );

/** Humanize a minutes figure into "Xh Ym" / "Xm". */
$ss_fmt_mins = static function ( ?float $mins ): string {
	if ( null === $mins ) {
		return '—';
	}
	$mins = (int) round( $mins );
	if ( $mins < 60 ) {
		/* translators: %d: minutes */
		return sprintf( _n( '%d min', '%d mins', $mins, 'answyr' ), $mins );
	}
	$h = intdiv( $mins, 60 );
	$m = $mins % 60;
	/* translators: 1: hours, 2: minutes */
	return 0 === $m ? sprintf( __( '%dh', 'answyr' ), $h ) : sprintf( __( '%1$dh %2$dm', 'answyr' ), $h, $m );
};

$ss_csat_pct = $ss_csat['count'] > 0 ? round( $ss_csat['positive'] / $ss_csat['count'] * 100 ) : null;
?>
<div class="wrap answyr-insights">
	<?php Answyr_Plugin::admin_header( __( 'Insights', 'answyr' ) ); ?>
	<p class="description"><?php esc_html_e( 'A quick read on your support — tickets and AI chat.', 'answyr' ); ?></p>

	<h2 class="nav-tab-wrapper answyr-insights-tabs">
		<a href="#answyr-tickets" class="nav-tab" data-answyr-tab="tickets">
			<?php esc_html_e( 'Tickets', 'answyr' ); ?>
			<?php
			if ( ! $ss_desk_on ) :
				?>
				<span class="answyr-tab-badge"><?php esc_html_e( 'Off', 'answyr' ); ?></span><?php endif; ?>
		</a>
		<a href="#answyr-chat" class="nav-tab" data-answyr-tab="chat">
			<?php esc_html_e( 'Chat', 'answyr' ); ?>
			<?php
			if ( ! $ss_chat_on ) :
				?>
				<span class="answyr-tab-badge"><?php esc_html_e( 'Off', 'answyr' ); ?></span><?php endif; ?>
		</a>
	</h2>

	<section class="answyr-insights-section<?php echo $ss_desk_on ? '' : ' answyr-section-off'; ?>" data-answyr-tab="tickets">
		<?php if ( ! $ss_desk_on ) : ?>
			<div class="answyr-section-disabled">
				<p>
					<?php esc_html_e( 'Ticketing is turned off, so these numbers are paused.', 'answyr' ); ?>
					<a href="<?php echo esc_url( $ss_settings_url . '#answyr-tickets' ); ?>"><?php esc_html_e( 'Enable Tickets in Settings', 'answyr' ); ?></a>
					<?php esc_html_e( 'to track volume, response time, and satisfaction.', 'answyr' ); ?>
				</p>
			</div>
		<?php else : ?>
			<div class="answyr-stat-grid">
				<div class="answyr-stat">
					<div class="answyr-stat-icon"><span class="dashicons dashicons-clock" aria-hidden="true"></span></div>
					<div class="answyr-stat-num"><?php echo (int) $ss_open; ?></div>
					<div class="answyr-stat-label"><?php esc_html_e( 'Awaiting us (open + pending)', 'answyr' ); ?></div>
				</div>
				<div class="answyr-stat">
					<div class="answyr-stat-icon"><span class="dashicons dashicons-tickets-alt" aria-hidden="true"></span></div>
					<div class="answyr-stat-num"><?php echo (int) $ss_total; ?></div>
					<div class="answyr-stat-label"><?php esc_html_e( 'Total tickets', 'answyr' ); ?></div>
				</div>
				<div class="answyr-stat">
					<div class="answyr-stat-icon"><span class="dashicons dashicons-chart-line" aria-hidden="true"></span></div>
					<div class="answyr-stat-num"><?php echo (int) $ss_7d; ?> <span class="answyr-stat-sub">/ <?php echo (int) $ss_30d; ?></span></div>
					<div class="answyr-stat-label"><?php esc_html_e( 'New: last 7 / 30 days', 'answyr' ); ?></div>
				</div>
				<div class="answyr-stat">
					<div class="answyr-stat-icon"><span class="dashicons dashicons-performance" aria-hidden="true"></span></div>
					<div class="answyr-stat-num"><?php echo esc_html( $ss_fmt_mins( $ss_frt ) ); ?></div>
					<div class="answyr-stat-label"><?php esc_html_e( 'Avg first response', 'answyr' ); ?></div>
				</div>
				<div class="answyr-stat">
					<div class="answyr-stat-icon"><span class="dashicons dashicons-heart" aria-hidden="true"></span></div>
					<div class="answyr-stat-num">
						<?php echo null === $ss_csat_pct ? '—' : esc_html( $ss_csat_pct . '%' ); ?>
					</div>
					<div class="answyr-stat-label">
						<?php
						/* translators: %d: number of ratings */
						echo esc_html( sprintf( _n( 'Satisfaction (%d rating)', 'Satisfaction (%d ratings)', $ss_csat['count'], 'answyr' ), $ss_csat['count'] ) );
						?>
					</div>
				</div>
			</div>

			<div class="answyr-card answyr-narrow">
				<h2><?php esc_html_e( 'By status', 'answyr' ); ?></h2>
				<table class="widefat striped">
					<tbody>
					<?php foreach ( $ss_statuses as $ss_key => $ss_label ) : ?>
						<?php $ss_n = $ss_by_status[ $ss_key ] ?? 0; ?>
						<tr>
							<td><span class="answyr-pill answyr-pill-<?php echo esc_attr( $ss_key ); ?>"><?php echo esc_html( $ss_label ); ?></span></td>
							<td><?php echo (int) $ss_n; ?></td>
							<td>
								<a href="<?php echo esc_url( admin_url( 'admin.php?page=answyr&status=' . $ss_key ) ); ?>"><?php esc_html_e( 'View', 'answyr' ); ?></a>
							</td>
						</tr>
					<?php endforeach; ?>
					</tbody>
				</table>
			</div>
		<?php endif; ?>
	</section>

	<section class="answyr-insights-section<?php echo $ss_chat_on ? '' : ' answyr-section-off'; ?>" data-answyr-tab="chat">
		<?php if ( ! $ss_chat_on ) : ?>
			<div class="answyr-section-disabled">
				<p>
					<?php esc_html_e( 'The AI chat widget is turned off.', 'answyr' ); ?>
					<a href="<?php echo esc_url( $ss_settings_url . '#answyr-chat' ); ?>"><?php esc_html_e( 'Enable Chat in Settings', 'answyr' ); ?></a>
					<?php esc_html_e( 'to deflect questions before they become tickets.', 'answyr' ); ?>
				</p>
			</div>
		<?php else : ?>
			<?php
			$ss_pro = function_exists( 'answyr_is_pro' ) && answyr_is_pro();
			// Premium-only partial; stripped from the free (WordPress.org) build.
			$ss_pro_partial = ANSWYR_PATH . 'inc/modules/chat/views/insights-chat-pro.php';
			?>
			<div class="answyr-stat-grid">
				<div class="answyr-stat">
					<div class="answyr-stat-icon"><span class="dashicons dashicons-migrate" aria-hidden="true"></span></div>
					<div class="answyr-stat-num"><?php echo (int) $ss_chat_7d; ?> <span class="answyr-stat-sub">/ <?php echo (int) $ss_chat_30d; ?></span></div>
					<div class="answyr-stat-label"><?php esc_html_e( 'Chats → tickets: last 7 / 30 days', 'answyr' ); ?></div>
				</div>
				<div class="answyr-stat">
					<div class="answyr-stat-icon"><span class="dashicons dashicons-format-chat" aria-hidden="true"></span></div>
					<div class="answyr-stat-num"><?php echo (int) $ss_chat_total; ?></div>
					<div class="answyr-stat-label"><?php esc_html_e( 'Chats escalated to tickets, all time', 'answyr' ); ?></div>
				</div>
			</div>

			<?php if ( $ss_pro && is_readable( $ss_pro_partial ) ) : ?>
				<?php require $ss_pro_partial; ?>
			<?php else : ?>
				<div class="answyr-stat-grid">
					<?php
					$ss_pro_teasers = array(
						array(
							'icon'  => 'dashicons-yes-alt',
							'label' => __( 'Deflection rate', 'answyr' ),
						),
						array(
							'icon'  => 'dashicons-lightbulb',
							'label' => __( 'Documentation gaps', 'answyr' ),
						),
						array(
							'icon'  => 'dashicons-editor-help',
							'label' => __( 'Top questions asked', 'answyr' ),
						),
					);
					foreach ( $ss_pro_teasers as $ss_teaser ) :
						?>
						<div class="answyr-stat answyr-stat-locked">
							<span class="answyr-pro-tag"><?php esc_html_e( 'Pro', 'answyr' ); ?></span>
							<div class="answyr-stat-icon"><span class="dashicons <?php echo esc_attr( $ss_teaser['icon'] ); ?>" aria-hidden="true"></span></div>
							<div class="answyr-stat-num" aria-hidden="true">•••</div>
							<div class="answyr-stat-label"><?php echo esc_html( $ss_teaser['label'] ); ?></div>
						</div>
					<?php endforeach; ?>
				</div>
				<p class="description answyr-pro-note">
					<span class="dashicons dashicons-lock" aria-hidden="true"></span>
					<?php esc_html_e( 'Answyr Pro adds conversation-level analytics — deflection rate, top questions, full transcripts, and a documentation-gap report showing exactly which questions your knowledge base failed to answer.', 'answyr' ); ?>
					<?php if ( answyr_pro_upgrade_available() ) : ?>
						<a class="button button-primary answyr-pro-upgrade" href="<?php echo esc_url( answyr_upgrade_url() ); ?>"><?php esc_html_e( 'Upgrade to Pro', 'answyr' ); ?></a>
					<?php endif; ?>
				</p>
				<?php $ss_license_url = function_exists( 'answyr_license_activation_url' ) ? answyr_license_activation_url() : ''; ?>
				<p class="description answyr-pro-note answyr-pro-haslicense">
					<span class="dashicons dashicons-admin-network" aria-hidden="true"></span>
					<?php if ( $ss_license_url ) : ?>
						<?php esc_html_e( 'Already purchased Answyr Pro?', 'answyr' ); ?>
						<a href="<?php echo esc_url( $ss_license_url ); ?>"><?php esc_html_e( 'Activate your license', 'answyr' ); ?></a>
						<?php esc_html_e( '— enter your key and the Pro version installs automatically.', 'answyr' ); ?>
					<?php else : ?>
						<?php esc_html_e( 'Already purchased Answyr Pro? Install the Answyr Pro plugin from your purchase confirmation email to activate your license — the Pro features live in that separate build.', 'answyr' ); ?>
					<?php endif; ?>
				</p>
			<?php endif; ?>
		<?php endif; ?>
	</section>
</div>
