<?php
require_once '../config/db.php';
require_once '../config/cors.php';
header('Content-Type: application/json');

$userId = isset($_GET['user_id']) ? (int) $_GET['user_id'] : 0;
if ($userId <= 0) {
    echo json_encode(['status' => 'error', 'message' => 'Invalid user ID']);
    exit;
}

$sigDir = __DIR__ . '/../storage/officers_signature/';
if (!is_dir($sigDir)) {
    echo json_encode(['status' => 'success', 'signature_path' => null]);
    exit;
}

$files = glob($sigDir . 'officer_' . $userId . '_*');
if (!empty($files)) {
    usort($files, fn($a, $b) => filemtime($b) - filemtime($a));
    $sigPath = 'storage/officers_signature/' . basename($files[0]);
    echo json_encode(['status' => 'success', 'signature_path' => $sigPath]);
} else {
    echo json_encode(['status' => 'success', 'signature_path' => null]);
}
?>