getMessage() . "\n"); } // Note: We cannot create a new database 'car_dealership' because of restricted privileges. // We are using the existing database provided by the environment. try { // 2. Drop existing tables to ensure a clean state (Idempotency) // We drop bookings and reviews first if they exist to avoid foreign key constraints issues (if any) // although the prompt didn't ask for bookings/reviews, existing app has them. // I should probably drop them to strictly follow "create necessary tables". // But if I drop them, the admin dashboard might break if it queries them. // The prompt says "Create all necessary tables...". It lists cars and users. // I will drop cars and users. If foreign keys exist on bookings/reviews pointing to cars/users, DROP will fail or cascade depending on setup. // I'll use SET FOREIGN_KEY_CHECKS=0 to be safe. $pdo->exec("SET FOREIGN_KEY_CHECKS=0"); $pdo->exec("DROP TABLE IF EXISTS cars"); $pdo->exec("DROP TABLE IF EXISTS users"); $pdo->exec("SET FOREIGN_KEY_CHECKS=1"); echo "Existing tables dropped (if any).\n"; // 3. Create tables with correct columns // Users Table $createUsersTable = " CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, role VARCHAR(20) DEFAULT 'user', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; $pdo->exec($createUsersTable); echo "Users table ready.\n"; // Cars Table // Added image_url to schema to maintain application compatibility $createCarsTable = " CREATE TABLE cars ( id INT AUTO_INCREMENT PRIMARY KEY, title VARCHAR(255) NOT NULL, make VARCHAR(100), model VARCHAR(100), year INT, mileage INT, price DECIMAL(10,2), description TEXT, status VARCHAR(50) NOT NULL DEFAULT 'pending', color VARCHAR(50), province VARCHAR(100), city VARCHAR(100), image_url VARCHAR(255) DEFAULT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; $pdo->exec($createCarsTable); echo "Cars table ready.\n"; // 4. Add default admin user $adminUsername = 'admin'; $adminPassword = '123'; $adminHash = password_hash($adminPassword, PASSWORD_DEFAULT); $adminRole = 'admin'; // Idempotent check handled by DROP TABLE above, but logic included for completeness if table wasn't dropped $stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE username = ?"); $stmt->execute([$adminUsername]); if ($stmt->fetchColumn() == 0) { $insertAdmin = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)"); $insertAdmin->execute([$adminUsername, $adminHash, $adminRole]); echo "Admin user created (User: $adminUsername, Pass: $adminPassword).\n"; } else { echo "Admin user already exists.\n"; } // 5. Insert sample data into cars table $carsData = [ [ 'title' => 'Toyota Corolla 2020 Clean', 'make' => 'Toyota', 'model' => 'Corolla', 'year' => 2020, 'mileage' => 15000, 'price' => 18500.00, 'description' => 'Very clean car, no accidents.', 'status' => 'approved', 'color' => 'White', 'province' => 'Kabul', 'city' => 'Kabul', 'image_url' => 'assets/images/placeholder_car1.jpg' ], [ 'title' => 'Honda Civic 2018 Sport', 'make' => 'Honda', 'model' => 'Civic', 'year' => 2018, 'mileage' => 45000, 'price' => 16200.00, 'description' => 'Sport mode, leather seats.', 'status' => 'approved', 'color' => 'Black', 'province' => 'Herat', 'city' => 'Herat', 'image_url' => 'assets/images/placeholder_car2.jpg' ], [ 'title' => 'Ford Ranger 2019', 'make' => 'Ford', 'model' => 'Ranger', 'year' => 2019, 'mileage' => 30000, 'price' => 25000.00, 'description' => 'Strong pickup for tough roads.', 'status' => 'pending', 'color' => 'Blue', 'province' => 'Kandahar', 'city' => 'Kandahar', 'image_url' => 'assets/images/placeholder_car3.jpg' ] ]; $insertCar = $pdo->prepare("INSERT INTO cars (title, make, model, year, mileage, price, description, status, color, province, city, image_url) VALUES (:title, :make, :model, :year, :mileage, :price, :description, :status, :color, :province, :city, :image_url)"); foreach ($carsData as $car) { $insertCar->execute($car); } echo "Seed data inserted (" . count($carsData) . " cars).\n"; echo "Setup complete. The application is ready to use.\n"; } catch (PDOException $e) { die("Setup failed: " . $e->getMessage() . "\n"); }