<?php
require_once __DIR__ . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();

use App\Database;

$pdo = Database::getConnection();

// Temizle
$pdo->exec('DELETE FROM patient_relatives');
$pdo->exec('DELETE FROM patient_notes');
$pdo->exec('DELETE FROM patient_referrals');
$pdo->exec('DELETE FROM patients');
$pdo->exec('DELETE FROM doctors');
$pdo->exec('DELETE FROM institutions');

// Test Kurumları Ekle
$institutions = [
    ['name' => 'Ankara Şehir Hastanesi'],
    ['name' => 'İstanbul Tıp Merkezi'],
    ['name' => 'İzmir Üniversite Hastanesi'],
];

foreach ($institutions as $inst) {
    $ins = $pdo->prepare('INSERT INTO institutions (name, type) VALUES (:name, :type)');
    $ins->execute([':name' => $inst['name'], ':type' => 'Hospital']);
}

// Test Doktorları Ekle
$doctors = [
    ['name' => 'Dr. Mehmet Kaya'],
    ['name' => 'Dr. Ayşe Çelik'],
    ['name' => 'Dr. Osman Şahin'],
    ['name' => 'Dr. Fatma Yıldız'],
    ['name' => 'Dr. Ahmet Demir'],
];

foreach ($doctors as $doc) {
    $ins = $pdo->prepare('INSERT INTO doctors (full_name) VALUES (:name)');
    $ins->execute([':name' => $doc['name']]);
}

// Test Hastaları Ekle
$patients = [
    [
        'first_name' => 'Hasan',
        'last_name' => 'Kaya',
        'national_id' => '12345678901',
        'birth_date' => '1975-05-12',
        'gender' => 'male',
        'blood_type' => 'O Rh(+)',
        'phone' => '0312-1234567',
        'email' => 'hasan.kaya@example.com',
        'city' => 'Ankara',
        'district' => 'Çankaya',
        'address' => 'Ankara Caddesi No:45',
    ],
    [
        'first_name' => 'Elif',
        'last_name' => 'Arslan',
        'national_id' => '12345678902',
        'birth_date' => '1982-03-25',
        'gender' => 'female',
        'blood_type' => 'A Rh(-)',
        'phone' => '0542-8765432',
        'email' => 'elif.arslan@example.com',
        'city' => 'İstanbul',
        'district' => 'Kadıköy',
        'address' => 'Moda Cad. No:100',
    ],
    [
        'first_name' => 'Mustafa',
        'last_name' => 'Demir',
        'national_id' => '12345678903',
        'birth_date' => '1988-07-14',
        'gender' => 'male',
        'blood_type' => 'B Rh(+)',
        'phone' => '0533-5555555',
        'email' => 'mustafa.demir@example.com',
        'city' => 'İzmir',
        'district' => 'Alsancak',
        'address' => 'Alsancak Pasajı No:22',
    ],
    [
        'first_name' => 'Zeynep',
        'last_name' => 'Güzel',
        'national_id' => '12345678904',
        'birth_date' => '1990-11-03',
        'gender' => 'female',
        'blood_type' => 'AB Rh(+)',
        'phone' => '0505-4444444',
        'email' => 'zeynep.guzel@example.com',
        'city' => 'Ankara',
        'district' => 'Keçiören',
        'address' => 'Keçiören Mah. No:88',
    ],
    [
        'first_name' => 'Ibrahim',
        'last_name' => 'Yıldız',
        'national_id' => '12345678905',
        'birth_date' => '1980-02-18',
        'gender' => 'male',
        'blood_type' => 'O Rh(-)',
        'phone' => '0216-3333333',
        'email' => 'ibrahim.yildiz@example.com',
        'city' => 'İstanbul',
        'district' => 'Beyoğlu',
        'address' => 'İstiklal Cad. No:150',
    ],
    [
        'first_name' => 'Nurgül',
        'last_name' => 'Ak',
        'national_id' => '12345678906',
        'birth_date' => '1985-09-07',
        'gender' => 'female',
        'blood_type' => 'A Rh(+)',
        'phone' => '0232-2222222',
        'email' => 'nurgul.ak@example.com',
        'city' => 'İzmir',
        'district' => 'Konak',
        'address' => 'Konak Liman Cad. No:55',
    ],
    [
        'first_name' => 'Kerem',
        'last_name' => 'Özdemir',
        'national_id' => '12345678907',
        'birth_date' => '1992-06-21',
        'gender' => 'male',
        'blood_type' => 'B Rh(-)',
        'phone' => '0555-1111111',
        'email' => 'kerem.ozdemir@example.com',
        'city' => 'Ankara',
        'district' => 'Çukurambar',
        'address' => 'Çukurambar Mah. No:12',
    ],
    [
        'first_name' => 'Sema',
        'last_name' => 'Polat',
        'national_id' => '12345678908',
        'birth_date' => '1979-12-30',
        'gender' => 'female',
        'blood_type' => 'AB Rh(-)',
        'phone' => '0541-0000000',
        'email' => 'sema.polat@example.com',
        'city' => 'İstanbul',
        'district' => 'Bakırköy',
        'address' => 'Bakırköy Sahil Cad. No:200',
    ],
    [
        'first_name' => 'Cem',
        'last_name' => 'Kocabaş',
        'national_id' => '12345678909',
        'birth_date' => '1986-04-16',
        'gender' => 'male',
        'blood_type' => 'O Rh(+)',
        'phone' => '0246-9876543',
        'email' => 'cem.kocabas@example.com',
        'city' => 'İzmir',
        'district' => 'Buca',
        'address' => 'Buca Kampüs Cad. No:78',
    ],
    [
        'first_name' => 'Pınar',
        'last_name' => 'Kaplan',
        'national_id' => '12345678910',
        'birth_date' => '1994-08-09',
        'gender' => 'female',
        'blood_type' => 'A Rh(-)',
        'phone' => '0533-7777777',
        'email' => 'pinar.kaplan@example.com',
        'city' => 'Ankara',
        'district' => 'Belkız',
        'address' => 'Belkız Mahallesi No:33',
    ],
];

$patient_ids = [];
foreach ($patients as $idx => $p) {
    $ins = $pdo->prepare('INSERT INTO patients (first_name, last_name, national_id, birth_date, gender, blood_type, phone, email, city, district, address) VALUES (:fn, :ln, :ni, :bd, :g, :bt, :ph, :em, :ci, :d, :a)');
    $ins->execute([
        ':fn' => $p['first_name'],
        ':ln' => $p['last_name'],
        ':ni' => $p['national_id'],
        ':bd' => $p['birth_date'],
        ':g' => $p['gender'],
        ':bt' => $p['blood_type'],
        ':ph' => $p['phone'],
        ':em' => $p['email'],
        ':ci' => $p['city'],
        ':d' => $p['district'],
        ':a' => $p['address'],
    ]);
    $patient_ids[$idx] = $pdo->lastInsertId();
}

// Hastaları Doktor/Kurum ile İlişkilendir
$pdo->exec('SET FOREIGN_KEY_CHECKS=0');
$doctor_ids = $pdo->query('SELECT id FROM doctors')->fetchAll(PDO::FETCH_COLUMN);
$institution_ids = $pdo->query('SELECT id FROM institutions')->fetchAll(PDO::FETCH_COLUMN);

$referral_reasons = ['Böbrek taş', 'Kronik böbrek hastalığı', 'Hipertansiyon', 'Diabet', 'Üretral darlaşma'];

foreach ($patient_ids as $idx => $pid) {
    $doctor_id = $doctor_ids[$idx % count($doctor_ids)];
    $institution_id = $institution_ids[$idx % count($institution_ids)];
    $reason = $referral_reasons[$idx % count($referral_reasons)];
    
    $ref = $pdo->prepare('INSERT INTO patient_referrals (patient_id, doctor_id, institution_id, referral_reason, referral_date) VALUES (:pid, :did, :iid, :reason, NOW())');
    $ref->execute([':pid' => $pid, ':did' => $doctor_id, ':iid' => $institution_id, ':reason' => $reason]);
    
    // Notlar ekle
    $note = $pdo->prepare('INSERT INTO patient_notes (patient_id, note_type, content, created_at) VALUES (:pid, :type, :content, NOW())');
    $note->execute([
        ':pid' => $pid,
        ':type' => 'tibbi',
        ':content' => 'İlk muayene tamamlandı. Ruttin testler yapılması gerekiyor.'
    ]);
    
    // Yakınlar ekle
    if ($idx % 2 == 0) {
        $rel = $pdo->prepare('INSERT INTO patient_relatives (patient_id, relation, full_name, phone) VALUES (:pid, :rel, :name, :phone)');
        $rel->execute([':pid' => $pid, ':rel' => 'Eş', ':name' => 'İsim Soyadı', ':phone' => '0555-1234567']);
    }
}
$pdo->exec('SET FOREIGN_KEY_CHECKS=1');

echo "✓ 10 demo hasta başarıyla oluşturuldu!\n";
echo "✓ 3 test kurumu oluşturuldu!\n";
echo "✓ 5 test doktoru oluşturuldu!\n";
echo "✓ Notlar ve yakınlar eklendi!\n";
?>
