<?php
/**
 * Dashboard view: executive overview.
 *
 * @package Lucrativeit_Subscription_Recurring_Billing
 *
 * @var LUCRSURE_Settings $settings Settings.
 */

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

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

$subs_repo    = new LUCRSURE_Subscription_Repository();
$plans_repo   = new LUCRSURE_Plan_Repository();
$invoice_repo = new LUCRSURE_Invoice_Repository();

$mrr            = $subs_repo->total_mrr();
$arr            = $mrr * 12;
$active_count   = $subs_repo->count_status( 'active' ) + $subs_repo->count_status( 'trialing' );
$now            = current_time( 'mysql' );
$compare_date   = gmdate( 'Y-m-d H:i:s', strtotime( '-30 days', strtotime( $now ) ) );
$churn          = $subs_repo->churn_rate_as_of( $now );

$mrr_prev       = $subs_repo->mrr_as_of( $compare_date );
$active_prev    = $subs_repo->active_count_as_of( $compare_date );
$churn_prev     = $subs_repo->churn_rate_as_of( $compare_date );

$mrr_trend      = lucrsure_percent_change( $mrr, $mrr_prev );
$arr_trend      = $mrr_trend;
$active_trend   = lucrsure_percent_change( $active_count, $active_prev );
$churn_trend    = round( $churn - $churn_prev, 1 );

$latest_plans    = $plans_repo->latest( 3 );
$recent_subs    = $subs_repo->query( array( 'limit' => 5 ) );
$recent_invoices = $invoice_repo->query( array( 'limit' => 5 ) );

$chart_year     = (int) gmdate( 'Y' );
$revenue_series = $invoice_repo->revenue_for_year( $chart_year );
$trend_months   = array();
for ( $m = 1; $m <= 12; $m++ ) {
	$ym           = sprintf( '%04d-%02d', $chart_year, $m );
	$trend_months[] = array(
		'label' => gmdate( 'M', strtotime( $ym . '-01' ) ),
		'value' => isset( $revenue_series[ $ym ] ) ? (float) $revenue_series[ $ym ] : 0.0,
	);
}
?>

<div class="lucrsure-page-head">
	<div>
		<h1><?php esc_html_e( 'Executive Overview', 'lucrativeit-subscription-recurring-billing' ); ?></h1>
		<p class="lucrsure-sub"><?php esc_html_e( 'Real-time subscription performance and revenue insights.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
	</div>
	<a class="lucrsure-btn lucrsure-btn-primary" href="<?php echo esc_url( lucrsure_admin_nonce_url( 'subscriptions', array( 'action' => 'new' ) ) ); ?>">+ <?php esc_html_e( 'New Subscription', 'lucrativeit-subscription-recurring-billing' ); ?></a>
</div>

<div class="lucrsure-kpis">
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'Monthly Recurring Revenue', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( lucrsure_format_money( $mrr ) ); ?></strong>
		<?php echo lucrsure_render_kpi_trend( $mrr_trend ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
	</div>
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'Annual Recurring Revenue', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( lucrsure_format_money( $arr ) ); ?></strong>
		<?php echo lucrsure_render_kpi_trend( $arr_trend ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
	</div>
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'ACTIVE SUBSCRIPTIONS', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( number_format_i18n( $active_count ) ); ?></strong>
		<?php echo lucrsure_render_kpi_trend( $active_trend ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
	</div>
	<div class="lucrsure-kpi">
		<span class="lucrsure-kpi-label"><?php esc_html_e( 'CHURN RATE', 'lucrativeit-subscription-recurring-billing' ); ?></span>
		<strong class="lucrsure-kpi-value"><?php echo esc_html( $churn ); ?>%</strong>
		<?php echo lucrsure_render_kpi_trend( $churn_trend, true ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
	</div>
</div>

<div class="lucrsure-grid-2">
	<div class="lucrsure-card lucrsure-revenue-trend-card">
		<div class="lucrsure-card-head lucrsure-revenue-trend-head">
			<div>
				<h2><?php esc_html_e( 'Revenue Trend', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
				<p class="lucrsure-muted lucrsure-revenue-trend-sub">
					<?php
					printf(
						/* translators: %d: four-digit year */
						esc_html__( 'Monthly paid revenue · %d', 'lucrativeit-subscription-recurring-billing' ),
						absint( $chart_year )
					);
					?>
				</p>
			</div>
			<a href="<?php echo esc_url( lucrsure_admin_url( 'reports' ) ); ?>"><?php esc_html_e( 'View Report', 'lucrativeit-subscription-recurring-billing' ); ?></a>
		</div>
		<div
			id="lucrsure-revenue-trend-chart"
			class="lucrsure-revenue-trend-chart"
			data-series="<?php echo esc_attr( wp_json_encode( $trend_months ) ); ?>"
			role="img"
			aria-label="<?php esc_attr_e( 'Monthly revenue trend chart', 'lucrativeit-subscription-recurring-billing' ); ?>"
		></div>
	</div>

	<div class="lucrsure-card">
		<div class="lucrsure-card-head">
			<div>
				<h2><?php esc_html_e( 'Plan Distribution', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
				<p class="lucrsure-muted"><?php esc_html_e( 'Latest 3 plans', 'lucrativeit-subscription-recurring-billing' ); ?></p>
			</div>
		</div>
		<?php if ( empty( $latest_plans ) ) : ?>
			<p class="lucrsure-empty"><?php esc_html_e( 'No plans yet.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
		<?php else : ?>
			<ul class="lucrsure-dist">
				<?php foreach ( $latest_plans as $plan ) : ?>
					<li>
						<span><?php echo esc_html( $plan['name'] ); ?></span>
						<strong><?php echo esc_html( lucrsure_plan_format_price_display( $plan )['effective'] ); ?></strong>
					</li>
				<?php endforeach; ?>
			</ul>
		<?php endif; ?>
		<div class="lucrsure-card-foot">
			<a href="<?php echo esc_url( lucrsure_admin_url( 'plans' ) ); ?>"><?php esc_html_e( 'View all plans', 'lucrativeit-subscription-recurring-billing' ); ?></a>
		</div>
	</div>
</div>

<div class="lucrsure-grid-2">
	<div class="lucrsure-card">
		<div class="lucrsure-card-head">
			<h2><?php esc_html_e( 'Recent Subscriptions', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
			<a href="<?php echo esc_url( lucrsure_admin_url( 'subscriptions' ) ); ?>"><?php esc_html_e( 'View all', 'lucrativeit-subscription-recurring-billing' ); ?></a>
		</div>
		<?php if ( empty( $recent_subs ) ) : ?>
			<p class="lucrsure-empty"><?php esc_html_e( 'No subscriptions yet.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
		<?php else : ?>
			<table class="lucrsure-mini-table">
				<?php foreach ( $recent_subs as $sub ) : ?>
					<tr>
						<td><?php echo esc_html( $sub['customer_name'] ); ?></td>
						<td><?php echo esc_html( $sub['plan_name'] ); ?></td>
						<td><span class="lucrsure-pill lucrsure-status-<?php echo esc_attr( $sub['status'] ); ?>"><?php echo esc_html( $sub['status'] ); ?></span></td>
						<td class="lucrsure-right"><?php echo esc_html( lucrsure_format_money( $sub['mrr'] ) ); ?></td>
					</tr>
				<?php endforeach; ?>
			</table>
		<?php endif; ?>
	</div>

	<div class="lucrsure-card">
		<div class="lucrsure-card-head">
			<h2><?php esc_html_e( 'Recent Invoices', 'lucrativeit-subscription-recurring-billing' ); ?></h2>
			<a href="<?php echo esc_url( lucrsure_admin_url( 'billing' ) ); ?>"><?php esc_html_e( 'View all', 'lucrativeit-subscription-recurring-billing' ); ?></a>
		</div>
		<?php if ( empty( $recent_invoices ) ) : ?>
			<p class="lucrsure-empty"><?php esc_html_e( 'No invoices yet.', 'lucrativeit-subscription-recurring-billing' ); ?></p>
		<?php else : ?>
			<table class="lucrsure-mini-table">
				<?php foreach ( $recent_invoices as $inv ) : ?>
					<tr>
						<td>
							<a href="<?php echo esc_url( lucrsure_admin_nonce_url( 'billing', array( 'action' => 'view', 'id' => $inv['id'] ) ) ); ?>"><?php echo esc_html( $inv['invoice_number'] ); ?></a>
						</td>
						<td><?php echo esc_html( $inv['customer_name'] ); ?></td>
						<td><span class="lucrsure-pill lucrsure-status-<?php echo esc_attr( $inv['status'] ); ?>"><?php echo esc_html( $inv['status'] ); ?></span></td>
						<td class="lucrsure-right"><?php echo esc_html( lucrsure_format_money( $inv['amount'], $inv['currency'] ) ); ?></td>
					</tr>
				<?php endforeach; ?>
			</table>
		<?php endif; ?>
	</div>
</div>
