<?php
/**
 * Temporary script to download face-api.js model files
 * This helps users set up the required models for face detection
 */

// Define the required model files
$models = [
    'tiny_face_detector_model-weights_manifest.json' => 'https://raw.githubusercontent.com/justadudewhohacks/face-api.js/master/weights/tiny_face_detector_model-weights_manifest.json',
    'tiny_face_detector_model-shard1' => 'https://raw.githubusercontent.com/justadudewhohacks/face-api.js/master/weights/tiny_face_detector_model-shard1',
    'tiny_face_detector_model-shard2' => 'https://raw.githubusercontent.com/justadudewhohacks/face-api.js/master/weights/tiny_face_detector_model-shard2',
    'tiny_face_detector_model-shard3' => 'https://raw.githubusercontent.com/justadudewhohacks/face-api.js/master/weights/tiny_face_detector_model-shard3'
];

$modelDir = __DIR__ . '/assets/models';

// Create directory if it doesn't exist
if (!is_dir($modelDir)) {
    mkdir($modelDir, 0755, true);
    echo "<p>Created models directory at: {$modelDir}</p>\n";
}

echo "<h1>Downloading Face Detection Models</h1>\n";
echo "<p>This script will download the required face detection models.</p>\n";

$successCount = 0;
$totalCount = count($models);

foreach ($models as $filename => $url) {
    $filepath = $modelDir . '/' . $filename;
    
    echo "<p>Downloading <strong>{$filename}</strong>...</p>\n";
    
    // Attempt to download the file
    $content = @file_get_contents($url);
    
    if ($content !== false) {
        $bytes = file_put_contents($filepath, $content);
        if ($bytes !== false) {
            echo "<p style='color: green;'>✓ Successfully downloaded {$filename} ({$bytes} bytes)</p>\n";
            $successCount++;
        } else {
            echo "<p style='color: red;'>✗ Failed to save {$filename}</p>\n";
        }
    } else {
        echo "<p style='color: red;'>✗ Failed to download {$filename} from {$url}</p>\n";
        
        // Provide manual download link
        echo "<p>Manual download: <a href='{$url}' target='_blank'>{$filename}</a></p>\n";
    }
    
    flush(); // Flush output buffer
    ob_flush();
    usleep(500000); // Half second delay to be respectful to the server
}

echo "<hr>";
echo "<h2>Download Summary</h2>\n";
echo "<p>Successfully downloaded {$successCount} of {$totalCount} files.</p>\n";

if ($successCount == $totalCount) {
    echo "<p style='color: green; font-weight: bold;'>✓ All models downloaded successfully!</p>\n";
    echo "<p>Face detection should now work properly on the registration pages.</p>\n";
} else {
    echo "<p style='color: orange;'>⚠ Some models failed to download. Face detection may not work optimally.</p>\n";
    echo "<p>Please try refreshing this page or download the missing files manually.</p>\n";
}

echo "<p><a href='setup_face_models.php'>← Back to Setup Guide</a> | ";
echo "<a href='attendance_dashboard/register_biometric.php'>Biometric Registration</a> | ";
echo "<a href='attendance_dashboard/photo_registration.php'>Photo Registration</a></p>\n";

// Clean up this temporary file after execution
// unlink(__FILE__); // Uncomment this line after testing to automatically delete the file
?>