<?php
/**
 * Delete Doctor Account
 * Soft delete or permanent delete based on cases
 */

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

requireAdmin();

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

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

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

// Check if doctor has cases
$stmt = $pdo->prepare("SELECT COUNT(*) as count FROM cases WHERE user_id = ?");
$stmt->execute([$doctorId]);
$caseCount = $stmt->fetch()['count'];

$error = '';
$success = '';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $confirm = $_POST['confirm'] ?? '';
    
    if ($confirm == 'DELETE') {
        beginTransaction();
        
        try {
            // Delete subscription records first
            $stmt = $pdo->prepare("DELETE FROM subscriptions WHERE user_id = ?");
            $stmt->execute([$doctorId]);
            
            // Delete follow-ups (via cases cascade)
            // Delete cases
            $stmt = $pdo->prepare("DELETE FROM cases WHERE user_id = ?");
            $stmt->execute([$doctorId]);
            
            // Delete user
            $stmt = $pdo->prepare("DELETE FROM users WHERE id = ?");
            $stmt->execute([$doctorId]);
            
            commitTransaction();
            
            $success = "Doctor account deleted successfully!";
            header("refresh:2;url=index.php");
            
        } catch (Exception $e) {
            rollbackTransaction();
            $error = "Failed to delete: " . $e->getMessage();
        }
    } else {
        $error = "Please type DELETE to confirm deletion";
    }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Delete Doctor - Admin Panel</title>
    <link href="../../assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../../assets/vendor/fontawesome/css/all.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row">
            <div class="col-md-6 mx-auto">
                <div class="card">
                    <div class="card-header bg-danger text-white">
                        <h4 class="mb-0"><i class="fas fa-exclamation-triangle"></i> Delete Doctor Account</h4>
                    </div>
                    <div class="card-body">
                        <?php if ($error): ?>
                            <div class="alert alert-danger"><?php echo $error; ?></div>
                        <?php endif; ?>
                        
                        <?php if ($success): ?>
                            <div class="alert alert-success"><?php echo $success; ?></div>
                        <?php else: ?>
                            <div class="alert alert-warning">
                                <strong>⚠️ Warning!</strong><br>
                                You are about to delete the following doctor account:
                            </div>
                            
                            <table class="table table-bordered">
                                <tr>
                                    <th>Name:</th>
                                    <td><?php echo escape($doctor['full_name']); ?></td>
                                </tr>
                                <tr>
                                    <th>Username:</th>
                                    <td><?php echo $doctor['username']; ?></td>
                                </tr>
                                <tr>
                                    <th>Email:</th>
                                    <td><?php echo $doctor['email']; ?></td>
                                </tr>
                                <tr>
                                    <th>Cases:</th>
                                    <td><?php echo $caseCount; ?> cases</td>
                                </tr>
                                <tr>
                                    <th>Joined:</th>
                                    <td><?php echo date(DISPLAY_DATE_FORMAT, strtotime($doctor['created_at'])); ?></td>
                                </tr>
                            </table>
                            
                            <div class="alert alert-danger">
                                <strong>This action cannot be undone!</strong><br>
                                All cases and data associated with this doctor will be permanently deleted.
                            </div>
                            
                            <form method="POST">
                                <div class="mb-3">
                                    <label>Type <strong>DELETE</strong> to confirm:</label>
                                    <input type="text" name="confirm" class="form-control" placeholder="DELETE" required>
                                </div>
                                <button type="submit" class="btn btn-danger">
                                    <i class="fas fa-trash"></i> Permanently Delete
                                </button>
                                <a href="index.php" class="btn btn-secondary">Cancel</a>
                            </form>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>