<?php
/**
 * Seeds 126 demo users: 1 admin, 5 managers, 120 customers.
 * Run: php database/seed_demo_users.php
 */
declare(strict_types=1);

require_once __DIR__ . '/../app/config/database.php';

$password = 'Demo@2026';
$hash = password_hash($password, PASSWORD_DEFAULT);

$rows = [];

$rows[] = [
    'full_name' => 'Super Admin',
    'username' => 'superadmin',
    'email' => 'superadmin@zylon.local',
    'phone' => '09120000001',
    'role' => 'admin',
];

for ($i = 1; $i <= 5; $i++) {
    $n = str_pad((string)$i, 2, '0', STR_PAD_LEFT);
    $rows[] = [
        'full_name' => 'Manager ' . $n,
        'username' => 'manager' . $n,
        'email' => 'manager' . $n . '@zylon.local',
        'phone' => '09120001' . str_pad((string)(100 + $i), 3, '0', STR_PAD_LEFT),
        'role' => 'manager',
    ];
}

for ($i = 1; $i <= 120; $i++) {
    $n = str_pad((string)$i, 3, '0', STR_PAD_LEFT);
    $rows[] = [
        'full_name' => 'Customer ' . $n,
        'username' => 'customer' . $n,
        'email' => 'customer' . $n . '@zylon.local',
        'phone' => '0913' . str_pad((string)(100000 + $i), 6, '0', STR_PAD_LEFT),
        'role' => 'customer',
    ];
}

$pdo = db();
$stmt = $pdo->prepare(
    'INSERT INTO users (full_name, username, email, phone, password_hash, role, wallet_balance, suspended)
     VALUES (:full_name, :username, :email, :phone, :password_hash, :role, 0, 0)'
);

$inserted = 0;
$skipped = 0;
foreach ($rows as $r) {
    $check = $pdo->prepare('SELECT id FROM users WHERE username = :u LIMIT 1');
    $check->execute(['u' => $r['username']]);
    if ($check->fetch()) {
        $skipped++;
        continue;
    }
    $stmt->execute([
        'full_name' => $r['full_name'],
        'username' => $r['username'],
        'email' => $r['email'],
        'phone' => $r['phone'],
        'password_hash' => $hash,
        'role' => $r['role'],
    ]);
    $inserted++;
}

fwrite(STDOUT, "Done. Inserted: {$inserted}, skipped (existing username): {$skipped}\n");
fwrite(STDOUT, "Default password for all seeded accounts: {$password}\n");
