<?php
/**
 * Camera detail partial — Identity & Dates (left column).
 *
 * Compact definition list of non-hero identity fields (S/N, IMEI, Owner)
 * and the lifecycle dates (purchase, deploy, added, last check, recovered).
 * Merges the old basic-info + dates-stats partials.
 *
 * Expected variables in scope (via include):
 *   $mtc_cam_id, $mtc_cam, $mtc_zones (PRO), $mtc_all_cameras
 *
 * @package My Trail Camera
 * @since   8.2.4
 */

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

call_user_func( function () use ( $mtc_cam_id, $mtc_cam, $mtc_extras, $mtc_all_cameras ) {

$profile_id      = $mtc_cam['profile'] ?? '';
$mtcam_show_imei = ! empty( $mtc_cam['imei'] );
if ( ! $mtcam_show_imei && ! empty( $profile_id ) && $profile_id !== 'custom' ) {
	$imei_profile    = function_exists( 'mtcam_get_camera_profile' ) ? mtcam_get_camera_profile( $profile_id ) : array();
	$mtcam_show_imei = ! empty( $imei_profile['has_sim'] );
}

?>
<div class="mtcam-detail-card-inner">
    <h4>📋 <?php esc_html_e( 'Identity & Dates', 'my-trail-camera' ); ?></h4>
    <dl class="mtcam-dl">
        <?php if ( ! empty( $mtc_cam['serial_number'] ) ) : ?>
        <div class="mtcam-dl-row">
            <dt><?php esc_html_e( 'S/N:', 'my-trail-camera' ); ?></dt>
            <dd><?php echo esc_html( $mtc_cam['serial_number'] ); ?></dd>
        </div>
        <?php endif; ?>

        <?php if ( $mtcam_show_imei ) : ?>
        <div class="mtcam-dl-row">
            <dt><?php esc_html_e( 'IMEI:', 'my-trail-camera' ); ?></dt>
            <dd><?php echo ! empty( $mtc_cam['imei'] ) ? esc_html( $mtc_cam['imei'] ) : '—'; ?></dd>
        </div>
        <?php endif; ?>

        

        <?php if ( ! mtcam_is_my_camera( $mtc_cam ) ) : ?>
        <div class="mtcam-dl-row">
            <dt><?php esc_html_e( 'Owner:', 'my-trail-camera' ); ?></dt>
            <dd>
                <strong><?php echo esc_html( $mtc_cam['owner_name'] ?? __( 'Unknown', 'my-trail-camera' ) ); ?></strong>
                · <small>📧 <?php echo esc_html( $mtc_cam['owner_email'] ?? '—' ); ?></small>
            </dd>
        </div>
        <?php endif; ?>

        <div class="mtcam-dl-sep" aria-hidden="true"></div>

        <div class="mtcam-dl-row">
            <dt><?php esc_html_e( 'Purchased:', 'my-trail-camera' ); ?></dt>
            <dd><?php mtcam_echo_date_html( $mtc_cam['purchase_date'] ?? '' ); ?></dd>
        </div>
        <div class="mtcam-dl-row">
            <dt><?php esc_html_e( 'Deployed:', 'my-trail-camera' ); ?></dt>
            <dd><?php
                mtcam_echo_date_html( $mtc_cam['deploy_date'] ?? '' );
                if ( ! empty( $mtc_cam['deploy_time'] ) ) {
                    echo ' ' . esc_html( $mtc_cam['deploy_time'] );
                }
            ?></dd>
        </div>
        <div class="mtcam-dl-row">
            <dt><?php esc_html_e( 'Added:', 'my-trail-camera' ); ?></dt>
            <dd><?php
                if ( ! empty( $mtc_cam['created_date'] ) ) {
                    mtcam_echo_date_html( $mtc_cam['created_date'], 'j.m.Y' );
                    echo ' <small class="mtcam-text-muted">' . esc_html( sprintf(
                        /* translators: %s: time in HH:MM */
                        __( 'at %s', 'my-trail-camera' ),
                        wp_date( 'H:i', strtotime( $mtc_cam['created_date'] ) )
                    ) ) . '</small>';
                } else {
                    echo '—';
                }
            ?></dd>
        </div>

        <?php if ( ! empty( $mtc_cam['last_checked'] ) ) :
            $last_days = max( 0, intval( ( time() - strtotime( $mtc_cam['last_checked'] ) ) / 86400 ) );
        ?>
        <div class="mtcam-dl-row">
            <dt>🔍 <?php esc_html_e( 'Last check:', 'my-trail-camera' ); ?></dt>
            <dd><?php mtcam_echo_date_html( $mtc_cam['last_checked'] ); ?><?php if ( $last_days > 0 ) : ?>
                <small class="mtcam-text-muted">(<?php
                    /* translators: %d: number of days since last check */
                    echo esc_html( sprintf( __( '%d days ago', 'my-trail-camera' ), $last_days ) );
                ?>)</small>
            <?php endif; ?></dd>
        </div>
        <?php endif; ?>

        <?php if ( ! empty( $mtc_cam['recovered_date'] ) ) : ?>
        <div class="mtcam-dl-row">
            <dt>✅ <?php esc_html_e( 'Recovered:', 'my-trail-camera' ); ?></dt>
            <dd><?php mtcam_echo_date_html( $mtc_cam['recovered_date'] ); ?></dd>
        </div>
        <?php endif; ?>
    </dl>
</div>
<?php
} );
