AFG CARS Enterprise Setup"; try { global $pdo; // Assumes db/config.php creates $pdo echo "

1. Initializing Database Schema...

"; // Read SQL file $sql_file = __DIR__ . '/db/database.sql'; if (!file_exists($sql_file)) { throw new Exception("Database SQL file not found at: $sql_file"); } $sql_content = file_get_contents($sql_file); // Split into individual queries (basic splitting by semicolon) // Note: This is a simple splitter and might break on complex stored procedures, but sufficient for this schema. $queries = explode(';', $sql_content); foreach ($queries as $query) { $query = trim($query); if (!empty($query)) { $pdo->exec($query); } } echo "

Schema imported successfully.

"; echo "

2. Seeding Data...

"; // Seed Users (Admin & Customer) // Check if admin exists to avoid duplicates if re-run $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE email = ?"); $stmt->execute(['admin@afgcars.com']); if ($stmt->fetchColumn() == 0) { $password = password_hash('admin123', PASSWORD_DEFAULT); $stmt = $pdo->prepare("INSERT INTO users (name, email, password, role) VALUES (?, ?, ?, ?)"); $stmt->execute(['Administrator', 'admin@afgcars.com', $password, 'admin']); echo "

Admin user created (admin@afgcars.com / admin123)

"; } $stmt->execute(['John Doe', 'user@example.com', password_hash('user123', PASSWORD_DEFAULT), 'user']); echo "

Demo user created (user@example.com / user123)

"; // Seed Branches // Tables are fresh from schema import, so no need to truncate $branches = [ ['Kabul Main', 'Kabul', 'Shar-e-Naw, Kabul', '+93 700 111 222'], ['Herat Branch', 'Herat', 'Main Road, Herat', '+93 700 333 444'], ['Mazar Center', 'Mazar-i-Sharif', 'Balkh Street, Mazar', '+93 700 555 666'], ['Kandahar Hub', 'Kandahar', 'Airport Road, Kandahar', '+93 700 777 888'] ]; $stmt = $pdo->prepare("INSERT INTO branches (name, city, address, phone) VALUES (?, ?, ?, ?)"); foreach ($branches as $branch) { $stmt->execute($branch); } echo "

Branches seeded.

"; // Seed Cars // $pdo->exec("SET FOREIGN_KEY_CHECKS=0"); // $pdo->exec("TRUNCATE TABLE cars"); // $pdo->exec("SET FOREIGN_KEY_CHECKS=1"); $brands = ['Toyota', 'Lexus', 'Mercedes-Benz', 'BMW', 'Audi', 'Land Rover', 'Porsche', 'Tesla']; $models = [ 'Toyota' => ['Camry', 'Land Cruiser', 'Corolla', 'RAV4'], 'Lexus' => ['LX 600', 'RX 350', 'ES 350'], 'Mercedes-Benz' => ['S-Class', 'G-Wagon', 'E-Class'], 'BMW' => ['X7', 'X5', '7 Series'], 'Audi' => ['Q8', 'A8', 'RS7'], 'Land Rover' => ['Defender', 'Range Rover'], 'Porsche' => ['911 Carrera', 'Cayenne'], 'Tesla' => ['Model S', 'Model X'] ]; $stmt = $pdo->prepare("INSERT INTO cars (brand, model, year, price, mileage, fuel_type, transmission, description, image_path, branch_id, is_featured, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); for ($i = 1; $i <= 20; $i++) { $brand = $brands[array_rand($brands)]; $model = $models[$brand][array_rand($models[$brand])]; $year = rand(2020, 2024); $price = rand(25000, 150000); $mileage = rand(0, 50000); $fuel = rand(0, 1) ? 'Petrol' : 'Hybrid'; $desc = "Premium condition $brand $model. Full options, well maintained."; $image = "assets/images/cars/car{$i}.jpg"; $branch_id = rand(1, 4); $is_featured = ($i <= 6) ? 1 : 0; // First 6 are featured $stmt->execute([ $brand, $model, $year, $price, $mileage, $fuel, 'Automatic', $desc, $image, $branch_id, $is_featured, 'available' ]); } echo "

20 Demo cars seeded.

"; echo "

Setup Complete!

"; echo "

Go to Homepage

"; } catch (Exception $e) { die("

Setup Failed: " . $e->getMessage() . "

"); }