<?php
/**
 * The analytics screen.
 *
 * The shared summary partial, then the one thing a page has room for that a
 * dashboard widget does not: every event with how it actually went.
 *
 * @package TextTango
 *
 * @var array<string, mixed> $args View data supplied by AnalyticsPage::render().
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

$texttango_stats     = (array) ( $args['stats'] ?? array() );
$texttango_days      = (int) ( $texttango_stats['days'] ?? 0 );
$texttango_breakdown = (array) ( $texttango_stats['breakdown'] ?? array() );
$texttango_currency  = (string) ( $args['currency'] ?? '' );
$texttango_camp      = (array) ( $texttango_stats['campaigns'] ?? array() );
$texttango_campaigns = (int) ( $texttango_camp['campaigns'] ?? 0 );
?>
<div class="wrap">
	<div id="texttango-app" class="texttango-ui">
		<div class="tt-py-4">

			<header class="tt-mb-6">
				<?php
				load_template(
					TEXTTANGO_PATH . 'templates/admin/partials/brand.php',
					false,
					array( 'title' => __( 'Analytics', 'texttango' ) )
				);
				?>
				<p class="tt-mt-1 tt-mb-0 tt-text-sm tt-text-slate-500">
					<?php esc_html_e( 'What this site has been sending.', 'texttango' ); ?>
				</p>
			</header>

			<div class="tt-card tt-mb-4">
				<?php
				load_template(
					TEXTTANGO_PATH . 'templates/admin/partials/analytics.php',
					false,
					array(
						'stats'   => $texttango_stats,
						'compact' => false,
						'tones'   => (array) ( $args['tones'] ?? array() ),
						'windows' => (array) ( $args['windows'] ?? array() ),
						'links'   => array(
							__( 'Message log', 'texttango' ) => (string) ( $args['log_url'] ?? '' ),
						),
					)
				);
				?>
			</div>

			<div class="tt-card">
				<h2 class="tt-m-0 tt-mb-1 tt-p-0 tt-text-lg tt-font-semibold tt-text-slate-900">
					<?php esc_html_e( 'By event', 'texttango' ); ?>
				</h2>
				<p class="tt-mt-0 tt-mb-4 tt-text-sm tt-text-slate-500">
					<?php esc_html_e( 'Which triggers are sending, and how they are landing.', 'texttango' ); ?>
				</p>

				<?php if ( array() === $texttango_breakdown ) : ?>
					<?php
					load_template(
						TEXTTANGO_PATH . 'templates/admin/partials/empty-state.php',
						false,
						array(
							'title' => __( 'Nothing to break down yet.', 'texttango' ),
							'lead'  => sprintf(
								/* translators: %s: number of days. */
								_n(
									'No messages have gone out in the last %s day.',
									'No messages have gone out in the last %s days.',
									$texttango_days,
									'texttango'
								),
								number_format_i18n( $texttango_days )
							),
						)
					);
					?>
				<?php else : ?>
					<div class="tt-overflow-x-auto">
						<table class="tt-table">
							<thead>
								<tr>
									<th class="tt-th tt-pl-0"><?php esc_html_e( 'Event', 'texttango' ); ?></th>
									<th class="tt-th tt-text-right"><?php esc_html_e( 'Sent', 'texttango' ); ?></th>
									<th class="tt-th tt-text-right"><?php esc_html_e( 'Delivered', 'texttango' ); ?></th>
									<th class="tt-th tt-text-right"><?php esc_html_e( 'Failed', 'texttango' ); ?></th>
									<th class="tt-th tt-pr-0 tt-text-right"><?php esc_html_e( 'Spent', 'texttango' ); ?></th>
								</tr>
							</thead>
							<tbody>
								<?php foreach ( $texttango_breakdown as $texttango_row ) : ?>
									<tr>
										<?php
										/*
										 * The stored event key, exactly as the message log
										 * prints it. A friendlier name here would give one
										 * event two names across two screens, and the log's
										 * event filter is the thing somebody reaches for
										 * next after reading a row of this table.
										 */
										?>
										<td class="tt-td tt-pl-0 tt-font-medium tt-text-slate-700">
											<?php echo esc_html( (string) $texttango_row['event'] ); ?>
										</td>
										<td class="tt-td tt-whitespace-nowrap tt-text-right">
											<?php echo esc_html( number_format_i18n( (int) $texttango_row['total'] ) ); ?>
										</td>
										<td class="tt-td tt-whitespace-nowrap tt-text-right">
											<?php echo esc_html( number_format_i18n( (int) $texttango_row['delivered'] ) ); ?>
											<span class="tt-text-slate-400">
												<?php echo esc_html( number_format_i18n( (float) $texttango_row['rate'] ) ); ?>%
											</span>
										</td>
										<td class="tt-td tt-whitespace-nowrap tt-text-right <?php echo (int) $texttango_row['failed'] > 0 ? 'tt-text-rose-600' : 'tt-td-muted'; ?>">
											<?php echo esc_html( number_format_i18n( (int) $texttango_row['failed'] ) ); ?>
										</td>
										<td class="tt-td tt-whitespace-nowrap tt-pr-0 tt-text-right">
											<?php
											/*
											 * Credits, not money, for a campaign. The platform bills a
											 * campaign in credits and reports no per-message cost for
											 * one, so this cell names the unit it is in rather than
											 * printing a currency figure nothing ever measured.
											 */
											if ( (int) ( $texttango_row['credits'] ?? 0 ) > 0 ) {
												echo esc_html(
													sprintf(
														/* translators: %s: number of credits. */
														_n( '%s credit', '%s credits', (int) $texttango_row['credits'], 'texttango' ),
														number_format_i18n( (int) $texttango_row['credits'] )
													)
												);
											} else {
												echo esc_html( trim( $texttango_currency . ' ' . number_format_i18n( (float) $texttango_row['spend'], 2 ) ) );
											}
											?>
										</td>
									</tr>
								<?php endforeach; ?>
							</tbody>
						</table>
					</div>

					<?php
					/*
					 * Said here as well as in the summary above. Somebody who scrolled
					 * straight to this table to work out a bill never read the other one,
					 * and a total that quietly excludes sandbox sends is the kind of
					 * figure that gets quoted at somebody.
					 */
					?>
					<p class="tt-mt-3 tt-mb-0 tt-text-xs tt-text-slate-400">
						<?php esc_html_e( 'Sandbox messages are not counted.', 'texttango' ); ?>
					</p>
				<?php endif; ?>
			</div>

			<?php
			/*
			 * Campaigns get a block of their own, because the table above cannot
			 * carry what makes them different: how many campaigns those recipients
			 * arrived in, and the credits they were billed in rather than a money
			 * cost the platform never reports per message.
			 *
			 * Only once one has actually gone out. A store that sends none should
			 * not have to read a row of zeros to work out that it sends none.
			 */
			?>
			<?php if ( $texttango_campaigns > 0 ) : ?>
				<div class="tt-card tt-mt-4">
					<div class="tt-mb-4 tt-flex tt-flex-wrap tt-items-start tt-justify-between tt-gap-3">
						<div>
							<h2 class="tt-m-0 tt-mb-1 tt-p-0 tt-text-lg tt-font-semibold tt-text-slate-900">
								<?php esc_html_e( 'Campaigns', 'texttango' ); ?>
							</h2>
							<p class="tt-m-0 tt-text-sm tt-text-slate-500">
								<?php esc_html_e( 'Sent in bulk by TextTango, so they have no individual rows in the message log. These counts come back from the platform.', 'texttango' ); ?>
							</p>
						</div>
						<a class="tt-btn tt-btn-secondary tt-shrink-0" href="<?php echo esc_url( (string) ( $args['history_url'] ?? '' ) ); ?>">
							<?php esc_html_e( 'Campaign history', 'texttango' ); ?>
						</a>
					</div>

					<div class="tt-grid tt-grid-cols-2 tt-gap-4 sm:tt-grid-cols-5">
						<?php
						$texttango_figures = array(
							__( 'Campaigns', 'texttango' ) => number_format_i18n( $texttango_campaigns ),
							__( 'Recipients', 'texttango' ) => number_format_i18n( (int) ( $texttango_camp['recipients'] ?? 0 ) ),
							__( 'Delivered', 'texttango' ) => number_format_i18n( (int) ( $texttango_camp['delivered'] ?? 0 ) ),
							__( 'Failed', 'texttango' )    => number_format_i18n( (int) ( $texttango_camp['failed'] ?? 0 ) ),
							__( 'Credits', 'texttango' )   => number_format_i18n( (int) ( $texttango_camp['credits'] ?? 0 ) ),
						);
						?>
						<?php foreach ( $texttango_figures as $texttango_label => $texttango_value ) : ?>
							<div>
								<div class="tt-eyebrow"><?php echo esc_html( (string) $texttango_label ); ?></div>
								<div class="tt-analytics-figure"><?php echo esc_html( (string) $texttango_value ); ?></div>
							</div>
						<?php endforeach; ?>
					</div>

					<?php
					/*
					 * Said plainly rather than left for somebody to discover by adding
					 * the columns up. A campaign scheduled for next week has sent
					 * nothing yet, and counting it here would report messages that do
					 * not exist and hold the delivery rate down while it waits.
					 */
					?>
					<p class="tt-mt-3 tt-mb-0 tt-text-xs tt-text-slate-400">
						<?php esc_html_e( 'Scheduled campaigns are counted once they go out, not when they are written.', 'texttango' ); ?>
					</p>
				</div>
			<?php endif; ?>

		</div>
	</div>
</div>
