<?php
/**
 * Camera detail partial — Basic Info section.
 *
 * Expected variables in scope (via include):
 *   $mtc_cam_id, $mtc_cam, $mtc_zones (PRO), $mtc_all_cameras
 *
 * @package My Trail Camera
 * @since   8.2.0
 */

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

call_user_func( function () use ( $mtc_cam_id, $mtc_cam, $mtc_extras, $mtc_all_cameras ) {
?>
<!-- Basic Info -->
<div>
    <h4>📋 <?php esc_html_e('Basic Info', 'my-trail-camera'); ?></h4>
    <table class="mtcam-info-table">
        <tr>
            <td><?php esc_html_e('ID:', 'my-trail-camera'); ?></td>
            <td>
                <?php if (mtcam_is_my_camera($mtc_cam)): ?>
                    <strong><?php echo esc_html($mtc_cam_id); ?></strong>
                <?php else: ?>
                    <span class="mtcam-text-muted">●●●●●● (<?php esc_html_e('hidden', 'my-trail-camera'); ?>)</span>
                <?php endif; ?>
            </td>
        </tr>
        <tr>
            <td><?php esc_html_e('Model:', 'my-trail-camera'); ?></td>
            <td><?php
                $profile_id   = $mtc_cam['profile'] ?? '';
                $manufacturer = $mtc_cam['manufacturer'] ?? '';
                $profile_label = '';

                if (!empty($profile_id) && $profile_id !== 'custom') {
                    $custom_profiles = get_option('mtcam_custom_profiles', array());
                    if (isset($custom_profiles[$profile_id])) {
                        $profile_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')) {
                            $profile_label = $profile['label'] ?? '';
                        }
                    }
                }

                if (!empty($profile_label)) {
                    echo esc_html($profile_label);
                } elseif (!empty($manufacturer)) {
                    echo esc_html($manufacturer);
                } else {
                    echo '<span class="mtcam-text-muted">—</span>';
                }
            ?></td>
        </tr>
        <?php if (!empty($mtc_cam['serial_number'])): ?>
        <tr>
            <td><?php esc_html_e('S/N:', 'my-trail-camera'); ?></td>
            <td><?php echo esc_html($mtc_cam['serial_number']); ?></td>
        </tr>
        <?php endif; ?>
        <?php
        $mtcam_show_imei = ! empty( $mtc_cam['imei'] );
        if ( ! $mtcam_show_imei && ! empty( $profile_id ) && $profile_id !== 'custom' ) {
            $mtcam_imei_profile = function_exists( 'mtcam_get_camera_profile' ) ? mtcam_get_camera_profile( $profile_id ) : array();
            $mtcam_show_imei    = ! empty( $mtcam_imei_profile['has_sim'] );
        }
        if ( $mtcam_show_imei ) :
        ?>
        <tr>
            <td><?php esc_html_e('IMEI:', 'my-trail-camera'); ?></td>
            <td><?php echo !empty($mtc_cam['imei']) ? esc_html($mtc_cam['imei']) : '—'; ?></td>
        </tr>
        <?php endif; ?>
        <tr>
            <td><?php esc_html_e('Power:', 'my-trail-camera'); ?></td>
            <td><?php echo esc_html(mtcam_get_power_label($mtc_cam['power_type'] ?? 'lithium')); ?></td>
        </tr>
        <tr>
            <td><?php esc_html_e('Location:', 'my-trail-camera'); ?></td>
            <td><?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">';
                    echo esc_html($mtc_cam['location']);
                    echo ' 🗺️</a>';
                } else {
                    echo esc_html($mtc_cam['location']);
                }
            ?></td>
        </tr>
        <?php
        ?>
        <?php
        $total_value = mtcam_get_camera_total_value($mtc_cam);
        if ($total_value > 0): ?>
        <tr>
            <td>💰 <?php esc_html_e('Value:', 'my-trail-camera'); ?></td>
            <td><strong class="mtcam-text-danger"><?php echo esc_html(number_format($total_value, 2, ',', ' ')); ?> €</strong>
            <?php if ($total_value != floatval($mtc_cam['price'] ?? 0)): ?>
                <br><small class="mtcam-text-muted">📷 <?php echo esc_html(number_format(floatval($mtc_cam['price'] ?? 0), 2, ',', ' ')); ?> € + <?php esc_html_e('accessories', 'my-trail-camera'); ?></small>
            <?php endif; ?>
            </td>
        </tr>
        <?php endif; ?>
        <?php if (!mtcam_is_my_camera($mtc_cam)): ?>
        <tr>
            <td><?php esc_html_e('Owner:', 'my-trail-camera'); ?></td>
            <td>
                <strong><?php echo esc_html($mtc_cam['owner_name'] ?? __('Unknown', 'my-trail-camera')); ?></strong><br>
                <small>📧 <?php echo esc_html($mtc_cam['owner_email'] ?? '—'); ?></small>
            </td>
        </tr>
        <?php endif; ?>
    </table>
</div>
<?php
} );
