<?php
require __DIR__ . '/../vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->safeLoad();

use App\Database;

$pdo = Database::getConnection();

$doctors = [
  ['full_name' => 'Dr. Ayla Karaca', 'email' => 'ayla.karaca@example.com', 'phone' => '0532 210 10 10', 'specializations' => ['Nefroloji']],
  ['full_name' => 'Dr. Murat Sezer', 'email' => 'murat.sezer@example.com', 'phone' => '0533 310 10 10', 'specializations' => ['Dahiliye', 'Nefroloji']],
  ['full_name' => 'Dr. Selin Öz', 'email' => 'selin.oz@example.com', 'phone' => '0534 410 10 10', 'specializations' => ['Kardiyoloji']],
  ['full_name' => 'Dr. Kerem Yıldız', 'email' => 'kerem.yildiz@example.com', 'phone' => '0535 510 10 10', 'specializations' => ['Göğüs Hastalıkları']],
  ['full_name' => 'Dr. Elif Aksoy', 'email' => 'elif.aksoy@example.com', 'phone' => '0536 610 10 10', 'specializations' => ['Enfeksiyon Hastalıkları']],
  ['full_name' => 'Dr. Onur Demir', 'email' => 'onur.demir@example.com', 'phone' => '0537 710 10 10', 'specializations' => ['Acil Tıp']],
  ['full_name' => 'Dr. Deniz Arslan', 'email' => 'deniz.arslan@example.com', 'phone' => '0538 810 10 10', 'specializations' => ['Nöroloji']],
  ['full_name' => 'Dr. Ceren Kaya', 'email' => 'ceren.kaya@example.com', 'phone' => '0539 910 10 10', 'specializations' => ['Genel Cerrahi']],
  ['full_name' => 'Dr. Batuhan Erel', 'email' => 'batuhan.erel@example.com', 'phone' => '0540 010 10 10', 'specializations' => ['Hemşirelik']],
  ['full_name' => 'Dr. İpek Tan', 'email' => 'ipek.tan@example.com', 'phone' => '0541 110 10 10', 'specializations' => ['Anesteziyoloji']]
];

$pdo->beginTransaction();
try {
  // Ensure specializations exist
  $existingSpecs = $pdo->query('SELECT id, name FROM specializations')->fetchAll();
  $specMap = [];
  foreach ($existingSpecs as $row) {
    $specMap[$row['name']] = (int)$row['id'];
  }

  $insertSpec = $pdo->prepare('INSERT IGNORE INTO specializations (name) VALUES (:name)');
  foreach ($doctors as $d) {
    foreach ($d['specializations'] as $sn) {
      if (!isset($specMap[$sn])) {
        $insertSpec->execute([':name' => $sn]);
        $specMap[$sn] = (int)$pdo->lastInsertId();
      }
    }
  }

  $selectDoctor = $pdo->prepare('SELECT id FROM doctors WHERE email = :email');
  $insertDoctor = $pdo->prepare('INSERT INTO doctors (full_name, email, phone) VALUES (:full_name, :email, :phone)');
  $insertLink = $pdo->prepare('INSERT IGNORE INTO doctor_specializations (doctor_id, specialization_id) VALUES (:doctor_id, :specialization_id)');

  foreach ($doctors as $d) {
    $selectDoctor->execute([':email' => $d['email']]);
    $doctorId = $selectDoctor->fetchColumn();
    if (!$doctorId) {
      $insertDoctor->execute([
        ':full_name' => $d['full_name'],
        ':email' => $d['email'],
        ':phone' => $d['phone']
      ]);
      $doctorId = (int)$pdo->lastInsertId();
    }

    foreach ($d['specializations'] as $sn) {
      $specId = $specMap[$sn] ?? null;
      if ($specId) {
        $insertLink->execute([
          ':doctor_id' => $doctorId,
          ':specialization_id' => $specId
        ]);
      }
    }
  }

  $pdo->commit();
  echo 'Seeded ' . count($doctors) . " doctors with specializations.\n";
} catch (Throwable $e) {
  if ($pdo->inTransaction()) {
    $pdo->rollBack();
  }
  echo 'Seed failed: ' . $e->getMessage() . "\n";
}
