<?php
/**
 * Camera detail partial — Hero strip.
 *
 * Top band of the expanded detail: trail camera identity (ID, model, location)
 * on the left, at-a-glance stat tiles (battery, days, alerts, value, status)
 * on the right. Replaces the scattered "label: value" rows that previously
 * lived across 4 separate tables.
 *
 * Expected variables in scope (via include):
 *   $mtc_cam_id, $mtc_cam, $mtc_days_in_field, $mtc_detection_count,
 *   $mtc_battery_cached
 *
 * @package My Trail Camera
 * @since   8.2.4
 */

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

call_user_func( function () use ( $mtc_cam_id, $mtc_cam, $mtc_days_in_field, $mtc_detection_count, $mtc_battery_cached ) {

// Resolve model label (profile > custom profile > manufacturer).
$profile_id  = $mtc_cam['profile'] ?? '';
$model_label = '';
if ( ! empty( $profile_id ) && $profile_id !== 'custom' ) {
	$custom_profiles = get_option( 'mtcam_custom_profiles', array() );
	if ( isset( $custom_profiles[ $profile_id ] ) ) {
		$model_label = $custom_profiles[ $profile_id ]['label'] ?? '';
	} elseif ( function_exists( 'mtcam_get_camera_profile' ) ) {
		$profile = mtcam_get_camera_profile( $profile_id );
		if ( ( $profile['label'] ?? '' ) !== __( 'Custom camera', 'my-trail-camera' ) ) {
			$model_label = $profile['label'] ?? '';
		}
	}
}
if ( empty( $model_label ) && ! empty( $mtc_cam['manufacturer'] ) ) {
	$model_label = $mtc_cam['manufacturer'];
}

$total_value = function_exists( 'mtcam_get_camera_total_value' )
	? mtcam_get_camera_total_value( $mtc_cam )
	: floatval( $mtc_cam['price'] ?? 0 );
$price       = floatval( $mtc_cam['price'] ?? 0 );
$value_title = '';
if ( $total_value > $price && $price > 0 ) {
	$value_title = sprintf(
		/* translators: 1: camera price, 2: accessories total */
		__( 'Camera %1$s € + accessories %2$s €', 'my-trail-camera' ),
		number_format( $price, 2, ',', ' ' ),
		number_format( $total_value - $price, 2, ',', ' ' )
	);
}
?>
<div class="mtcam-detail-hero">
    <div class="mtcam-detail-hero-identity">
        <div class="mtcam-detail-hero-idrow">
            <?php if ( mtcam_is_my_camera( $mtc_cam ) ) : ?>
                <code class="mtcam-camera-badge mtcam-camera-badge-lg"><?php echo esc_html( $mtc_cam_id ); ?></code>
            <?php else : ?>
                <code class="mtcam-camera-badge mtcam-camera-badge-lg mtcam-badge-hidden">●●●●●●</code>
            <?php endif; ?>
            <?php mtcam_render_owner_badge( $mtc_cam ); ?>
            <?php
            $pt = $mtc_cam['power_type'] ?? 'lithium';
            echo '<span class="mtcam-detail-hero-power" title="' . esc_attr( mtcam_get_power_label( $pt ) ) . '">' . esc_html( mtcam_get_power_icon( $pt ) ) . '</span>';
            ?>
        </div>
        <?php if ( $model_label ) : ?>
            <div class="mtcam-detail-hero-model"><?php echo esc_html( $model_label ); ?></div>
        <?php endif; ?>
        <div class="mtcam-detail-hero-location">
            <?php
            if ( mtcam_is_active( $mtc_cam ) && ! empty( $mtc_cam['gps_lat'] ) && ! empty( $mtc_cam['gps_lon'] ) ) {
                $maps_url = 'https://www.google.com/maps?q=' . rawurlencode( $mtc_cam['gps_lat'] ) . ',' . rawurlencode( $mtc_cam['gps_lon'] );
                echo '📍 <a href="' . esc_url( $maps_url ) . '" target="_blank" class="mtcam-link-map">' . esc_html( $mtc_cam['location'] ) . ' 🗺️</a>';
            } else {
                echo '📍 ' . esc_html( $mtc_cam['location'] );
            }
            ?>
        </div>
    </div>

    <div class="mtcam-detail-hero-stats">
        <?php if ( $mtc_battery_cached !== null ) :
            $bp = floatval( $mtc_battery_cached['battery_pct'] );
            $bc = mtcam_battery_color( $bp );
        ?>
        <div class="mtcam-stat-tile">
            <span class="mtcam-stat-tile-icon">🔋</span>
            <span class="mtcam-stat-tile-value" style="color:<?php echo esc_attr( $bc ); ?>;"><?php echo esc_html( number_format( $bp, 0 ) ); ?>%</span>
            <span class="mtcam-stat-tile-label"><?php esc_html_e( 'Battery', 'my-trail-camera' ); ?></span>
        </div>
        <?php endif; ?>

        <?php if ( ! empty( $mtc_cam['deploy_date'] ) ) : ?>
        <div class="mtcam-stat-tile">
            <span class="mtcam-stat-tile-icon">📅</span>
            <span class="mtcam-stat-tile-value"><?php echo intval( $mtc_days_in_field ); ?></span>
            <span class="mtcam-stat-tile-label"><?php esc_html_e( 'Days in field', 'my-trail-camera' ); ?></span>
        </div>
        <?php endif; ?>

        <div class="mtcam-stat-tile<?php echo $mtc_detection_count > 0 ? ' mtcam-stat-tile-alert' : ''; ?>">
            <span class="mtcam-stat-tile-icon">🚨</span>
            <span class="mtcam-stat-tile-value"><?php echo intval( $mtc_detection_count ); ?></span>
            <span class="mtcam-stat-tile-label"><?php esc_html_e( 'Alerts', 'my-trail-camera' ); ?></span>
        </div>

        <?php if ( $total_value > 0 ) : ?>
        <div class="mtcam-stat-tile"<?php if ( $value_title ) echo ' title="' . esc_attr( $value_title ) . '"'; ?>>
            <span class="mtcam-stat-tile-icon">💰</span>
            <span class="mtcam-stat-tile-value"><?php echo esc_html( number_format( $total_value, 0, ',', ' ' ) ); ?> €</span>
            <span class="mtcam-stat-tile-label"><?php esc_html_e( 'Value', 'my-trail-camera' ); ?></span>
        </div>
        <?php endif; ?>

        <div class="mtcam-stat-tile mtcam-stat-tile-status">
            <span class="mtcam-stat-tile-label"><?php esc_html_e( 'Status', 'my-trail-camera' ); ?></span>
            <div class="mtcam-stat-tile-status-badge"><?php mtcam_render_status_badge( $mtc_cam ); ?></div>
        </div>
    </div>
</div>
<?php
} );
