<?php
/**
 * Template: pdf-spec-sheet
 *
 * Rendered through dompdf — must be self-contained, no external CSS, no
 * JavaScript. dompdf supports a subset of CSS 2.1 and limited HTML5; keep
 * the markup minimal and inline the styles.
 *
 * Variables in scope (set up by PdfController::buildHtml):
 *   $post           \WP_Post
 *   $specs          array<string,mixed>  (the wp_trx_ad_vehicle_specs row)
 *   $dealer         array<string,string> Dealer meta (name + contact + address)
 *   $images         string[]             File paths (not URLs) of gallery images
 *   $currency       string               Currency symbol
 *   $generated_at   string               Localised timestamp
 *
 * @package ThemeREX\AutoDealer
 * @version 1.0.0
 */

declare(strict_types=1);

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

/** @var \WP_Post $post */
/** @var array<string,mixed> $specs */
/** @var array<string,string> $dealer */
/** @var string[] $images */
/** @var string $currency */
/** @var string $generated_at */

$year         = isset( $specs['year'] ) ? (int) $specs['year'] : '';
$make         = $specs['make'] ?? '';
$model        = $specs['model'] ?? '';
$title        = trim( "{$year} {$make} {$model}" ) ?: $post->post_title;
$price        = isset( $specs['price'] ) ? (float) $specs['price'] : 0.0;
$sale_price   = isset( $specs['sale_price'] ) && '' !== $specs['sale_price'] ? (float) $specs['sale_price'] : 0.0;
$on_request   = ! empty( $specs['price_on_request'] );
$lead_image   = ! empty( $images ) ? (string) $images[0] : '';

// Format helpers — kept inline so the template stays portable for dompdf.
$fmt_money = static function ( float $n ) use ( $currency ): string {
	return $currency . number_format( $n, 0, '.', ',' );
};
$fmt_int = static fn( $n ) => null === $n || '' === $n ? '—' : number_format( (float) $n, 0, '.', ',' );

$rows = array(
	array( __( 'Year', 'trx-auto-dealer' ), (string) $year ),
	array( __( 'VIN', 'trx-auto-dealer' ), (string) ( $specs['vin'] ?? '' ) ),
	array( __( 'Stock #', 'trx-auto-dealer' ), (string) ( $specs['stock_number'] ?? '' ) ),
	array( __( 'Mileage', 'trx-auto-dealer' ), $fmt_int( $specs['mileage'] ?? null ) ),
	array( __( 'Body style', 'trx-auto-dealer' ), (string) ( $specs['body_style'] ?? '' ) ),
	array( __( 'Fuel', 'trx-auto-dealer' ), (string) ( $specs['fuel_type'] ?? '' ) ),
	array( __( 'Transmission', 'trx-auto-dealer' ), (string) ( $specs['transmission'] ?? '' ) ),
	array( __( 'Drivetrain', 'trx-auto-dealer' ), (string) ( $specs['drivetrain'] ?? '' ) ),
	array( __( 'Engine', 'trx-auto-dealer' ), (string) ( $specs['engine_size'] ?? '' ) ),
	array( __( 'Horsepower', 'trx-auto-dealer' ), $fmt_int( $specs['horsepower'] ?? null ) ),
	array( __( 'Doors', 'trx-auto-dealer' ), $fmt_int( $specs['doors'] ?? null ) ),
	array( __( 'Seats', 'trx-auto-dealer' ), $fmt_int( $specs['seats'] ?? null ) ),
	array( __( 'Exterior', 'trx-auto-dealer' ), (string) ( $specs['color_ext'] ?? '' ) ),
	array( __( 'Condition', 'trx-auto-dealer' ), (string) ( $specs['condition_type'] ?? '' ) ),
);
$rows = array_values( array_filter( $rows, static fn( $r ) => '' !== $r[1] && '—' !== $r[1] ) );
?>
<!DOCTYPE html>
<html lang="<?php echo esc_attr( get_locale() ); ?>">
<head>
<meta charset="utf-8">
<title><?php echo esc_html( $title ); ?></title>
<style>
	@page { margin: 18mm; }
	body { font-family: DejaVu Sans, Arial, sans-serif; color: #1a202c; font-size: 10.5pt; }
	h1 { font-size: 18pt; margin: 0 0 4pt; }
	.meta { color: #6b7280; font-size: 9pt; margin-bottom: 12pt; }
	.lead { width: 100%; max-height: 200pt; margin-bottom: 14pt; }
	.lead img { width: 100%; max-height: 200pt; object-fit: cover; border-radius: 4pt; }
	.price-row { display: table; width: 100%; margin: 8pt 0 14pt; padding: 8pt 0; border-top: 1px solid #e5e7eb; border-bottom: 1px solid #e5e7eb; }
	.price-row .price { display: table-cell; font-size: 16pt; font-weight: bold; color: #1a56db; }
	.price-row .sale  { display: table-cell; text-align: right; font-size: 10pt; color: #6b7280; text-decoration: line-through; }
	table.specs { width: 100%; border-collapse: collapse; }
	table.specs td { padding: 5pt 6pt; border-bottom: 1px solid #f1f1f3; vertical-align: top; }
	table.specs td.label { width: 35%; color: #6b7280; }
	table.specs td.value { font-weight: 600; }
	.dealer { margin-top: 14pt; padding: 10pt; background: #f9fafb; border-radius: 4pt; }
	.dealer h3 { margin: 0 0 4pt; font-size: 11pt; }
	.dealer p  { margin: 0; line-height: 1.5; color: #374151; }
	.gallery { margin-top: 12pt; }
	.gallery img { width: 32%; height: 80pt; object-fit: cover; margin-right: 1%; margin-bottom: 6pt; border-radius: 3pt; }
	.footer { margin-top: 16pt; padding-top: 8pt; border-top: 1px solid #e5e7eb; color: #9ca3af; font-size: 8pt; }
</style>
</head>
<body>

<h1><?php echo esc_html( $title ); ?></h1>
<div class="meta">
	<?php
	if ( ! empty( $specs['stock_number'] ) ) {
		echo esc_html( __( 'Stock #', 'trx-auto-dealer' ) . ' ' . (string) $specs['stock_number'] );
		echo ' &middot; ';
	}
	echo esc_html( $generated_at );
	?>
</div>

<?php if ( '' !== $lead_image ) : ?>
	<div class="lead">
		<img src="<?php echo esc_attr( $lead_image ); ?>" alt="">
	</div>
<?php endif; ?>

<div class="price-row">
	<?php if ( $on_request ) : ?>
		<span class="price"><?php esc_html_e( 'Price on request', 'trx-auto-dealer' ); ?></span>
	<?php elseif ( $sale_price > 0 && $price > 0 ) : ?>
		<span class="price"><?php echo esc_html( $fmt_money( $sale_price ) ); ?></span>
		<span class="sale"><?php echo esc_html( $fmt_money( $price ) ); ?></span>
	<?php elseif ( $price > 0 ) : ?>
		<span class="price"><?php echo esc_html( $fmt_money( $price ) ); ?></span>
	<?php endif; ?>
</div>

<table class="specs">
	<?php foreach ( $rows as $row ) : ?>
		<tr>
			<td class="label"><?php echo esc_html( $row[0] ); ?></td>
			<td class="value"><?php echo esc_html( $row[1] ); ?></td>
		</tr>
	<?php endforeach; ?>
</table>

<?php if ( ! empty( $dealer['name'] ?? '' ) ) : ?>
	<div class="dealer">
		<h3><?php echo esc_html( (string) $dealer['name'] ); ?></h3>
		<p>
			<?php
			$bits = array_filter(
				array(
					$dealer['phone'] ?? '',
					$dealer['email'] ?? '',
					$dealer['street_address'] ?? '',
					$dealer['city'] ?? '',
					$dealer['country'] ?? '',
				)
			);
			echo esc_html( implode( ' · ', $bits ) );
			?>
		</p>
	</div>
<?php endif; ?>

<?php
$rest = array_slice( $images, 1, 6 );
if ( ! empty( $rest ) ) :
	?>
	<div class="gallery">
		<?php foreach ( $rest as $path ) : ?>
			<img src="<?php echo esc_attr( (string) $path ); ?>" alt="">
		<?php endforeach; ?>
	</div>
<?php endif; ?>

<div class="footer">
	<?php
	printf(
		/* translators: 1: site name 2: site URL */
		esc_html__( 'Generated by %1$s — %2$s', 'trx-auto-dealer' ),
		esc_html( get_bloginfo( 'name' ) ),
		esc_html( home_url( '/' ) )
	);
	?>
</div>

</body>
</html>
