<?php
/**
 * إضافة patient_id و order_id إلى doctor_alerts إذا لم تكونا موجودتين
 * تشغيل تلقائي من doctor.php
 */
$config = require __DIR__ . '/../config/config.php';
$pdo = new PDO(
    'mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['name'] . ';charset=utf8mb4',
    $config['db']['user'],
    $config['db']['pass'],
    [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
);
try {
    $cols = $pdo->query("SHOW COLUMNS FROM doctor_alerts LIKE 'patient_id'")->fetch();
    if (!$cols) {
        $pdo->exec("ALTER TABLE doctor_alerts ADD COLUMN patient_id INT NULL");
    }
} catch (Throwable $e) {}
try {
    $cols = $pdo->query("SHOW COLUMNS FROM doctor_alerts LIKE 'order_id'")->fetch();
    if (!$cols) {
        $pdo->exec("ALTER TABLE doctor_alerts ADD COLUMN order_id INT NULL");
    }
} catch (Throwable $e) {}
try {
    $cols = $pdo->query("SHOW COLUMNS FROM doctor_alerts LIKE 'action_url'")->fetch();
    if (!$cols) {
        $pdo->exec("ALTER TABLE doctor_alerts ADD COLUMN action_url VARCHAR(500) NULL");
    }
} catch (Throwable $e) {}
try {
    $cols = $pdo->query("SHOW COLUMNS FROM doctor_alerts LIKE 'entity_type'")->fetch();
    if (!$cols) {
        $pdo->exec("ALTER TABLE doctor_alerts ADD COLUMN entity_type VARCHAR(50) NULL");
    }
} catch (Throwable $e) {}
try {
    $cols = $pdo->query("SHOW COLUMNS FROM doctor_alerts LIKE 'entity_id'")->fetch();
    if (!$cols) {
        $pdo->exec("ALTER TABLE doctor_alerts ADD COLUMN entity_id INT NULL");
    }
} catch (Throwable $e) {}
try {
    $cols = $pdo->query("SHOW COLUMNS FROM doctor_alerts LIKE 'created_at'")->fetch();
    if (!$cols) {
        $pdo->exec("ALTER TABLE doctor_alerts ADD COLUMN created_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP");
    }
} catch (Throwable $e) {}
