{% extends "base.html" %}
{% block title %}Chat - {{ session.id }} - Personaut{% endblock %}

{% block content %}
<div class="chat-container">
    <header class="chat-header">
        <div class="chat-info">
            <h2>{{ individual.name }}</h2>
            <span class="badge">{{ modality }}</span>
        </div>
        <div class="chat-controls">
            <button class="btn btn-sm btn-secondary" id="clear-chat">Clear</button>
            <a href="{{ url_for('chat.select_session') }}" class="btn btn-sm btn-secondary">End</a>
        </div>
    </header>

    <div class="messages" id="messages">
        {% for message in messages %}
        <div class="message {% if message.sender == 'user' %}sent{% else %}received{% endif %}">
            <div class="message-sender">{{ message.sender_name or message.sender }}</div>
            <div class="message-content">{{ message.content }}</div>
            <div class="message-time">{{ message.timestamp }}</div>
        </div>
        {% endfor %}
    </div>

    <form class="input-area" id="message-form">
        <input type="text" id="message-input" placeholder="Type a message..." autocomplete="off">
        <button type="submit" class="btn btn-primary">Send</button>
    </form>
</div>

<style>
    .chat-container {
        display: flex;
        flex-direction: column;
        height: calc(100vh - 120px);
    }

    .chat-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1rem;
        background: var(--bg-secondary);
        border-radius: 12px 12px 0 0;
    }

    .chat-info {
        display: flex;
        align-items: center;
        gap: 1rem;
    }

    .messages {
        flex: 1;
        overflow-y: auto;
        padding: 1rem;
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .message {
        max-width: 70%;
        padding: 0.75rem 1rem;
        border-radius: 16px;
    }

    .message.sent {
        background: var(--accent);
        align-self: flex-end;
        border-bottom-right-radius: 4px;
    }

    .message.received {
        background: var(--bg-tertiary);
        align-self: flex-start;
        border-bottom-left-radius: 4px;
    }

    .message-sender {
        font-size: 0.75rem;
        opacity: 0.7;
        margin-bottom: 0.25rem;
    }

    .message-time {
        font-size: 0.65rem;
        opacity: 0.5;
        text-align: right;
        margin-top: 0.25rem;
    }

    .input-area {
        display: flex;
        gap: 0.5rem;
        padding: 1rem;
        background: var(--bg-secondary);
        border-radius: 0 0 12px 12px;
    }

    .input-area input {
        flex: 1;
        padding: 0.75rem;
        border-radius: 20px;
        border: none;
        background: var(--bg-tertiary);
        color: var(--text-primary);
    }
</style>
{% endblock %}

{% block scripts %}
<script>
    const sessionId = '{{ session.id }}';
    const ws = new PersonautWebSocket();

    document.getElementById('message-form').addEventListener('submit', async (e) => {
        e.preventDefault();
        const input = document.getElementById('message-input');
        const content = input.value.trim();
        if (!content) return;

        // Add message to UI
        addMessage({ sender: 'user', sender_name: 'You', content: content, timestamp: new Date().toLocaleTimeString() });
        input.value = '';

        // Send via WebSocket
        ws.sendMessage(sessionId, content);
    });

    function addMessage(msg) {
        const messages = document.getElementById('messages');
        const div = document.createElement('div');
        div.className = `message ${msg.sender === 'user' ? 'sent' : 'received'}`;
        div.innerHTML = `
        <div class="message-sender">${msg.sender_name}</div>
        <div class="message-content">${msg.content}</div>
        <div class="message-time">${msg.timestamp}</div>
    `;
        messages.appendChild(div);
        messages.scrollTop = messages.scrollHeight;
    }

    // Listen for responses
    ws.addHandler('message', (data) => {
        addMessage(data);
    });
</script>
{% endblock %}