<?php
/**
 * Public Leads Viewer template (token-authenticated, no WordPress login).
 *
 * Reuses the transcript-viewer shell classes (sitestaffr-tv-*) for a consistent
 * branded look, with leads-specific table styling in sitestaffr-leads-viewer.css.
 *
 * Expects (from SiteStaffr_Leads::maybe_render_leads_page):
 *   $leads, $business_name, $logo_url, $csv_url, $cycle_label,
 *   $prev_cycle_url, $next_cycle_url, $leads_diag
 *
 * @package SiteStaffr
 */

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

$sitestaffr_lead_count = is_array( $leads ) ? count( $leads ) : 0;

// Handled / follow-up split for the filter tabs.
$sitestaffr_handled_count = 0;
if ( $sitestaffr_lead_count > 0 ) {
	foreach ( $leads as $sitestaffr_l ) {
		if ( SiteStaffr_Leads::is_handled( $sitestaffr_l ) ) {
			$sitestaffr_handled_count++;
		}
	}
}
$sitestaffr_followup_count = $sitestaffr_lead_count - $sitestaffr_handled_count;

$sitestaffr_page_title = ! empty( $business_name )
	? $business_name . ' — Qualified Leads'
	: 'Qualified Leads';
?>
<!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_page_title ); ?></title>
	<?php wp_head(); ?>
</head>
<body class="sitestaffr-leads-page">

	<!-- Top Bar (contained) -->
	<div class="sitestaffr-tv-topbar">
		<div class="sitestaffr-tv-topbar-inner">
			<img src="<?php echo esc_url( $logo_url ); ?>" alt="SiteStaffr">
			<?php if ( $sitestaffr_lead_count > 0 ) : ?>
				<div class="sitestaffr-leads-actions">
					<details class="sitestaffr-leads-actions-toggle-wrap">
						<summary class="sitestaffr-leads-actions-toggle"><?php echo esc_html__( 'Actions', 'sitestaffr' ); ?></summary>
					</details>
					<div class="sitestaffr-leads-actions-menu">
						<button type="button" onclick="window.print()" class="sitestaffr-tv-print-btn"><?php echo esc_html__( 'Print / Download PDF', 'sitestaffr' ); ?></button>
						<a href="<?php echo esc_url( $csv_url ); ?>" class="sitestaffr-leads-csv-btn"><?php echo esc_html__( 'Download CSV', 'sitestaffr' ); ?></a>
					</div>
				</div>
			<?php endif; ?>
		</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>
			</div>
		<?php endif; ?>

		<div class="sitestaffr-tv-card">
			<div class="sitestaffr-tv-card-header">
				<h2><?php echo esc_html__( 'Qualified Leads', '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 else : ?>
						<span class="sitestaffr-leads-cyclenav-btn is-disabled">&larr; <?php echo esc_html__( 'Previous cycle', 'sitestaffr' ); ?></span>
					<?php endif; ?>

					<span class="sitestaffr-leads-cyclenav-label">
						<strong><?php echo esc_html( $cycle_label ); ?></strong>
						<span class="sitestaffr-leads-count">
							<?php
							/* translators: %d: number of leads in this billing cycle */
							echo esc_html( sprintf( _n( '%d lead', '%d leads', $sitestaffr_lead_count, 'sitestaffr' ), $sitestaffr_lead_count ) );
							?>
						</span>
					</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 else : ?>
						<span class="sitestaffr-leads-cyclenav-btn is-disabled"><?php echo esc_html__( 'Next cycle', 'sitestaffr' ); ?> &rarr;</span>
					<?php endif; ?>
				</div>

				<?php if ( ! empty( $leads_diag ) && is_array( $leads_diag ) ) : ?>
					<p class="sitestaffr-leads-subhead" style="font-size:13px;color:#555;">
						<?php
						echo esc_html( sprintf(
							/* translators: counts only, no contact data */
							__( 'Diagnostic (v%1$s): scanned %2$d conversations — name detected in %3$d, email in %4$d, phone in %5$d; %6$d qualify, %7$d currently flagged.', 'sitestaffr' ),
							$leads_diag['version'],
							$leads_diag['scanned'],
							$leads_diag['has_name'],
							$leads_diag['has_email'],
							$leads_diag['has_phone'],
							$leads_diag['qualifies'],
							$leads_diag['flagged']
						) );
						?>
					</p>
				<?php endif; ?>

				<?php if ( 0 === $sitestaffr_lead_count ) : ?>
					<p class="sitestaffr-tv-empty"><?php echo esc_html__( 'No qualified leads in this billing cycle.', 'sitestaffr' ); ?></p>
				<?php else : ?>
					<div class="sitestaffr-leads-filter" role="tablist" aria-label="<?php echo esc_attr__( 'Filter leads', 'sitestaffr' ); ?>">
						<button type="button" class="sitestaffr-leads-filter-tab is-active" data-filter="all" aria-pressed="true">
							<?php echo esc_html__( 'All', 'sitestaffr' ); ?> <span class="sitestaffr-leads-filter-count"><?php echo esc_html( (string) $sitestaffr_lead_count ); ?></span>
						</button>
						<button type="button" class="sitestaffr-leads-filter-tab" data-filter="followup" aria-pressed="false">
							<?php echo esc_html__( 'Follow-up', 'sitestaffr' ); ?> <span class="sitestaffr-leads-filter-count"><?php echo esc_html( (string) $sitestaffr_followup_count ); ?></span>
						</button>
						<button type="button" class="sitestaffr-leads-filter-tab" data-filter="handled" aria-pressed="false">
							<?php echo esc_html__( 'Handled', 'sitestaffr' ); ?> <span class="sitestaffr-leads-filter-count"><?php echo esc_html( (string) $sitestaffr_handled_count ); ?></span>
						</button>
					</div>
					<p class="sitestaffr-leads-empty-filter" hidden><?php echo esc_html__( 'No leads in this view.', 'sitestaffr' ); ?></p>
					<table class="sitestaffr-leads-table">
						<thead>
							<tr>
								<th><?php echo esc_html__( 'Date', 'sitestaffr' ); ?></th>
								<th><?php echo esc_html__( 'Name', 'sitestaffr' ); ?></th>
								<th><?php echo esc_html__( 'Contact', 'sitestaffr' ); ?></th>
								<th><?php echo esc_html__( 'What they wanted', 'sitestaffr' ); ?></th>
								<th><?php echo esc_html__( 'Conversation', 'sitestaffr' ); ?></th>
								<th><?php echo esc_html__( 'Follow-up', 'sitestaffr' ); ?></th>
							</tr>
						</thead>
						<tbody>
							<?php foreach ( $leads as $sitestaffr_lead ) : ?>
								<?php
								$sitestaffr_transcript_url = class_exists( 'SiteStaffr_Magic_Link' ) ? SiteStaffr_Magic_Link::generate_transcript_url( (int) $sitestaffr_lead->id ) : '';
								$sitestaffr_clean_phone    = preg_replace( '/[^\d+]/', '', (string) $sitestaffr_lead->lead_phone );
								$sitestaffr_is_handled     = SiteStaffr_Leads::is_handled( $sitestaffr_lead );
								?>
								<tr data-lead-id="<?php echo esc_attr( (string) (int) $sitestaffr_lead->id ); ?>" data-handled="<?php echo $sitestaffr_is_handled ? '1' : '0'; ?>">
									<td data-label="<?php echo esc_attr__( 'Date', 'sitestaffr' ); ?>"><?php echo esc_html( SiteStaffr_Leads::format_lead_date( $sitestaffr_lead ) ); ?></td>
									<td data-label="<?php echo esc_attr__( 'Name', 'sitestaffr' ); ?>"><?php echo esc_html( $sitestaffr_lead->lead_name ); ?></td>
									<td data-label="<?php echo esc_attr__( 'Contact', 'sitestaffr' ); ?>">
										<?php if ( ! empty( $sitestaffr_lead->lead_phone ) ) : ?>
											<a href="<?php echo esc_attr( 'tel:' . $sitestaffr_clean_phone ); ?>"><?php echo esc_html( $sitestaffr_lead->lead_phone ); ?></a><br>
										<?php endif; ?>
										<?php if ( ! empty( $sitestaffr_lead->lead_email ) ) : ?>
											<a href="<?php echo esc_attr( 'mailto:' . $sitestaffr_lead->lead_email ); ?>"><?php echo esc_html( $sitestaffr_lead->lead_email ); ?></a>
										<?php endif; ?>
									</td>
									<td data-label="<?php echo esc_attr__( 'What they wanted', 'sitestaffr' ); ?>"><?php echo esc_html( $sitestaffr_lead->lead_interest ); ?></td>
									<td data-label="<?php echo esc_attr__( 'Conversation', 'sitestaffr' ); ?>">
										<?php if ( $sitestaffr_transcript_url ) : ?>
											<a class="sitestaffr-leads-view" href="<?php echo esc_url( $sitestaffr_transcript_url ); ?>">
												<span class="sitestaffr-leads-view-short"><?php echo esc_html__( 'View', 'sitestaffr' ); ?></span>
												<span class="sitestaffr-leads-view-full"><?php echo esc_html__( 'View Conversation', 'sitestaffr' ); ?></span>
											</a>
										<?php endif; ?>
									</td>
									<td data-label="<?php echo esc_attr__( 'Follow-up', 'sitestaffr' ); ?>" class="sitestaffr-leads-handled-cell">
										<button type="button" class="sitestaffr-leads-handled-btn" aria-pressed="<?php echo $sitestaffr_is_handled ? 'true' : 'false'; ?>">
											<span class="sitestaffr-leads-handled-mark"><?php echo esc_html__( 'Mark handled', 'sitestaffr' ); ?></span>
											<span class="sitestaffr-leads-handled-done"><?php echo esc_html__( '✓ Handled', 'sitestaffr' ); ?></span>
											<span class="sitestaffr-leads-handled-undo"><?php echo esc_html__( 'Undo', 'sitestaffr' ); ?></span>
										</button>
									</td>
								</tr>
							<?php endforeach; ?>
						</tbody>
					</table>
				<?php endif; ?>
			</div>
		</div>
	</div>
	<?php wp_footer(); ?>
</body>
</html>
