<?php

defined( 'ABSPATH' ) || exit;

class ZACLS_PDF {

	public static function generate( $html, $filename = 'report.pdf' ) {
		if ( ! class_exists( 'Dompdf\\Dompdf' ) ) {
			$dompdf_path = ZACLS_PLUGIN_DIR . 'vendor/autoload.php';
			if ( file_exists( $dompdf_path ) ) {
				require_once $dompdf_path;
			} else {
				return self::generate_html_fallback( $html, $filename );
			}
		}

		if ( class_exists( 'Dompdf\\Dompdf' ) ) {
			try {
				$dompdf = new Dompdf\Dompdf();
				$dompdf->loadHtml( $html );
				$dompdf->setPaper( 'A4', 'portrait' );
				$dompdf->render();
				$dompdf->stream( $filename, array( 'Attachment' => true ) );
				exit;
			} catch ( \Exception $e ) {
				return self::generate_html_fallback( $html, $filename );
			}
		}

		return self::generate_html_fallback( $html, $filename );
	}

	private static function generate_html_fallback( $html, $filename ) {
		if ( ob_get_level() ) {
			ob_end_clean();
		}

		nocache_headers();
		header( 'Content-Type: text/html; charset=UTF-8' );
		header( 'Content-Disposition: attachment; filename="' . str_replace( '.pdf', '.html', $filename ) . '"' );
		echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- HTML document for file download, needs all tags
		exit;
	}

	public static function generate_login_report( $days = 30 ) {
		$stats     = ZACLS_Analytics::get_summary_stats( $days );
		$activity  = ZACLS_Analytics::get_login_activity( min( $days, 30 ) );
		$attackers = ZACLS_Analytics::get_top_attackers( 10, $days );
		$health    = ZACLS_Health::calculate_score();

		$html = '<html><head><meta charset="UTF-8"><style>
			body { font-family: DejaVu Sans, sans-serif; font-size: 12px; color: #333; padding: 20px; }
			h1 { color: #2271b1; font-size: 22px; border-bottom: 2px solid #2271b1; padding-bottom: 8px; }
			h2 { color: #1d2327; font-size: 16px; margin-top: 20px; }
			table { width: 100%; border-collapse: collapse; margin: 12px 0; }
			th, td { padding: 8px 10px; text-align: left; border: 1px solid #ddd; font-size: 11px; }
			th { background: #f0f0f1; font-weight: 600; }
			.score { font-size: 36px; font-weight: 700; color: ' . ( $health['score'] >= 80 ? '#00a32a' : ( $health['score'] >= 50 ? '#dba617' : '#d63638' ) ) . '; }
			.footer { margin-top: 30px; font-size: 10px; color: #999; text-align: center; border-top: 1px solid #ddd; padding-top: 10px; }
		</style></head><body>
		<h1>' . esc_html__( 'Security Report', 'za-creative-login-shield' ) . '</h1>
		<p>' . sprintf( esc_html__( 'Report period: Last %d days', 'za-creative-login-shield' ), $days ) . '</p>
		<p>' . sprintf( esc_html__( 'Generated: %s', 'za-creative-login-shield' ), current_time( 'mysql' ) ) . '</p>

		<h2>' . esc_html__( 'Security Score', 'za-creative-login-shield' ) . '</h2>
		<div class="score">' . esc_html( $health['score'] ) . '%</div>

		<h2>' . esc_html__( 'Summary Statistics', 'za-creative-login-shield' ) . '</h2>
		<table>
			<tr><th>' . esc_html__( 'Metric', 'za-creative-login-shield' ) . '</th><th>' . esc_html__( 'Value', 'za-creative-login-shield' ) . '</th></tr>
			<tr><td>' . esc_html__( 'Total Attempts', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $stats['total'] ) . '</td></tr>
			<tr><td>' . esc_html__( 'Failed', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $stats['failed'] ) . '</td></tr>
			<tr><td>' . esc_html__( 'Successful', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $stats['success'] ) . '</td></tr>
			<tr><td>' . esc_html__( 'Blocked IPs', 'za-creative-login-shield' ) . '</td><td>' . esc_html( $stats['blocked'] ) . '</td></tr>
		</table>';

		if ( ! empty( $attackers ) ) {
			$html .= '<h2>' . esc_html__( 'Top Attackers', 'za-creative-login-shield' ) . '</h2><table><tr><th>' . esc_html__( 'IP', 'za-creative-login-shield' ) . '</th><th>' . esc_html__( 'Attempts', 'za-creative-login-shield' ) . '</th><th>' . esc_html__( 'Last Attempt', 'za-creative-login-shield' ) . '</th></tr>';
			foreach ( $attackers as $a ) {
				$html .= '<tr><td>' . esc_html( $a['ip'] ) . '</td><td>' . esc_html( $a['attempts'] ) . '</td><td>' . esc_html( $a['last_attempt'] ) . '</td></tr>';
			}
			$html .= '</table>';
		}

		$html .= '<div class="footer">' . sprintf( esc_html__( 'Generated by ZA Creative Login Shield v%s', 'za-creative-login-shield' ), ZACLS_VERSION ) . '</div>
		</body></html>';

		return $html;
	}
}
