<?php
/**
 * Dashboard view — analytics + recent conversations.
 *
 * @package AetherisAIChat
 *
 * @var array               $settings
 * @var array|WP_Error|null $stats
 * @var array|WP_Error|null $convos
 */
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}
$aetheris_has_creds = Aetheris_AI_Chat_API::has_credentials();
$aetheris_dash_url  = AETHERIS_AI_CHAT_DEFAULT_DASHBOARD;

$aetheris_stat_cards = array();
if ( is_array( $stats ) && ! is_wp_error( $stats ) ) {
	$aetheris_stat_cards = array(
		array( 'label' => __( 'Conversations (7d)', 'aetheris-ai-chat' ), 'value' => isset( $stats['conversations_7d'] ) ? (int) $stats['conversations_7d'] : 0 ),
		array( 'label' => __( 'Messages (7d)', 'aetheris-ai-chat' ),      'value' => isset( $stats['messages_7d'] ) ? (int) $stats['messages_7d'] : 0 ),
		array( 'label' => __( 'Leads (7d)', 'aetheris-ai-chat' ),         'value' => isset( $stats['leads_7d'] ) ? (int) $stats['leads_7d'] : 0 ),
		array( 'label' => __( 'Documents indexed', 'aetheris-ai-chat' ),   'value' => isset( $stats['documents'] ) ? (int) $stats['documents'] : 0 ),
	);
}
?>
<div class="wrap aetheris-wrap">
	<?php Aetheris_AI_Chat_Admin::render_header( __( 'Chatbot dashboard', 'aetheris-ai-chat' ) ); ?>

	<?php if ( ! $aetheris_has_creds ) : ?>
		<div class="notice notice-info">
			<p>
				<?php
				printf(
					/* translators: %s: link to the plugin Settings page. */
					esc_html__( 'Connect your account in %s to view analytics and chat history.', 'aetheris-ai-chat' ),
					'<a href="' . esc_url( admin_url( 'admin.php?page=' . Aetheris_AI_Chat_Admin::SLUG ) ) . '">'
					. esc_html__( 'Settings', 'aetheris-ai-chat' ) . '</a>'
				);
				?>
			</p>
		</div>
	<?php elseif ( is_wp_error( $stats ) ) : ?>
		<div class="notice notice-error">
			<p><?php echo esc_html( $stats->get_error_message() ); ?></p>
		</div>
	<?php endif; ?>

	<?php if ( ! empty( $aetheris_stat_cards ) ) : ?>
		<div class="aetheris-stats">
			<?php foreach ( $aetheris_stat_cards as $aetheris_card ) : ?>
				<div class="aetheris-stat">
					<div class="aetheris-stat-value"><?php echo esc_html( number_format_i18n( $aetheris_card['value'] ) ); ?></div>
					<div class="aetheris-stat-label"><?php echo esc_html( $aetheris_card['label'] ); ?></div>
				</div>
			<?php endforeach; ?>
		</div>
	<?php endif; ?>

	<div class="aetheris-card">
		<h2><?php esc_html_e( 'Recent conversations', 'aetheris-ai-chat' ); ?></h2>
		<?php if ( is_array( $convos ) && ! empty( $convos['items'] ) ) : ?>
			<table class="widefat striped aetheris-table">
				<thead>
					<tr>
						<th><?php esc_html_e( 'Started', 'aetheris-ai-chat' ); ?></th>
						<th><?php esc_html_e( 'Visitor', 'aetheris-ai-chat' ); ?></th>
						<th><?php esc_html_e( 'Last message', 'aetheris-ai-chat' ); ?></th>
						<th><?php esc_html_e( 'Messages', 'aetheris-ai-chat' ); ?></th>
					</tr>
				</thead>
				<tbody>
					<?php foreach ( $convos['items'] as $aetheris_row ) : ?>
						<tr>
							<td>
								<?php
								$aetheris_ts = isset( $aetheris_row['started_at'] ) ? strtotime( $aetheris_row['started_at'] ) : false;
								echo esc_html(
									$aetheris_ts
										? sprintf(
											/* translators: %s: human-readable time difference, e.g. "5 mins". */
											__( '%s ago', 'aetheris-ai-chat' ),
											human_time_diff( $aetheris_ts, time() )
										)
										: '—'
								);
								?>
							</td>
							<td><?php echo esc_html( isset( $aetheris_row['visitor'] ) ? $aetheris_row['visitor'] : __( 'Anonymous', 'aetheris-ai-chat' ) ); ?></td>
							<td><?php echo esc_html( isset( $aetheris_row['last_message'] ) ? wp_trim_words( $aetheris_row['last_message'], 14 ) : '' ); ?></td>
							<td><?php echo esc_html( isset( $aetheris_row['message_count'] ) ? (int) $aetheris_row['message_count'] : '—' ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		<?php elseif ( is_wp_error( $convos ) ) : ?>
			<p class="description"><?php echo esc_html( $convos->get_error_message() ); ?></p>
		<?php else : ?>
			<p class="description">
				<?php esc_html_e( 'No conversations yet. Once visitors start chatting, recent transcripts appear here.', 'aetheris-ai-chat' ); ?>
			</p>
		<?php endif; ?>

		<p>
			<a class="button button-secondary" href="<?php echo esc_url( $aetheris_dash_url ); ?>" target="_blank" rel="noopener">
				<?php esc_html_e( 'Open full dashboard', 'aetheris-ai-chat' ); ?> &rarr;
			</a>
		</p>
	</div>
</div>
