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

use App\Database;

$pdo = Database::getConnection();

$map = [
  '0 Rh(+)' => 'O Rh(+)',
  '0 Rh(-)' => 'O Rh(-)',
  '0 rh(+)' => 'O Rh(+)',
  '0 rh(-)' => 'O Rh(-)',
  '0 Rh('  => 'O Rh(+)',
  '0 Rh'   => 'O Rh(+)',
  '0Rh('   => 'O Rh(+)',
  '0Rh'    => 'O Rh(+)',
  '0'      => 'O Rh(+)',
];

$updated = 0;
foreach ($map as $old => $new) {
  $st = $pdo->prepare('UPDATE patients SET blood_type = :new WHERE blood_type = :old');
  $st->execute([':new'=>$new, ':old'=>$old]);
  $updated += $st->rowCount();
}

// catch truncated starting with '0 Rh'
$st2 = $pdo->prepare("UPDATE patients SET blood_type = 'O Rh(+)' WHERE blood_type LIKE '0 Rh%'");
$st2->execute();
$updated += $st2->rowCount();

echo "Updated blood types: {$updated}\n";
