{% extends 'base.html.twig' %}

{% block title %}Game Dashboard{% endblock %}

{% block body %}
    <h1>Game Dashboard</h1>

    {% if is_granted('ROLE_ADMIN') %}
        <p><a href="{{ path('game_admin_dashboard') }}">Go to Game Admin Dashboard</a></p>
    {% endif %}

    <h2>Create New Session</h2>
    {% if availableGames is not empty %}
        <form method="post">
            <select name="game_id">
                {% for game in availableGames %}
                    <option value="{{ game.id }}">
                        {{ game.name }} ({{ game.numberOfPlayers }} players)
                        {% if is_granted('ROLE_ADMIN') %}
                            [{{ game.status.value }}]
                        {% endif %}
                    </option>
                {% endfor %}
            </select>
            <button type="submit" name="create_session">Create Session</button>
        </form>
    {% else %}
        <p>No games available to start.</p>
    {% endif %}

    <h2>Join Session</h2>
    <form method="post">
        <input type="text" name="invite_code" placeholder="Enter Invite Code" required>
        <button type="submit" name="join_session">Join Session</button>
    </form>

    <h2>Your Sessions</h2>
    {% if sessions is not empty %}
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Game</th>
                    <th>Status</th>
                    <th>Created At</th>
                    <th>Invite Code</th>
                    <th>Actions</th>
                </tr>
            </thead>
            <tbody>
                {% for session in sessions %}
                    <tr>
                        <td>{{ session.id }}</td>
                        <td>{{ session.game.name }}</td>
                        <td>{{ session.status.value }}</td>
                        <td>{{ session.created|date('Y-m-d H:i') }}</td>
                        <td>
                            {% set inviteCode = '' %}
                            {% for setting in session.settings %}
                                {% if setting.name.value == 'InviteCode' %}
                                    {% set inviteCode = setting.value %}
                                {% endif %}
                            {% endfor %}

                            {% if inviteCode %}
                                <code>{{ inviteCode }}</code>
                            {% else %}
                                <form method="post" style="display:inline;">
                                    <input type="hidden" name="session_id" value="{{ session.id }}">
                                    <button type="submit" name="create_invite">Generate Invite</button>
                                </form>
                            {% endif %}
                        </td>
                        <td>
                            <a href="{{ path('game', {session: session.id}) }}">Enter Game</a>
                            {% if session.status.value == 'created' %}
                                {% if session.players|length >= session.game.numberOfPlayers %}
                                    <form method="post" style="display:inline;">
                                        <input type="hidden" name="session_id" value="{{ session.id }}">
                                        <button type="submit" name="start_session">Start Session</button>
                                    </form>
                                {% endif %}
                                {% if session.timer == 0 %}
                                    <form method="post" style="display:inline;">
                                        <input type="hidden" name="session_id" value="{{ session.id }}">
                                        <button type="submit" name="leave_session" onclick="return confirm('Are you sure you want to leave this session?')">Leave Session</button>
                                    </form>
                                {% endif %}
                            {% endif %}
                        </td>
                    </tr>
                {% endfor %}
            </tbody>
        </table>
    {% else %}
        <p>You are not part of any sessions.</p>
    {% endif %}
{% endblock %}
