<?php
/**
 * Download Assets Script
 * This script downloads all required CDN assets locally
 */

echo "Starting asset download...\n";

$assets = [
    // JavaScript Files
    'js/jquery.min.js' => 'https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js',
    'js/sweetalert2.min.js' => 'https://cdn.jsdelivr.net/npm/sweetalert2@11/dist/sweetalert2.all.min.js',
    
    // CSS Files
    'css/tailwind.min.css' => 'https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css',
    'css/fontawesome.min.css' => 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css',
    
    // Font Awesome Webfonts (required for icons to display)
    'webfonts/fa-solid-900.woff2' => 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/webfonts/fa-solid-900.woff2',
    'webfonts/fa-solid-900.ttf' => 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/webfonts/fa-solid-900.ttf',
    'webfonts/fa-regular-400.woff2' => 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/webfonts/fa-regular-400.woff2',
    'webfonts/fa-regular-400.ttf' => 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/webfonts/fa-regular-400.ttf',
    'webfonts/fa-brands-400.woff2' => 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/webfonts/fa-brands-400.woff2',
    'webfonts/fa-brands-400.ttf' => 'https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/webfonts/fa-brands-400.ttf',
    
    // Inter Font Files
    'fonts/Inter-Regular.woff2' => 'https://fonts.gstatic.com/s/inter/v13/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuLyfAZ9hiA.woff2',
    'fonts/Inter-Medium.woff2' => 'https://fonts.gstatic.com/s/inter/v13/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuI6fAZ9hiA.woff2',
    'fonts/Inter-SemiBold.woff2' => 'https://fonts.gstatic.com/s/inter/v13/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuGKYAZ9hiA.woff2',
    'fonts/Inter-Bold.woff2' => 'https://fonts.gstatic.com/s/inter/v13/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuFuYAZ9hiA.woff2',
    'fonts/Inter-ExtraBold.woff2' => 'https://fonts.gstatic.com/s/inter/v13/UcCO3FwrK3iLTeHuS_fvQtMwCp50KnMw2boKoduKmMEVuDyYAZ9hiA.woff2',
];

$errors = [];
$success = [];

foreach ($assets as $localPath => $url) {
    $fullPath = __DIR__ . '/' . $localPath;
    $dir = dirname($fullPath);
    
    // Create directory if it doesn't exist
    if (!file_exists($dir)) {
        mkdir($dir, 0755, true);
    }
    
    echo "Downloading: $url\n";
    echo "Saving to: $localPath\n";
    
    $content = @file_get_contents($url);
    
    if ($content === false) {
        $errors[] = "Failed to download: $url";
        echo "❌ FAILED\n\n";
    } else {
        file_put_contents($fullPath, $content);
        $success[] = $localPath;
        echo "✅ SUCCESS\n\n";
    }
}

echo "\n===========================================\n";
echo "Download Summary\n";
echo "===========================================\n";
echo "Successful: " . count($success) . "\n";
echo "Failed: " . count($errors) . "\n";

if (!empty($success)) {
    echo "\n✅ Successfully downloaded:\n";
    foreach ($success as $file) {
        echo "  - $file\n";
    }
}

if (!empty($errors)) {
    echo "\n❌ Errors:\n";
    foreach ($errors as $error) {
        echo "  - $error\n";
    }
}

echo "\n===========================================\n";
echo "Next Steps:\n";
echo "===========================================\n";
echo "1. Visit check_assets.php to verify all files\n";
echo "2. Run ../download.php to generate login.html\n";
echo "3. Access your hotspot at {domain}/hotspot/\n";
echo "\n✅ All assets downloaded including:\n";
echo "  - JavaScript libraries (jQuery, SweetAlert2, Tailwind)\n";
echo "  - Font Awesome CSS & webfonts\n";
echo "  - Inter font family (5 weights)\n";
echo "\n⚠️  IMPORTANT: Access via web server (http://localhost)\n";
echo "   Do NOT open HTML file directly from disk to avoid CORS errors.\n";
echo "===========================================\n";
