<?php
/**
 * Reports view: last 30 days overview.
 *
 * Advanced controls are extension points (`lucrsure_reports_toolbar`, filters).
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Included template; variables are local to the including scope.

$range   = lucrsure_get_report_date_range( 'view' );
$from    = $range['from'];
$to      = $range['to'];
$filters = lucrsure_get_report_filters( 'view' );

$report  = LUCRSURE_Reports::generate( $from, $to, $filters );
$max_day = ! empty( $report['revenue_by_day'] ) ? max( $report['revenue_by_day'] ) : 0;
$max_day = max( 1, $max_day );
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php esc_html_e( 'Reports', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub">
			<?php
			if ( has_action( 'lucrsure_reports_toolbar' ) ) {
				printf(
					/* translators: 1: from date, 2: to date */
					esc_html__( 'Showing data from %1$s to %2$s.', 'lucrativeit-subscription-recurring-billing' ),
					esc_html( date_i18n( 'M j, Y', strtotime( $range['from_attr'] ) ) ),
					esc_html( date_i18n( 'M j, Y', strtotime( $range['to_attr'] ) ) )
				);
			} else {
				esc_html_e( 'Last 30 days overview.', 'lucrativeit-subscription-recurring-billing' );
			}
			?>
		</p>
	</div>
</div>

<?php
/**
 * Extension point for advanced report controls (custom date range, filters, CSV).
 *
 * @param array $range   Date range from lucrsure_get_report_date_range().
 * @param array $filters Normalised filters.
 */
do_action( 'lucrsure_reports_toolbar', $range, $filters );
?>

<div class="lucrsure-kpis">
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'REVENUE (PAID)', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( lucrsure_format_money( $report['revenue'] ) ); ?></strong>
	</div>
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'NEW SUBSCRIPTIONS', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( number_format_i18n( $report['new_subscriptions'] ) ); ?></strong>
	</div>
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'CANCELLATIONS', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( number_format_i18n( $report['canceled_subscriptions'] ) ); ?></strong>
	</div>
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'CURRENT MRR', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( lucrsure_format_money( $report['mrr_now'] ) ); ?></strong>
	</div>
</div>

<div class="lucrsure-card">
	<div class="lucrsure-card-head">
		<h2><?php esc_html_e( 'Invoices in range', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
	</div>
	<div class="lucrsure-reports-stat-row">
		<div class="lucrsure-reports-stat"><span><?php esc_html_e( 'Total', 'lucrativeit-subscription-recurring-billing' ); ?></span><strong><?php echo esc_html( number_format_i18n( $report['invoice_counts']['total'] ) ); ?></strong></div>
		<div class="lucrsure-reports-stat"><span><?php esc_html_e( 'Paid', 'lucrativeit-subscription-recurring-billing' ); ?></span><strong><?php echo esc_html( number_format_i18n( $report['invoice_counts']['paid'] ) ); ?></strong></div>
		<div class="lucrsure-reports-stat"><span><?php esc_html_e( 'Unpaid', 'lucrativeit-subscription-recurring-billing' ); ?></span><strong><?php echo esc_html( number_format_i18n( $report['invoice_counts']['unpaid'] ) ); ?></strong></div>
		<div class="lucrsure-reports-stat"><span><?php esc_html_e( 'Overdue', 'lucrativeit-subscription-recurring-billing' ); ?></span><strong><?php echo esc_html( number_format_i18n( $report['invoice_counts']['overdue'] ) ); ?></strong></div>
		<div class="lucrsure-reports-stat"><span><?php esc_html_e( 'Void', 'lucrativeit-subscription-recurring-billing' ); ?></span><strong><?php echo esc_html( number_format_i18n( $report['invoice_counts']['void'] ) ); ?></strong></div>
	</div>
</div>

<div class="lucrsure-card">
	<div class="lucrsure-card-head">
		<h2><?php esc_html_e( 'Revenue by day', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
	</div>
	<?php if ( empty( $report['revenue_by_day'] ) ) : ?>
		<p class="lucrsure-empty"><?php esc_html_e( 'No paid invoices in this range.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	<?php else : ?>
		<div class="lucrsure-chart lucrsure-reports-chart">
			<?php foreach ( $report['revenue_by_day'] as $day => $value ) : ?>
				<div class="lucrsure-chart-col">
					<span class="lucrsure-chart-amount"><?php echo esc_html( lucrsure_format_money( $value ) ); ?></span>
					<div class="lucrsure-chart-bar" data-lucrsure-height="<?php echo esc_attr( (string) max( 4, round( ( $value / $max_day ) * 120 ) ) ); ?>"></div>
					<span class="lucrsure-chart-label"><?php echo esc_html( gmdate( 'M j', strtotime( $day ) ) ); ?></span>
				</div>
			<?php endforeach; ?>
		</div>
	<?php endif; ?>
</div>

<div class="lucrsure-grid-2">
	<div class="lucrsure-card">
		<div class="lucrsure-card-head">
			<h2><?php esc_html_e( 'Revenue by gateway', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
		</div>
		<?php if ( empty( $report['revenue_by_gateway'] ) ) : ?>
			<p class="lucrsure-empty"><?php esc_html_e( 'No data.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
		<?php else : ?>
			<table class="lucrsure-mini-table">
				<?php foreach ( $report['revenue_by_gateway'] as $gateway => $total ) : ?>
					<tr>
						<td><?php echo esc_html( ucfirst( $gateway ) ); ?></td>
						<td class="lucrsure-right"><?php echo esc_html( lucrsure_format_money( $total ) ); ?></td>
					</tr>
				<?php endforeach; ?>
			</table>
		<?php endif; ?>
	</div>

	<div class="lucrsure-card">
		<div class="lucrsure-card-head">
			<h2><?php esc_html_e( 'Revenue by plan', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
		</div>
		<?php if ( empty( $report['revenue_by_plan'] ) ) : ?>
			<p class="lucrsure-empty"><?php esc_html_e( 'No data.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
		<?php else : ?>
			<table class="lucrsure-mini-table">
				<?php foreach ( $report['revenue_by_plan'] as $plan => $total ) : ?>
					<tr>
						<td><?php echo esc_html( $plan ); ?></td>
						<td class="lucrsure-right"><?php echo esc_html( lucrsure_format_money( $total ) ); ?></td>
					</tr>
				<?php endforeach; ?>
			</table>
		<?php endif; ?>
	</div>
</div>
