<?php
$host = '127.0.0.1';
$db = 'nexcore_pharma';
$user = 'nexcore_pharma';
$pass = 'nexova@2026';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8mb4", $user, $pass);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    // Check if channel exists
    $stmt = $pdo->query("SHOW COLUMNS FROM notifyalertlog LIKE 'channel'");
    if ($stmt->rowCount() == 0) {
        $pdo->exec("ALTER TABLE notifyalertlog ADD COLUMN channel VARCHAR(50) DEFAULT 'Email' AFTER alert_type");
        // Update existing whatsapp records if any based on alert_type
        $pdo->exec("UPDATE notifyalertlog SET channel = 'WhatsApp', alert_type = 'manual' WHERE alert_type = 'manual_whatsapp'");
        $pdo->exec("UPDATE notifyalertlog SET channel = 'WhatsApp', alert_type = 'scheduler' WHERE alert_type = 'scheduler_whatsapp'");
        echo "Added channel column and updated existing records.\n";
    } else {
        echo "channel already exists.\n";
    }

    echo "Migration completed successfully.\n";
} catch(PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}
