<?php
/**
 * Template part: vehicle-specs-table
 *
 * Grouped specifications table. Skips empty fields.
 *
 * @package ThemeREX\AutoDealer
 * @version 1.0.0
 *
 * @var array<string, mixed> $specs Vehicle specs array (passed via extract).
 */

declare(strict_types=1);

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

if ( empty( $specs ) || ! is_array( $specs ) ) {
	return;
}

$unit_label = \ThemeREX\AutoDealer\getSettings( 'distance_unit', 'km' );

$groups = array(
	'identification' => array(
		'label'  => __( 'Identification', 'trx-auto-dealer' ),
		'fields' => array(
			'vin'            => __( 'VIN', 'trx-auto-dealer' ),
			'stock_number'   => __( 'Stock #', 'trx-auto-dealer' ),
			'condition_type' => __( 'Condition', 'trx-auto-dealer' ),
		),
	),
	'performance'    => array(
		'label'  => __( 'Performance', 'trx-auto-dealer' ),
		'fields' => array(
			'engine'     => __( 'Engine', 'trx-auto-dealer' ),
			'horsepower' => __( 'Horsepower', 'trx-auto-dealer' ),
			'torque'     => __( 'Torque', 'trx-auto-dealer' ),
		),
	),
	'drivetrain'     => array(
		'label'  => __( 'Drivetrain', 'trx-auto-dealer' ),
		'fields' => array(
			'fuel_type'    => __( 'Fuel Type', 'trx-auto-dealer' ),
			'transmission' => __( 'Transmission', 'trx-auto-dealer' ),
			'drivetrain'   => __( 'Drive', 'trx-auto-dealer' ),
		),
	),
	'dimensions'     => array(
		'label'  => __( 'Dimensions & Capacity', 'trx-auto-dealer' ),
		'fields' => array(
			'mileage' => __( 'Mileage', 'trx-auto-dealer' ),
			'doors'   => __( 'Doors', 'trx-auto-dealer' ),
			'seats'   => __( 'Seats', 'trx-auto-dealer' ),
			'weight'  => __( 'Weight', 'trx-auto-dealer' ),
		),
	),
	'economy'        => array(
		'label'  => __( 'Fuel Economy', 'trx-auto-dealer' ),
		'fields' => array(
			'mpg_city'      => __( 'City', 'trx-auto-dealer' ),
			'mpg_highway'   => __( 'Highway', 'trx-auto-dealer' ),
			'mpg_combined'  => __( 'Combined', 'trx-auto-dealer' ),
			'co2_emissions' => __( 'CO2 Emissions', 'trx-auto-dealer' ),
		),
	),
);

/**
 * Filter the spec groups configuration.
 *
 * @param array<string, array{label: string, fields: array<string, string>}> $groups The spec groups.
 * @param array<string, mixed> $specs The vehicle specs.
 */
$groups = apply_filters( 'trx_ad_specs_table_groups', $groups, $specs );

/**
 * Format a spec value for display.
 *
 * @param string               $key   Field key.
 * @param mixed                $value Raw value.
 * @param string               $unit  Distance unit.
 * @return string Formatted value.
 */
$format_value = static function ( string $key, mixed $value, string $unit ): string {
	if ( '' === $value || null === $value ) {
		return '';
	}

	return match ( $key ) {
		'mileage'        => number_format_i18n( (int) $value ) . ' ' . $unit,
		'horsepower'     => number_format_i18n( (int) $value ) . ' hp',
		'torque'         => number_format_i18n( (int) $value ) . ' lb-ft',
		'weight'         => number_format_i18n( (int) $value ) . ' kg',
		'mpg_city',
		'mpg_highway',
		'mpg_combined'   => number_format( (float) $value, 1 ) . ' mpg',
		'co2_emissions'  => number_format( (float) $value, 0 ) . ' g/km',
		'condition_type',
		'fuel_type',
		'transmission'   => ucfirst( (string) $value ),
		// Drivetrain values are acronyms (AWD/FWD/RWD/4WD) — ucfirst() would
		// render the lowercased spec-table copy as "Awd".
		'drivetrain'     => strtoupper( (string) $value ),
		default          => (string) $value,
	};
};

$has_content = false;

foreach ( $groups as $group ) {
	foreach ( $group['fields'] as $key => $label ) {
		$raw = $specs[ $key ] ?? '';
		if ( '' !== $raw && null !== $raw && 0 !== $raw ) {
			$has_content = true;
			break 2;
		}
	}
}

if ( ! $has_content ) {
	return;
}
?>

<div class="trx-ad-specs-table">
	<h2 class="trx-ad-specs-table__heading"><?php esc_html_e( 'Specifications', 'trx-auto-dealer' ); ?></h2>

	<?php foreach ( $groups as $group_key => $group ) : ?>
		<?php
		// Check if group has any non-empty fields.
		$group_has_values = false;
		foreach ( $group['fields'] as $key => $label ) {
			$raw = $specs[ $key ] ?? '';
			if ( '' !== (string) $raw && '0' !== (string) $raw ) {
				$group_has_values = true;
				break;
			}
		}

		if ( ! $group_has_values ) {
			continue;
		}
		?>

		<div class="trx-ad-specs-table__group">
			<h3 class="trx-ad-specs-table__group-title"><?php echo esc_html( $group['label'] ); ?></h3>
			<table class="trx-ad-specs-table__table">
				<tbody>
					<?php
					foreach ( $group['fields'] as $key => $label ) :
						?>
						<?php
						$raw       = $specs[ $key ] ?? '';
						$formatted = $format_value( $key, $raw, $unit_label );

						if ( '' === $formatted ) {
							continue;
						}
						?>
						<tr class="trx-ad-specs-table__row">
							<th class="trx-ad-specs-table__label" scope="row"><?php echo esc_html( $label ); ?></th>
							<td class="trx-ad-specs-table__value"><?php echo esc_html( $formatted ); ?></td>
						</tr>
					<?php endforeach; ?>
				</tbody>
			</table>
		</div>

	<?php endforeach; ?>
</div>
