<?php
/**
 * Plain Text Email Template for Scheduled Reports
 *
 * Available variables:
 * - $report: array - Report data from generator
 * - $schedule: array - Schedule configuration
 *
 * @package opti-behavior
 * @since 1.1.0
 */

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

// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$kpis          = $report['sections']['kpis'] ?? array();
$top_pages     = $report['sections']['top_pages'] ?? array();
$top_referrers = $report['sections']['top_referrers'] ?? array();
$traffic       = $report['sections']['traffic_breakdown'] ?? array();
$geographic    = $report['sections']['geographic'] ?? array();
$heatmap       = $report['sections']['heatmap_summary'] ?? array();
$funnels       = $report['sections']['funnels'] ?? array();
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

// Pro sections
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
$recordings     = $report['sections']['recordings'] ?? array();
$errors         = $report['sections']['errors'] ?? array();
$friction       = $report['sections']['friction'] ?? array();
$performance    = $report['sections']['performance'] ?? array();
$broken_links   = $report['sections']['broken_links'] ?? array();
$user_journeys  = $report['sections']['user_journeys'] ?? array();
$form_analytics = $report['sections']['form_analytics'] ?? array();
// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

// Header
echo esc_html( strtoupper( $schedule['name'] ) ) . "\n";
echo esc_html( str_repeat( '=', strlen( $schedule['name'] ) ) ) . "\n\n";
echo esc_html( $report['period_label'] ) . ' | ' . esc_html( $report['site_name'] ) . "\n\n";

// KPIs Section
if ( ! empty( $kpis ) ) {
	echo "KEY PERFORMANCE INDICATORS\n";
	echo esc_html( str_repeat( '-', 28 ) ) . "\n\n";

	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$kpi_labels = array(
		'visitors'         => __( 'Visitors', 'opti-behavior' ),
		'sessions'         => __( 'Sessions', 'opti-behavior' ),
		'pageviews'        => __( 'Page Views', 'opti-behavior' ),
		'bounce_rate'      => __( 'Bounce Rate', 'opti-behavior' ),
		'avg_session_time' => __( 'Avg. Session Time', 'opti-behavior' ),
		'avg_scroll_depth' => __( 'Avg. Scroll Depth', 'opti-behavior' ),
	);

	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	foreach ( $kpi_labels as $key => $label ) {
		if ( ! isset( $kpis[ $key ] ) ) {
			continue;
		}
		// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		$kpi = $kpis[ $key ];

		// Format value
		if ( 'avg_session_time' === $key && isset( $kpi['formatted'] ) ) {
			// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			$value = $kpi['formatted'];
		} elseif ( in_array( $key, array( 'bounce_rate', 'avg_scroll_depth' ), true ) ) {
			$value = $kpi['value'] . '%';
		} else {
			$value = number_format( $kpi['value'] );
			// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		}

		// Change indicator
		// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		$change_str = '';
		if ( ! empty( $kpi['change']['value'] ) ) {
			$arrow      = ( $kpi['change']['direction'] ?? '' ) === 'up' ? '^' : 'v';
			$change_str = ' (' . $arrow . ' ' . $kpi['change']['value'] . '%)';
			// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		}

		echo sprintf( "  %-20s %s%s\n", esc_html( $label ) . ':', esc_html( $value ), esc_html( $change_str ) );
	}
	echo "\n";
}

// Top Pages Section
if ( ! empty( $top_pages ) ) {
	echo "TOP PAGES\n";
	echo esc_html( str_repeat( '-', 10 ) ) . "\n\n";

	// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	foreach ( array_slice( $top_pages, 0, 5 ) as $i => $page ) {
		$title = mb_strimwidth( $page['title'], 0, 50, '...' );
		$views = number_format( $page['views'] );
		$time  = $page['avg_time_formatted'] ?? '-';
		// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

		echo sprintf( "  %d. %s\n", intval( $i + 1 ), esc_html( $title ) );
		echo sprintf( "     Views: %s | Avg. Time: %s\n\n", esc_html( $views ), esc_html( $time ) );
	}
}

// Top Referrers Section
if ( ! empty( $top_referrers ) ) {
	echo "TOP REFERRERS\n";
	echo esc_html( str_repeat( '-', 13 ) ) . "\n\n";

	// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	foreach ( array_slice( $top_referrers, 0, 5 ) as $i => $ref ) {
		$source = $ref['source'];
		$visits = number_format( $ref['visits'] );
		// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

		echo sprintf( "  %d. %-30s %s visits\n", intval( $i + 1 ), esc_html( $source ), esc_html( $visits ) );
	}
	echo "\n";
}

// Traffic Breakdown Section
if ( ! empty( $traffic ) && ! empty( $traffic['breakdown'] ) ) {
	echo "TRAFFIC BREAKDOWN\n";
	echo esc_html( str_repeat( '-', 17 ) ) . "\n\n";

	// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	foreach ( array_slice( $traffic['breakdown'], 0, 6 ) as $item ) {
		$type  = ucfirst( $item['type'] ?? __( 'Unknown', 'opti-behavior' ) );
		$count = number_format( $item['count'] );
		$pct   = $item['percentage'] ?? 0;
		// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

		echo sprintf( "  %-20s %s (%s%%)\n", esc_html( $type ) . ':', esc_html( $count ), esc_html( $pct ) );
	}
	echo "\n";
}

// Geographic Distribution Section
if ( ! empty( $geographic ) ) {
	echo "GEOGRAPHIC DISTRIBUTION\n";
	echo esc_html( str_repeat( '-', 23 ) ) . "\n\n";

	// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	foreach ( array_slice( $geographic, 0, 5 ) as $i => $country ) {
		$name     = $country['country_name'] ?? $country['country'] ?? __( 'Unknown', 'opti-behavior' );
		$visitors = number_format( $country['visitors'] );
		$sessions = number_format( $country['sessions'] );
		// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

		echo sprintf( "  %d. %-25s %s visitors, %s sessions\n", intval( $i + 1 ), esc_html( $name ), esc_html( $visitors ), esc_html( $sessions ) );
	}
	echo "\n";
}

// Heatmap Summary Section
if ( ! empty( $heatmap ) ) {
	echo "CLICK HEATMAP SUMMARY\n";
	echo esc_html( str_repeat( '-', 21 ) ) . "\n\n";

	echo sprintf( "  Total Clicks:       %s\n", esc_html( number_format( $heatmap['total_clicks'] ?? 0 ) ) );
	echo sprintf( "  Pages Tracked:      %s\n", esc_html( number_format( $heatmap['pages_tracked'] ?? 0 ) ) );

	if ( ! empty( $heatmap['top_clicked'] ) ) {
		echo "\n  Most Clicked Pages:\n";
		// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		foreach ( array_slice( $heatmap['top_clicked'], 0, 3 ) as $i => $clicked ) {
			$title  = mb_strimwidth( $clicked['title'], 0, 40, '...' );
			$clicks = number_format( $clicked['clicks'] );
			// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			echo sprintf( "    %d. %s (%s clicks)\n", intval( $i + 1 ), esc_html( $title ), esc_html( $clicks ) );
		}
	}
	echo "\n";
}

// Funnel Performance Section
if ( ! empty( $funnels ) && ! empty( $funnels['total_funnels'] ) ) {
	echo "FUNNEL PERFORMANCE\n";
	echo esc_html( str_repeat( '-', 18 ) ) . "\n\n";

	echo sprintf( "  Active Funnels:     %s\n\n", esc_html( $funnels['total_funnels'] ) );

	if ( ! empty( $funnels['funnels'] ) ) {
		// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		foreach ( $funnels['funnels'] as $i => $funnel ) {
			$name    = mb_strimwidth( $funnel['name'], 0, 35, '...' );
			$entries = number_format( $funnel['total_entries'] );
			$compl   = number_format( $funnel['completions'] );
			$rate    = number_format( $funnel['conversion_rate'] ?? 0, 1 ) . '%';
			// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound

			echo sprintf( "  %d. %-35s\n", intval( $i + 1 ), esc_html( $name ) );
			echo sprintf( "     Entries: %s | Completions: %s | Conv: %s\n\n", esc_html( $entries ), esc_html( $compl ), esc_html( $rate ) );
		}
	}
}

// Session Recordings Stats (Pro)
if ( $report['is_pro'] && ! empty( $recordings ) ) {
	echo "SESSION RECORDINGS (Pro)\n";
	echo esc_html( str_repeat( '-', 24 ) ) . "\n\n";

	echo sprintf( "  Total Recordings:   %s\n", esc_html( number_format( $recordings['total'] ) ) );
	echo sprintf( "  Watched:            %s\n", esc_html( number_format( $recordings['watched'] ) ) );
	echo sprintf( "  Unwatched:          %s\n", esc_html( number_format( $recordings['unwatched'] ) ) );
	echo sprintf( "  Avg. Duration:      %s\n", esc_html( $recordings['avg_duration_formatted'] ) );
	echo "\n";
}

// Errors Section (Pro)
if ( $report['is_pro'] && ! empty( $errors ) ) {
	echo "JAVASCRIPT ERRORS (Pro)\n";
	echo esc_html( str_repeat( '-', 23 ) ) . "\n\n";

	echo sprintf( "  Total Errors:     %s\n", esc_html( number_format( $errors['total'] ) ) );
	echo sprintf( "  Unresolved:       %s\n", esc_html( number_format( $errors['unresolved'] ) ) );
	echo "\n";
}

// Performance Section (Pro)
if ( $report['is_pro'] && ! empty( $performance ) ) {
	echo "WEB VITALS (Pro)\n";
	echo esc_html( str_repeat( '-', 16 ) ) . "\n\n";

	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$vitals = array(
		'lcp' => array( 'label' => 'LCP (Largest Contentful Paint)', 'suffix' => 's' ),
		'fid' => array( 'label' => 'FID (First Input Delay)', 'suffix' => 'ms' ),
		'cls' => array( 'label' => 'CLS (Cumulative Layout Shift)', 'suffix' => '' ),
		'inp' => array( 'label' => 'INP (Interaction to Next Paint)', 'suffix' => 'ms' ),
	);

	// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	foreach ( $vitals as $key => $config ) {
		if ( isset( $performance[ $key ] ) ) {
			$value = $performance[ $key ] . $config['suffix'];
			// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			echo sprintf( "  %-35s %s\n", esc_html( $config['label'] ) . ':', esc_html( $value ) );
		}
	}
	echo "\n";
}

// Friction Events Section (Pro)
if ( $report['is_pro'] && ! empty( $friction ) ) {
	echo "FRICTION EVENTS (Pro)\n";
	echo esc_html( str_repeat( '-', 21 ) ) . "\n\n";

	echo sprintf( "  Total Events:       %s\n", esc_html( number_format( $friction['total'] ) ) );
	echo sprintf( "  Rage Clicks:        %s\n", esc_html( number_format( $friction['rage_clicks'] ) ) );
	echo sprintf( "  Dead Clicks:        %s\n", esc_html( number_format( $friction['dead_clicks'] ) ) );
	echo "\n";
}

// Broken Links Section (Pro)
if ( $report['is_pro'] && ! empty( $broken_links ) ) {
	echo "BROKEN LINKS REPORT (Pro)\n";
	echo esc_html( str_repeat( '-', 25 ) ) . "\n\n";

	echo sprintf( "  Total Found:        %s\n", esc_html( number_format( $broken_links['total'] ) ) );
	echo sprintf( "  Open:               %s\n", esc_html( number_format( $broken_links['open'] ) ) );
	echo sprintf( "  Fixed:              %s\n", esc_html( number_format( $broken_links['fixed'] ) ) );

	if ( ! empty( $broken_links['top_links'] ) ) {
		echo "\n  Top Broken Links:\n";
		// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		foreach ( array_slice( $broken_links['top_links'], 0, 3 ) as $i => $link ) {
			$url   = mb_strimwidth( $link['url'], 0, 50, '...' );
			$status = $link['http_status'];
			$hits  = number_format( $link['occurrence_count'] );
			// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			echo sprintf( "    %d. %s (HTTP %s, %s hits)\n", intval( $i + 1 ), esc_html( $url ), esc_html( $status ), esc_html( $hits ) );
		}
	}
	echo "\n";
}

// User Journeys Section (Pro)
if ( $report['is_pro'] && ! empty( $user_journeys ) ) {
	echo "USER JOURNEYS (Pro)\n";
	echo esc_html( str_repeat( '-', 19 ) ) . "\n\n";

	echo sprintf( "  Sessions Analyzed:  %s\n", esc_html( number_format( $user_journeys['total_sessions'] ?? 0 ) ) );
	echo sprintf( "  Avg. Pages/Session: %s\n", esc_html( number_format( $user_journeys['avg_path_length'] ?? 0, 1 ) ) );

	// Depth distribution
	// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
	$depth = $user_journeys['depth'] ?? array();
	if ( ! empty( $depth ) ) {
		echo "\n  Session Depth:\n";
		echo sprintf( "    1 Page:     %s sessions\n", esc_html( number_format( $depth['1_page'] ?? 0 ) ) );
		echo sprintf( "    2-3 Pages:  %s sessions\n", esc_html( number_format( $depth['2_3_pages'] ?? 0 ) ) );
		echo sprintf( "    4+ Pages:   %s sessions\n", esc_html( number_format( $depth['4_plus_pages'] ?? 0 ) ) );
	}

	// Top entry pages
	if ( ! empty( $user_journeys['entry_pages'] ) ) {
		echo "\n  Top Entry Pages:\n";
		// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		foreach ( array_slice( $user_journeys['entry_pages'], 0, 5 ) as $i => $page ) {
			$url      = mb_strimwidth( $page['url'] ?? '', 0, 40, '...' );
			$sessions = number_format( $page['sessions'] ?? 0 );
			// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			echo sprintf( "    %d. %-40s %s sessions\n", intval( $i + 1 ), esc_html( $url ), esc_html( $sessions ) );
		}
	}

	// Top exit pages
	if ( ! empty( $user_journeys['exit_pages'] ) ) {
		echo "\n  Top Exit Pages:\n";
		// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		foreach ( array_slice( $user_journeys['exit_pages'], 0, 5 ) as $i => $page ) {
			$url       = mb_strimwidth( $page['url'] ?? '', 0, 35, '...' );
			$sessions  = number_format( $page['sessions'] ?? 0 );
			$exit_rate = number_format( $page['exit_rate'] ?? 0, 1 ) . '%';
			// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			echo sprintf( "    %d. %-35s %s sessions (%s exit)\n", intval( $i + 1 ), esc_html( $url ), esc_html( $sessions ), esc_html( $exit_rate ) );
		}
	}
	echo "\n";
}

// Form Analytics Section (Pro)
if ( $report['is_pro'] && ! empty( $form_analytics ) ) {
	echo "FORM ANALYTICS (Pro)\n";
	echo esc_html( str_repeat( '-', 20 ) ) . "\n\n";

	echo sprintf( "  Form Views:         %s\n", esc_html( number_format( $form_analytics['form_views'] ?? 0 ) ) );
	echo sprintf( "  Submissions:        %s\n", esc_html( number_format( $form_analytics['submissions'] ?? 0 ) ) );
	echo sprintf( "  Abandonments:       %s\n", esc_html( number_format( $form_analytics['abandonments'] ?? 0 ) ) );
	echo sprintf( "  Conversion Rate:    %s%%\n", esc_html( number_format( $form_analytics['conversion_rate'] ?? 0, 1 ) ) );
	echo sprintf( "  Avg. Comp. Time:    %s\n", esc_html( $form_analytics['avg_completion_time_formatted'] ?? '—' ) );

	if ( ! empty( $form_analytics['top_forms'] ) ) {
		echo "\n  Top Forms by Conversion:\n";
		// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
		foreach ( array_slice( $form_analytics['top_forms'], 0, 5 ) as $i => $form ) {
			$name       = mb_strimwidth( $form['form_name'] ?? __( 'Unknown', 'opti-behavior' ), 0, 30, '...' );
			$views      = number_format( $form['form_views'] ?? 0 );
			$conv_rate  = number_format( $form['conversion_rate'] ?? 0, 1 ) . '%';
			// phpcs:enable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
			echo sprintf( "    %d. %-30s %s views, %s conv.\n", intval( $i + 1 ), esc_html( $name ), esc_html( $views ), esc_html( $conv_rate ) );
		}
	}
	echo "\n";
}

// CTA
echo esc_html( str_repeat( '=', 50 ) ) . "\n\n";
echo esc_html( __( 'View Full Dashboard', 'opti-behavior' ) ) . ":\n";
echo esc_url( admin_url( 'admin.php?page=opti-behavior-analytics' ) ) . "\n\n";

// Footer
echo esc_html( str_repeat( '-', 50 ) ) . "\n";
echo esc_html( __( 'This report was automatically generated by Opti-Behavior', 'opti-behavior' ) ) . "\n";
echo esc_html( __( 'Manage report settings', 'opti-behavior' ) ) . ': ' . esc_url( admin_url( 'admin.php?page=opti-behavior-settings&settings_tab=scheduled-reports' ) ) . "\n";
echo esc_html( $report['site_name'] ) . ' | ' . esc_url( home_url() ) . "\n";
