<?php
// chat.php
require_once 'cache.php';

// Инициализация кэша
$cache = new Cache('cache/chat/', 300); // 5 минут кэш

$url = $_SERVER['REQUEST_URI'];
$path_parts = explode('/', $url);
$username = end($path_parts);

if (empty($username) || $username == 'chat.php') {
    header("Location: /");
    exit;
}

$cacheKey = "chat_model_{$username}";

// Пробуем получить из кэша
$model = $cache->get($cacheKey);

if (!$model) {
    require_once 'model.php';
    $model = findModel($username);
    
    // Сохраняем в кэш
    if ($model) {
        $cache->set($cacheKey, $model);
    }
}

if (!$model) {
    header("Location: /model/" . urlencode($username));
    exit;
}

if (empty($model['chat_url'])) {
    header("Location: /model/" . urlencode($username));
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Чат с <?= htmlspecialchars($model['display_name']) ?></title>
    <style>body, html { margin: 0; padding: 0; } iframe { width: 100%; height: 100vh; border: none; }</style>
</head>
<body>
    <iframe src="<?= htmlspecialchars($model['chat_url']) ?>"></iframe>
</body>
</html>