<?php
require_once __DIR__ . '/includes/functions.php';
ini_set('display_errors', 1);
error_reporting(E_ALL);

$results = [];

// 1. Create admin password
$adminPath = DATA_PATH . '/admin.json';
$adminExists = file_exists($adminPath);
if (!$adminExists || empty(json_decode(file_get_contents($adminPath), true)['password'] ?? '')) {
    $hash = password_hash('Inexpo2026Admin', PASSWORD_BCRYPT);
    saveData('admin', ['email' => 'marketing@infairsafrica.com', 'password' => $hash]);
    $results[] = ['item' => 'Admin account', 'status' => 'ok', 'message' => 'Created with default password'];
} else {
    $results[] = ['item' => 'Admin account', 'status' => 'ok', 'message' => 'Already exists'];
}

// 2. Check/create JSON data files
$defaultFiles = ['settings', 'leads', 'exhibitors', 'sectors', 'faq', 'testimonials', 'gallery', 'reset-token'];
foreach ($defaultFiles as $file) {
    $path = DATA_PATH . '/' . $file . '.json';
    if (!file_exists($path)) {
        $default = match($file) {
            'leads', 'exhibitors', 'testimonials', 'reset-token' => [],
            'gallery' => (object)[],
            default => null,
        };
        if ($default !== null) {
            saveData($file, $default);
        }
        $results[] = ['item' => 'data/' . $file . '.json', 'status' => 'ok', 'message' => 'Created'];
    }
}

// 3. Create upload directories
$dirs = [
    UPLOADS_PATH . '/gallery/2025',
    UPLOADS_PATH . '/gallery/2024',
    UPLOADS_PATH . '/logos',
    UPLOADS_PATH . '/covers',
];
foreach ($dirs as $dir) {
    if (!is_dir($dir)) {
        mkdir($dir, 0755, true);
        $results[] = ['item' => str_replace(__DIR__, '', $dir), 'status' => 'ok', 'message' => 'Created'];
    }
}

// 4. Check writable
$dataWritable = is_writable(DATA_PATH);
$uploadsWritable = is_writable(UPLOADS_PATH);
$results[] = ['item' => 'data/ writable', 'status' => $dataWritable ? 'ok' : 'error', 'message' => $dataWritable ? 'Yes' : 'No — set to 755'];
$results[] = ['item' => 'uploads/ writable', 'status' => $uploadsWritable ? 'ok' : 'error', 'message' => $uploadsWritable ? 'Yes' : 'No — set to 755'];

// 5. Check PHPMailer
$phpmailerFiles = [
    VENDOR_PATH . '/PHPMailer.php',
    VENDOR_PATH . '/SMTP.php',
    VENDOR_PATH . '/Exception.php',
];
foreach ($phpmailerFiles as $f) {
    $exists = file_exists($f);
    $results[] = ['item' => 'vendor/' . basename($f), 'status' => $exists ? 'ok' : 'warning', 'message' => $exists ? 'Found' : 'Missing — upload manually'];
}

// Delete self
unlink(__FILE__);

// Output results
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Setup — Ethiopia Food Expo</title>
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600&family=Roboto+Condensed:wght@400;700&display=swap" rel="stylesheet">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body { font-family: 'Open Sans', sans-serif; background: #f5f7f5; padding: 2rem; }
        .setup-card { max-width: 700px; margin: 0 auto; background: #fff; border-radius: 8px; padding: 2rem; box-shadow: 0 2px 8px rgba(0,0,0,0.06); }
        h1 { font-family: 'Roboto Condensed', sans-serif; color: #1e5c2e; text-transform: uppercase; }
        .ok { color: #2e7d32; font-weight: 600; }
        .error { color: #c62828; font-weight: 600; }
        .warning { color: #f59e0b; font-weight: 600; }
    </style>
</head>
<body>
    <div class="setup-card">
        <h1 style="margin-bottom:0.5rem;">Setup Complete</h1>
        <p style="color:#5f6368;margin-bottom:1.5rem;">The Ethiopia Food Expo website has been configured. <strong>setup.php has been deleted for security.</strong></p>
        <table class="table table-sm">
            <thead><tr><th>Item</th><th>Status</th></tr></thead>
            <tbody>
                <?php foreach ($results as $r): ?>
                <tr>
                    <td><?php echo htmlspecialchars($r['item']); ?></td>
                    <td class="<?php echo $r['status']; ?>"><?php echo htmlspecialchars($r['message']); ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <div style="margin-top:1.5rem;">
            <a href="index.php" class="btn btn-success">View Website</a>
            <a href="admin/index.php" class="btn btn-outline-success">Go to Admin Panel</a>
        </div>
        <div style="margin-top:1rem;padding:1rem;background:#f5f7f5;border-radius:4px;font-size:0.85rem;color:#5f6368;">
            <strong>Login:</strong> marketing@infairsafrica.com / Inexpo2026Admin
        </div>
    </div>
</body>
</html>
