<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/../src/Utils/Env.php';
require_once __DIR__ . '/../src/Config/Database.php';

use App\Config\Database;
use App\Utils\Env;

// Load Env
Env::load(__DIR__ . '/../.env');

// Explicit credentials provided by user
$host = 'localhost';
$dbName = 'messrypt_nons';
$user = 'messrypt_nons';
$pass = 'messrypt_nons';

try {
    $dsn = "mysql:host=$host;dbname=$dbName;charset=utf8mb4";
    $options = [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
        PDO::ATTR_EMULATE_PREPARES   => false,
    ];
    $db = new PDO($dsn, $user, $pass, $options);
} catch (Exception $e) {
    die("Database Connection Error: " . $e->getMessage());
}

echo "<pre>";
echo "Seeding realistic bot users...\n";

$maleNames = ['Ahmet', 'Mehmet', 'Mustafa', 'Can', 'Burak', 'Emre', 'Tolga', 'Kerem', 'Mert', 'Ozan', 'Volkan', 'Sinan', 'Arda', 'Kaan', 'Yigit', 'Ali', 'Veli', 'Murat', 'Serkan', 'Hakan', 'Onur', 'Cem', 'Deniz', 'Umut', 'Baris'];
$femaleNames = ['Ayse', 'Fatma', 'Zeynep', 'Elif', 'Merve', 'Gamze', 'Gizem', 'Selin', 'Pinar', 'Hande', 'Irem', 'Leyla', 'Asli', 'Buse', 'Cansu', 'Esra', 'Derya', 'Ece', 'Seda', 'Yagmur', 'Nazli', 'Melis', 'Simge', 'Busra', 'Ceren'];

$lastNames = ['Yilmaz', 'Kaya', 'Demir', 'Sahin', 'Celik', 'Yildiz', 'Yildirim', 'Ozturk', 'Aydin', 'Ozdemir', 'Arslan', 'Dogan', 'Kilic', 'Aslan', 'Cetin', 'Kara', 'Koc', 'Kurt', 'Ozkan', 'Simsek', 'Polat', 'Korkmaz', 'Ozcan', 'Cakir', 'Erdogan'];

$bios = [
    "Selam, sohbet etmeyi severim.",
    "Istanbul'da yasiyorum, universite ogrencisiyim.",
    "Muzik, kahve ve kitaplar ☕📚",
    "Burada yeniyim, arkadas ariyorum.",
    "Sadece ciddi sohbetler lutfen.",
    "DM kutum acik 📩",
    "Anonim sohbet eglenceliymis.",
    "Yazilimci, gezgin, hayalperest.",
    "Spor yapmayi severim 🏋️‍♂️",
    "Fotograf cekmek hobim 📸",
    "Netflix ve chill 🍕",
    "Kedileri cok severim 🐱",
    "Gezmeyi ve yeni yerler kesfetmeyi severim.",
    "Hayat kisa, kuslar ucuyor.",
    "Pozitif vibes only ✨",
    "Kahve bagimlisi ☕",
    "Ogrenci evi maceralari...",
    "Rock muzik dinlerim 🎸",
    "Doğa yuruyusleri favorim 🌲",
    "Sanat ve tasarimla ilgileniyorum 🎨"
];

$passwordHash = password_hash('123123', PASSWORD_BCRYPT);

for ($i = 0; $i < 50; $i++) {
    $isFemale = rand(0, 1) == 1;
    $firstName = $isFemale ? $femaleNames[array_rand($femaleNames)] : $maleNames[array_rand($maleNames)];
    $lastName = $lastNames[array_rand($lastNames)];
    
    $displayName = "$firstName $lastName";
    
    // Generate username
    $suffix = rand(0, 1) == 1 ? rand(10, 99) : '';
    $separator = rand(0, 1) == 1 ? '_' : '';
    $username = strtolower($firstName) . $separator . strtolower($lastName) . $suffix;
    $username = str_replace(['ı', 'ğ', 'ü', 'ş', 'İ', 'ö', 'ç'], ['i', 'g', 'u', 's', 'i', 'o', 'c'], $username); // Basic cleaning
    
    $email = $username . '@example.com';
    $slug = $username . rand(100, 999);
    $bio = $bios[array_rand($bios)];
    
    // Random profile picture using pravatar.cc (Real faces)
    // We add a random parameter to ensure variety
    $avatarUrl = "https://i.pravatar.cc/300?u=" . md5($username . time());

    try {
        $stmt = $db->prepare("INSERT INTO users (email, password_hash, username, display_name, link_slug, bio, avatar_url, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, 'online', NOW())");
        $stmt->execute([$email, $passwordHash, $username, $displayName, $slug, $bio, $avatarUrl]);
        echo "Created: $displayName ($username)\n";
    } catch (Exception $e) {
        // Retry with a slightly different username if duplicate
        try {
            $username .= rand(1, 9);
            $slug = $username . rand(100, 999);
            $stmt = $db->prepare("INSERT INTO users (email, password_hash, username, display_name, link_slug, bio, avatar_url, status, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, 'online', NOW())");
            $stmt->execute([$email, $passwordHash, $username, $displayName, $slug, $bio, $avatarUrl]);
            echo "Created (retry): $displayName ($username)\n";
        } catch (Exception $e2) {
            echo "Skipped $username: " . $e2->getMessage() . "\n";
        }
    }
}

echo "Done.\n";
echo "</pre>";
