<?php
session_start();
if (!isset($_SESSION['doctor_id'])) {
    header("Location: login.php");
    exit();
}
include '../include/config.php';
$doctor_id = $_SESSION['doctor_id'];
$ai_response = "";

if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['ask_ai'])) {
    $query = $conn->real_escape_string($_POST['ai_query']);
    // Simple AI logic (can be upgraded to OpenRouter/GPT later)
    $ai_response = "Based on clinical guidelines for UHD: “$query”. Please review patient history before prescribing.";
    $conn->query("INSERT INTO ai_consultation_assistant (doctor_id, query_text, ai_response) VALUES ($doctor_id, '$query', '$ai_response')");
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>AI Medical Assistant | UHD</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        body{ background: #0a3d2f; }
        .card{ border-radius: 20px; margin-top: 50px; }
        .btn-gold{ background: #d4a017; color: white; }
    </style>
</head>
<body>
<div class="container">
    <div class="card shadow">
        <div class="card-header bg-success text-white"><h3><i class="fas fa-robot"></i> UHD AI Clinical Assistant</h3></div>
        <div class="card-body">
            <form method="POST">
                <textarea name="ai_query" class="form-control" rows="4" placeholder="Ask anything: symptoms, diagnosis, drug interaction..."></textarea>
                <button type="submit" name="ask_ai" class="btn btn-gold mt-3 w-100">Ask AI</button>
            </form>
            <?php if($ai_response): ?>
                <div class="alert alert-info mt-4"><?= nl2br(htmlspecialchars($ai_response)) ?></div>
            <?php endif; ?>
            <a href="uhd_dashboard.php" class="btn btn-secondary mt-3">← Back to Dashboard</a>
        </div>
    </div>
</div>
<script src="https://kit.fontawesome.com/a076d05399.js"></script>
</body>
</html>