<!DOCTYPE html>
<html lang="en" data-theme="light">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OpenCompletion - AI-Powered Chat Rooms</title>
    <link rel="icon" href="{{ url_for('static', filename='favicon.ico') }}">
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.min.js"></script>
    <script src="{{ url_for('static', filename='js/utils.js') }}"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: var(--bg-page);
            color: var(--text-primary);
            display: grid;
            place-items: center;
            min-height: 100vh;
            margin: 0;
            padding: 20px;
            transition: background-color 0.3s ease, color 0.3s ease;
        }
        #main-container {
            background-color: var(--bg-card);
            border-radius: 10px;
            padding: 30px;
            width: 100%;
            max-width: 800px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
            transition: background-color 0.3s ease;
        }
        h1 {
            color: var(--text-primary);
            margin-bottom: 10px;
            text-align: center;
            transition: color 0.3s ease;
        }
        .tagline {
            text-align: center;
            color: var(--text-secondary);
            margin-bottom: 30px;
            transition: color 0.3s ease;
        }
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
            gap: 15px;
            margin-bottom: 30px;
        }
        .stat-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
        }
        .stat-number {
            font-size: 32px;
            font-weight: bold;
            margin-bottom: 5px;
        }
        .stat-label {
            font-size: 14px;
            opacity: 0.9;
        }
        #join-section {
            background-color: var(--bg-section);
            border-radius: 8px;
            padding: 25px;
            margin-bottom: 20px;
            transition: background-color 0.3s ease;
        }
        #join-section h2 {
            margin-top: 0;
            color: var(--text-primary);
            transition: color 0.3s ease;
        }
        #join-room-form {
            display: grid;
            gap: 10px;
        }
        input[type="text"] {
            width: 100%;
            border: 2px solid var(--border-color);
            border-radius: 5px;
            padding: 12px;
            font-size: 16px;
            box-sizing: border-box;
            background-color: var(--bg-card);
            color: var(--text-primary);
            transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease;
        }
        input[type="text"]:focus {
            outline: none;
            border-color: #667eea;
        }
        .room-type-selector {
            display: grid;
            grid-auto-flow: column;
            grid-auto-columns: max-content;
            gap: 20px;
            align-items: center;
            padding: 10px 0;
        }
        .room-type-selector label {
            display: grid;
            grid-auto-flow: column;
            align-items: center;
            gap: 5px;
            cursor: pointer;
            font-size: 16px;
        }
        .room-type-selector input[type="radio"] {
            width: auto;
            cursor: pointer;
        }
        button {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 5px;
            padding: 12px;
            font-size: 16px;
            cursor: pointer;
            transition: transform 0.2s;
        }
        button:hover {
            transform: translateY(-2px);
        }
        .auth-status {
            text-align: center;
            padding: 15px;
            background: var(--bg-status);
            border-radius: 8px;
            margin-bottom: 20px;
            transition: background-color 0.3s ease;
        }
        .auth-status.logged-in {
            background: var(--bg-status-logged);
        }
        .browse-link {
            text-align: center;
            margin-top: 20px;
        }
        .browse-link a {
            color: #667eea;
            text-decoration: none;
            font-weight: bold;
        }
        .browse-link a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div id="main-container">
        <h1>🚀 OpenCompletion</h1>
        <p class="tagline">Machine Learning Powered Collaboration</p>

        <!-- Statistics -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-number">{{ stats.total_public_rooms }}</div>
                <div class="stat-label">Public Rooms</div>
            </div>
            {% if user %}
            <div class="stat-card">
                <div class="stat-number">{{ stats.total_private_rooms }}</div>
                <div class="stat-label">Private Rooms</div>
            </div>
            {% endif %}
            <div class="stat-card">
                <div class="stat-number">{{ stats.active_public_rooms }}</div>
                <div class="stat-label">Active Rooms</div>
            </div>
            <div class="stat-card">
                <div class="stat-number">{{ stats.active_users }}</div>
                <div class="stat-label">Active Users</div>
            </div>
        </div>

        <!-- Auth status -->
        {% if user %}
        <div class="auth-status logged-in">
            ✅ Signed in as <strong>{{ user.display_name }}</strong> ({{ user.email }})
            <div style="margin-top: 10px;">
                <a href="/profile" style="color: #667eea; text-decoration: none; font-weight: bold;">⚙️ Profile Settings</a>
            </div>
        </div>
        {% else %}
        <div class="auth-status">
            👋 Welcome! <a href="/auth">Sign in</a> to create private rooms
        </div>
        {% endif %}

        <!-- Join room form -->
        <div id="join-section">
            <h2>Join or Create a Room</h2>
            <form id="join-room-form">
                <input id="room" type="text" placeholder="Enter room name" required>
                <div class="room-type-selector">
                    <label>
                        <input type="radio" name="room-type" value="public" checked>
                        🌍 Public Room
                    </label>
                    <label>
                        <input type="radio" name="room-type" value="private">
                        🔐 Private Room
                    </label>
                </div>
                <button type="submit">Create/Join Room</button>
            </form>
        </div>

        <div class="browse-link">
            <a href="/browse">Or browse existing rooms →</a>
        </div>
    </div>

    <script>
        // Theme persistence
        const savedTheme = localStorage.getItem('theme') || 'light';
        document.documentElement.setAttribute('data-theme', savedTheme);

        document.getElementById('join-room-form').addEventListener('submit', async function(e) {
            e.preventDefault();
            const roomInput = document.getElementById('room').value.trim();
            const roomType = document.querySelector('input[name="room-type"]:checked').value;

            if (!roomInput) {
                alert('Please enter a room name');
                return;
            }

            const room = slugify(roomInput);

            if (!room) {
                alert('Please enter a valid room name with at least some letters or numbers');
                return;
            }

            // If private room, check authentication
            if (roomType === 'private') {
                {% if not user %}
                alert('You must be signed in to create private rooms. Please sign in first.');
                window.location.href = '/auth';
                return;
                {% endif %}

                // Create private room via API
                try {
                    const response = await fetch('/api/rooms/create', {
                        method: 'POST',
                        headers: {'Content-Type': 'application/json'},
                        body: JSON.stringify({
                            name: room,
                            is_private: true
                        })
                    });

                    const data = await response.json();

                    if (response.ok) {
                        window.location.href = `/chat/${data.room.name}`;
                    } else {
                        alert(data.error || 'Failed to create private room');
                    }
                } catch (error) {
                    alert('Network error. Please try again.');
                }
            } else {
                // Public room - just navigate
                window.location.href = `/chat/${room}`;
            }
        });
    </script>
</body>
</html>

