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

use App\Database;

$pdo = Database::getConnection();

// Hasta 11'i al
$st = $pdo->prepare('SELECT * FROM patients WHERE id = :id');
$st->execute([':id' => 11]);
$patient = $st->fetch();

if ($patient) {
    // Yakınları sil
    $pdo->prepare('DELETE FROM patient_relatives WHERE patient_id = :id')->execute([':id' => 11]);
    
    // Yeni yakınları ekle (düzenleme formundan gelebilecek POST verilerinin simülasyonu)
    // Forme göre: relative_relation_0, relative_name_0, relative_phone_0, etc.
    
    $relatives = [
        [
            'relation' => 'Eş',
            'full_name' => 'Ayşe Yılmaz',
            'phone' => '555-5678',
            'email' => 'ayse@example.com',
            'address' => 'Ankara'
        ],
        [
            'relation' => 'Çocuk',
            'full_name' => 'Ali Yılmaz',
            'phone' => '555-9999',
            'email' => 'ali@example.com',
            'address' => 'İstanbul'
        ]
    ];
    
    foreach ($relatives as $rel) {
        $ins = $pdo->prepare('INSERT INTO patient_relatives (patient_id, relation, full_name, phone, email, address) VALUES (:pid, :rel, :name, :phone, :email, :addr)');
        $ins->execute([
            ':pid' => 11,
            ':rel' => $rel['relation'],
            ':name' => $rel['full_name'],
            ':phone' => $rel['phone'],
            ':email' => $rel['email'],
            ':addr' => $rel['address']
        ]);
    }
    
    echo "Yakınlar kaydedildi!\n";
    
    // Kontrol et
    $check = $pdo->prepare('SELECT COUNT(*) as cnt FROM patient_relatives WHERE patient_id = :id');
    $check->execute([':id' => 11]);
    $result = $check->fetch();
    echo "Toplam yakın sayısı: " . $result['cnt'] . "\n";
    
    // Yakınları listele
    $rels = $pdo->prepare('SELECT * FROM patient_relatives WHERE patient_id = :id');
    $rels->execute([':id' => 11]);
    $relatives = $rels->fetchAll();
    
    echo "\nYakınlar:\n";
    foreach ($relatives as $rel) {
        echo "- {$rel['relation']}: {$rel['full_name']} ({$rel['phone']})\n";
    }
} else {
    echo "Hasta bulunamadı\n";
}
?>
