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

use App\Database;

$pdo = Database::getConnection();

// Hasta 11'i var olan veriler ile simulate et
$id = 11;

// 1. Mevcut yakınları oku
$rels = $pdo->prepare('SELECT relation, full_name, phone, email, address FROM patient_relatives WHERE patient_id = :id');
$rels->execute([':id' => $id]);
$relatives = array_values($rels->fetchAll());

echo "Adım 1: Mevcut yakınlar okudu\n";
echo "Yakın sayısı: " . count($relatives) . "\n";
foreach ($relatives as $i => $rel) {
    echo "  [$i] {$rel['full_name']}\n";
}

// 2. POST data simulate et (3 yakın: eski iki adet + yeni bir adet)
$_POST['relative_count'] = '3';
$_POST['relative_relation_0'] = 'Eş';
$_POST['relative_name_0'] = 'Ayşe Yılmaz';
$_POST['relative_phone_0'] = '555-5678';
$_POST['relative_email_0'] = 'ayse@example.com';
$_POST['relative_address_0'] = 'Ankara';

$_POST['relative_relation_1'] = 'Çocuk';
$_POST['relative_name_1'] = 'Ali Yılmaz';
$_POST['relative_phone_1'] = '555-9999';
$_POST['relative_email_1'] = 'ali@example.com';
$_POST['relative_address_1'] = 'İstanbul';

$_POST['relative_relation_2'] = 'Anne';
$_POST['relative_name_2'] = 'Zümrüt Yılmaz';  // Yeni
$_POST['relative_phone_2'] = '555-1111';
$_POST['relative_email_2'] = 'zumrut@example.com';
$_POST['relative_address_2'] = 'İzmir';

echo "\nAdım 2: POST data hazırlandı\n";
echo "Gönderilecek yakın sayısı: " . $_POST['relative_count'] . "\n";

// 3. DELETE ve INSERT işlemini yap (hastaDuzenle.php'deki kod)
$pdo->prepare('DELETE FROM patient_relatives WHERE patient_id = :id')->execute([':id' => $id]);
echo "\nAdım 3: Tüm yakınlar silindi\n";

$rel_count = isset($_POST['relative_count']) ? (int)$_POST['relative_count'] : 0;
for ($i = 0; $i < $rel_count; $i++) {
    $relation = trim($_POST["relative_relation_$i"] ?? '');
    $full_name = trim($_POST["relative_name_$i"] ?? '');
    $rel_phone = trim($_POST["relative_phone_$i"] ?? '');
    $rel_email = trim($_POST["relative_email_$i"] ?? '');
    $rel_address = trim($_POST["relative_address_$i"] ?? '');
    
    if ($full_name) {
        $rIns = $pdo->prepare('INSERT INTO patient_relatives (patient_id, relation, full_name, phone, email, address) VALUES (:patient_id, :relation, :full_name, :phone, :email, :address)');
        $rIns->execute([
            ':patient_id' => $id,
            ':relation' => $relation ?: null,
            ':full_name' => $full_name,
            ':phone' => $rel_phone ?: null,
            ':email' => $rel_email ?: null,
            ':address' => $rel_address ?: null,
        ]);
        echo "  Eklendi: $full_name\n";
    }
}

echo "\nAdım 4: Yeni yakınlar eklendi\n";

// 4. Kontrol et
$check = $pdo->prepare('SELECT COUNT(*) as cnt FROM patient_relatives WHERE patient_id = :id');
$check->execute([':id' => $id]);
$result = $check->fetch();
echo "Toplam yakın sayısı: " . $result['cnt'] . "\n";

$rels = $pdo->prepare('SELECT relation, full_name FROM patient_relatives WHERE patient_id = :id ORDER BY id');
$rels->execute([':id' => $id]);
$relatives = $rels->fetchAll();

echo "\nSonuç:\n";
foreach ($relatives as $rel) {
    echo "  - {$rel['relation']}: {$rel['full_name']}\n";
}
?>
