<?php

declare(strict_types=1);

if (php_sapi_name() !== 'cli') {
    http_response_code(403);
    exit('CLI only');
}

require_once __DIR__ . '/db.php';

$username = 'testuser';
$email = 'test@mocker.local';
$password = 'Test1234!';
$coins = 150;

$pdo = mocker_db();
$hash = password_hash($password, PASSWORD_DEFAULT);

$st = $pdo->prepare('SELECT id FROM users WHERE username = :u OR email = :e LIMIT 1');
$st->execute([':u' => $username, ':e' => $email]);
$row = $st->fetch();

if ($row) {
    $id = (int) $row['id'];
    $up = $pdo->prepare('UPDATE users SET password_hash = :p, coins = :c WHERE id = :id');
    $up->execute([':p' => $hash, ':c' => $coins, ':id' => $id]);
    echo "Test kullanıcısı güncellendi (şifre sıfırlandı).\n\n";
} else {
    $now = time();
    $ins = $pdo->prepare(
        'INSERT INTO users (username, email, password_hash, coins, created_at, last_login_at, last_seen_at) VALUES (:u, :e, :p, :c, :t, :t, :t2)'
    );
    $ins->execute([
        ':u' => $username,
        ':e' => $email,
        ':p' => $hash,
        ':c' => $coins,
        ':t' => $now,
        ':t2' => $now,
    ]);
    echo "Test kullanıcısı oluşturuldu.\n\n";
}

echo "Kullanıcı adı: {$username}\n";
echo "E-posta:       {$email}\n";
echo "Şifre:         {$password}\n";
