<div class="max-w-4xl mx-auto">
    <div class="flex items-center gap-3 mb-6"><a href="?page=ai" class="text-gray-400 hover:text-gray-600"><svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg></a>
        <div><h1 class="text-2xl font-bold text-gray-900"><?= htmlspecialchars($agent['name']) ?></h1><p class="text-sm text-gray-500"><?= ucwords(str_replace('_',' ',$agent['type'])) ?> Agent</p></div>
        <span class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium <?= $agent['status']==='active' ? 'bg-green-50 text-green-700' : 'bg-gray-100 text-gray-500' ?>"><?= ucfirst($agent['status']) ?></span>
    </div>

    <form method="POST" action="?page=ai&action=update&id=<?= $agent['id'] ?>" class="space-y-6"><?= CSRF::field() ?>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Agent Configuration</h3>
            <div class="space-y-4">
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Status</label>
                    <select name="status" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="active" <?= $agent['status']==='active' ? 'selected' : '' ?>>Active</option><option value="inactive" <?= $agent['status']==='inactive' ? 'selected' : '' ?>>Inactive</option></select></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Instructions / System Prompt</label><textarea name="instructions" rows="8" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500 font-mono" placeholder="You are a helpful property management AI assistant..."><?= htmlspecialchars($agent['instructions'] ?? '') ?></textarea><p class="text-xs text-gray-400 mt-1">Define how this agent should behave, what information it can access, and its communication style.</p></div>
                <div><label class="block text-sm font-medium text-gray-700 mb-1">Escalation Rules</label><textarea name="escalation_rules" rows="4" class="w-full px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500" placeholder="Escalate to human when..."><?= htmlspecialchars($agent['escalation_rules'] ?? '') ?></textarea></div>
            </div>
        </div>
        <div class="flex justify-end gap-3"><a href="?page=ai" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Cancel</a><button type="submit" class="px-6 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium">Save Configuration</button></div>
    </form>

    <!-- Conversation Logs -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 mt-8">
        <div class="px-6 py-4 border-b border-gray-100"><h3 class="font-semibold text-gray-900">Conversation Logs</h3></div>
        <?php if (empty($conversations)): ?><div class="px-6 py-12 text-center text-gray-400">No conversations yet.</div>
        <?php else: ?>
            <div class="divide-y divide-gray-100">
                <?php foreach ($conversations as $c): ?>
                <div class="px-6 py-3 hover:bg-gray-50">
                    <div class="flex justify-between"><span class="text-sm font-medium text-gray-900"><?= htmlspecialchars($c['channel'] ?? 'chat') ?></span><span class="text-xs text-gray-400"><?= timeAgo($c['created_at']) ?></span></div>
                    <p class="text-xs text-gray-500 mt-1 truncate"><?= htmlspecialchars(substr($c['summary'] ?? $c['last_message'] ?? '', 0, 150)) ?></p>
                </div>
                <?php endforeach; ?>
            </div>
        <?php endif; ?>
    </div>
</div>
