<?php
/**
 * Toggle Doctor Status
 * Activate or deactivate a doctor account
 */

require_once '../../config/config.php';
require_once '../../includes/functions.php';

requireAdmin();

$pdo = getDB();
$doctorId = (int)($_GET['id'] ?? 0);

// Get current status
$stmt = $pdo->prepare("SELECT is_active, full_name FROM users WHERE id = ? AND role = 'doctor'");
$stmt->execute([$doctorId]);
$doctor = $stmt->fetch();

if (!$doctor) {
    header('Location: index.php?error=Doctor not found');
    exit;
}

// Toggle status
$newStatus = $doctor['is_active'] ? 0 : 1;
$statusText = $newStatus ? 'activated' : 'deactivated';

$stmt = $pdo->prepare("UPDATE users SET is_active = ? WHERE id = ?");
if ($stmt->execute([$newStatus, $doctorId])) {
    $message = "Doctor {$doctor['full_name']} has been {$statusText}.";
    header("Location: index.php?success=" . urlencode($message));
} else {
    header("Location: index.php?error=Failed to update status");
}
exit;
?>