prepare("SELECT id FROM users WHERE email = ?"); $stmt->execute([$email]); $adminUserId = $stmt->fetchColumn(); if ($adminUserId) { echo "Admin user already exists with ID: $adminUserId.\n"; } else { $stmt = $pdo->prepare("INSERT INTO users (email, password_enc, role, display_name) VALUES (?, ?, 'Admin', 'Admin User')"); $stmt->execute([$email, $hashedPassword]); $adminUserId = $pdo->lastInsertId(); echo "Admin user created successfully with ID: $adminUserId.\n"; echo "Email: " . $email . "\n"; echo "Password: " . $password . "\n"; } } catch (PDOException $e) { echo "Error seeding database: " . $e->getMessage() . "\n"; return false; } // Seed clients try { $stmt = $pdo->query("SELECT count(*) FROM clients"); if ($stmt->fetchColumn() > 0) { echo "Clients table already has data. Checking for user_id assignment.\n"; // Check if clients are unassigned $stmt = $pdo->prepare("SELECT client_id FROM clients WHERE user_id IS NULL OR user_id = 0"); $stmt->execute(); $unassigned_clients = $stmt->fetchAll(PDO::FETCH_COLUMN); if (count($unassigned_clients) > 0) { echo "Found " . count($unassigned_clients) . " unassigned clients. Assigning to admin user.\n"; $updateStmt = $pdo->prepare("UPDATE clients SET user_id = ? WHERE client_id = ?"); foreach ($unassigned_clients as $client_id) { $updateStmt->execute([$adminUserId, $client_id]); } echo "Assigned " . count($unassigned_clients) . " clients to admin user.\n"; } else { echo "All clients are already assigned to a user.\n"; } } else { echo "Clients table is empty. Seeding new clients.\n"; $clients = [ ['1001', 'Stark Industries', 'active', $adminUserId], ['1002', 'Wayne Enterprises', 'active', $adminUserId], ['1003', 'Cyberdyne Systems', 'inactive', $adminUserId], ]; $stmt = $pdo->prepare("INSERT INTO clients (client_id, name, status, user_id) VALUES (?, ?, ?, ?)"); foreach ($clients as $client) { $stmt->execute($client); } echo "Seeded " . count($clients) . " clients for admin user.\n"; } } catch (PDOException $e) { echo "Error seeding clients: " . $e->getMessage() . "\n"; return false; } return true; } if (php_sapi_name() === 'cli') { seed_database(); } ?>