<?php
/**
 * Public Usage & Analytics report template (token-authenticated, no login).
 *
 * Reuses the transcript-viewer shell classes (sitestaffr-tv-*) for the shared
 * branded "SiteStaffr report" look; print-ready via the @media print block.
 *
 * Expects (from SiteStaffr_Usage_Report::maybe_render_report_page):
 *   $business_name, $logo_url, $cycle_label, $prev_cycle_url, $next_cycle_url,
 *   $counts, $total_minutes, $leads_count, $avg_seconds, $daily
 *
 * @package SiteStaffr
 */

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

$sitestaffr_report_title = ! empty( $business_name )
	? $business_name . ' — Conversation Analytics'
	: 'Conversation Analytics';

$sitestaffr_avg_display = '0m 00s';
if ( $avg_seconds > 0 ) {
	$sitestaffr_avg_display = sprintf( '%dm %02ds', (int) floor( $avg_seconds / 60 ), (int) round( fmod( $avg_seconds, 60 ) ) );
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta name="robots" content="noindex, nofollow">
	<title><?php echo esc_html( $sitestaffr_report_title ); ?></title>
	<?php wp_head(); ?>
	<style>
		.sitestaffr-report-metrics { display: flex; flex-wrap: wrap; gap: 14px; margin: 0 0 18px; }
		.sitestaffr-report-metric { flex: 1 1 140px; background: #fff; border: 1px solid #E8E2D8; border-radius: 8px; padding: 14px 16px; text-align: center; }
		.sitestaffr-report-metric-value { display: block; font-size: 26px; font-weight: 700; color: #004D40; }
		.sitestaffr-report-metric-label { display: block; font-size: 12px; color: #646970; margin-top: 4px; }
		.sitestaffr-report-table { width: 100%; border-collapse: collapse; font-size: 14px; }
		.sitestaffr-report-table th, .sitestaffr-report-table td { padding: 8px 12px; border-bottom: 1px solid #F0EBE0; text-align: left; }
		.sitestaffr-report-table th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: #646970; }
		.sitestaffr-report-table td:nth-child(2), .sitestaffr-report-table td:nth-child(3),
		.sitestaffr-report-table th:nth-child(2), .sitestaffr-report-table th:nth-child(3) { text-align: right; }
		.sitestaffr-report-date { font-size: 12px; color: #646970; margin: 4px 0 0; }
		@media print {
			/* Keep the SiteStaffr logo on the printed report (the transcript view
			   does the same) — only the controls drop out. */
			.sitestaffr-tv-print-btn, .sitestaffr-leads-cyclenav { display: none !important; }
			.sitestaffr-tv-topbar { position: static; border-bottom: none; }
			body { background: #fff !important; }
			.sitestaffr-tv-card { border: none !important; box-shadow: none !important; }
			.sitestaffr-report-metric { border-color: #ccc; }
			.sitestaffr-widget-container, .sitestaffr-widget-trigger { display: none !important; }
		}
	</style>
</head>
<body class="sitestaffr-leads-page">

	<div class="sitestaffr-tv-topbar">
		<div class="sitestaffr-tv-topbar-inner">
			<img src="<?php echo esc_url( $logo_url ); ?>" alt="SiteStaffr">
			<button type="button" onclick="window.print()" class="sitestaffr-tv-print-btn"><?php echo esc_html__( 'Print / Download PDF', 'sitestaffr' ); ?></button>
		</div>
	</div>

	<div class="sitestaffr-tv-container">

		<?php if ( ! empty( $business_name ) ) : ?>
			<div class="sitestaffr-tv-biz-header">
				<h1><?php echo esc_html( $business_name ); ?></h1>
				<p class="sitestaffr-report-date">
					<?php
					echo esc_html( sprintf(
						/* translators: 1: billing cycle label, 2: generation date */
						__( 'Conversation analytics — %1$s (generated %2$s)', 'sitestaffr' ),
						$cycle_label,
						wp_date( 'M j, Y', time(), new DateTimeZone( 'America/New_York' ) )
					) );
					?>
				</p>
			</div>
		<?php endif; ?>

		<div class="sitestaffr-tv-card">
			<div class="sitestaffr-tv-card-header">
				<h2><?php echo esc_html__( 'Cycle Summary', 'sitestaffr' ); ?></h2>
			</div>
			<div class="sitestaffr-tv-card-body">
				<div class="sitestaffr-leads-cyclenav">
					<?php if ( ! empty( $prev_cycle_url ) ) : ?>
						<a class="sitestaffr-leads-cyclenav-btn" href="<?php echo esc_url( $prev_cycle_url ); ?>" rel="nofollow">&larr; <?php echo esc_html__( 'Previous cycle', 'sitestaffr' ); ?></a>
					<?php endif; ?>
					<span class="sitestaffr-leads-cyclenav-label"><strong><?php echo esc_html( $cycle_label ); ?></strong></span>
					<?php if ( ! empty( $next_cycle_url ) ) : ?>
						<a class="sitestaffr-leads-cyclenav-btn" href="<?php echo esc_url( $next_cycle_url ); ?>" rel="nofollow"><?php echo esc_html__( 'Next cycle', 'sitestaffr' ); ?> &rarr;</a>
					<?php endif; ?>
				</div>

				<div class="sitestaffr-report-metrics">
					<div class="sitestaffr-report-metric">
						<span class="sitestaffr-report-metric-value"><?php echo esc_html( number_format_i18n( (int) $counts['total_calls'] ) ); ?></span>
						<span class="sitestaffr-report-metric-label"><?php echo esc_html__( 'Conversations', 'sitestaffr' ); ?></span>
					</div>
					<div class="sitestaffr-report-metric">
						<span class="sitestaffr-report-metric-value"><?php echo esc_html( number_format_i18n( $leads_count ) ); ?></span>
						<span class="sitestaffr-report-metric-label"><?php echo esc_html__( 'Qualified Leads', 'sitestaffr' ); ?></span>
					</div>
					<div class="sitestaffr-report-metric">
						<span class="sitestaffr-report-metric-value"><?php echo esc_html( number_format_i18n( $total_minutes ) ); ?></span>
						<span class="sitestaffr-report-metric-label"><?php echo esc_html__( 'Minutes Used', 'sitestaffr' ); ?></span>
					</div>
					<div class="sitestaffr-report-metric">
						<span class="sitestaffr-report-metric-value"><?php echo esc_html( $sitestaffr_avg_display ); ?></span>
						<span class="sitestaffr-report-metric-label"><?php echo esc_html__( 'Avg Conversation Length', 'sitestaffr' ); ?></span>
					</div>
				</div>

				<?php if ( empty( $daily ) ) : ?>
					<p class="sitestaffr-tv-empty"><?php echo esc_html__( 'No conversation activity in this billing cycle yet.', 'sitestaffr' ); ?></p>
				<?php else : ?>
					<table class="sitestaffr-report-table">
						<thead>
							<tr>
								<th><?php echo esc_html__( 'Date', 'sitestaffr' ); ?></th>
								<th><?php echo esc_html__( 'Conversations', 'sitestaffr' ); ?></th>
								<th><?php echo esc_html__( 'Minutes', 'sitestaffr' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $daily as $day ) : ?>
								<tr>
									<td><?php echo esc_html( $day['label'] ); ?></td>
									<td><?php echo esc_html( number_format_i18n( $day['conversations'] ) ); ?></td>
									<td><?php echo esc_html( number_format_i18n( $day['minutes'] ) ); ?></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				<?php endif; ?>
			</div>
		</div>

		<?php if ( 'configured' === $hours_state || ! empty( $top_pages ) ) : ?>
		<div class="sitestaffr-tv-card">
			<div class="sitestaffr-tv-card-header">
				<h2><?php echo esc_html__( 'When & Where', 'sitestaffr' ); ?></h2>
			</div>
			<div class="sitestaffr-tv-card-body">

				<?php if ( 'configured' === $hours_state ) : ?>
					<div class="sitestaffr-report-metrics">
						<div class="sitestaffr-report-metric">
							<span class="sitestaffr-report-metric-value"><?php echo esc_html( $after_hours_pct . '%' ); ?></span>
							<span class="sitestaffr-report-metric-label"><?php echo esc_html__( 'Arrived After Hours', 'sitestaffr' ); ?></span>
						</div>
						<div class="sitestaffr-report-metric">
							<span class="sitestaffr-report-metric-value"><?php echo esc_html( number_format_i18n( $after_hours['after_hours'] ) ); ?></span>
							<span class="sitestaffr-report-metric-label"><?php echo esc_html__( 'After-Hours Conversations', 'sitestaffr' ); ?></span>
						</div>
						<div class="sitestaffr-report-metric">
							<span class="sitestaffr-report-metric-value"><?php echo esc_html( number_format_i18n( $after_hours['after_hours_leads'] ) ); ?></span>
							<span class="sitestaffr-report-metric-label"><?php echo esc_html__( 'After-Hours Qualified Leads', 'sitestaffr' ); ?></span>
						</div>
					</div>
				<?php endif; ?>

				<?php if ( ! empty( $top_pages ) ) : ?>
					<table class="sitestaffr-report-table">
						<thead>
							<tr>
								<th><?php echo esc_html__( 'Page', 'sitestaffr' ); ?></th>
								<th><?php echo esc_html__( 'Conversations', 'sitestaffr' ); ?></th>
								<th><?php echo esc_html__( 'Qualified Leads', 'sitestaffr' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $top_pages as $page_row ) : ?>
								<tr>
									<td>
										<?php
										if ( $page_row['is_unknown'] ) {
											echo esc_html__( 'Not recorded', 'sitestaffr' );
										} else {
											echo esc_html( '' !== $page_row['title'] ? $page_row['title'] : $page_row['page'] );
										}
										?>
									</td>
									<td><?php echo esc_html( number_format_i18n( $page_row['conversations'] ) ); ?></td>
									<td><?php echo esc_html( number_format_i18n( $page_row['leads'] ) ); ?></td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				<?php endif; ?>

			</div>
		</div>
		<?php endif; ?>

		<div class="sitestaffr-tv-footer">
			<?php echo esc_html__( 'Powered by SiteStaffr — AI staff for your website.', 'sitestaffr' ); ?>
		</div>
	</div>
	<?php wp_footer(); ?>
</body>
</html>
