{% extends "base.html" %}
{% block title %}{{ g.name }}{% endblock %}
{% block content %}
<div class="bbs-header">
    <h1>
        {% if g.tag %}<span class="badge bg-secondary">{{ g.tag }}</span>{% endif %}
        {{ g.name }}
    </h1>
    <p class="small text-muted">
        Leader: <a href="{{ url_for('profile.view_user', username=g.leader.username) }}">{{ g.leader.username }}</a> ·
        Founded {{ g.created_at.strftime('%Y-%m-%d') }} ·
        {% if g.is_open %}Open{% else %}Invite-only{% endif %}
    </p>
</div>

{% if g.description %}
<div class="card mb-3"><div class="card-body">{{ g.description }}</div></div>
{% endif %}

<div class="card mb-3">
    <div class="card-header">Members ({{ members | length }})</div>
    <ul class="list-group list-group-flush">
    {% for m in members %}
        <li class="list-group-item d-flex justify-content-between align-items-center">
            <span>
                <a href="{{ url_for('profile.view_user', username=m.user.username) }}">
                    {{ m.user.username }}
                </a>
                {% if m.user.id == g.leader_id %}<span class="badge bg-warning ms-1">leader</span>{% endif %}
                <small class="text-muted ms-2">since {{ m.joined_at.strftime('%Y-%m-%d') }}</small>
            </span>
            {% if current_user.is_authenticated and (current_user.id == g.leader_id or current_user.is_admin) and m.user.id != g.leader_id %}
                <form method="post" action="{{ url_for('groups.kick', group_id=g.id) }}" class="d-inline">
                    <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                    <input type="hidden" name="member_id" value="{{ m.id }}">
                    <button class="btn btn-sm btn-outline-danger">kick</button>
                </form>
            {% endif %}
        </li>
    {% endfor %}
    </ul>
</div>

{% if current_user.is_authenticated %}
<div class="d-flex gap-2 flex-wrap">
    {% if not is_member %}
        {% if g.is_open %}
        <form method="post" action="{{ url_for('groups.join', group_id=g.id) }}" class="d-inline">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <button class="btn btn-primary"><i class="bi bi-box-arrow-in-right"></i> Join</button>
        </form>
        {% endif %}
    {% else %}
        {% if current_user.id != g.leader_id %}
        <form method="post" action="{{ url_for('groups.leave', group_id=g.id) }}" class="d-inline">
            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
            <button class="btn btn-warning"><i class="bi bi-box-arrow-right"></i> Leave</button>
        </form>
        {% endif %}
    {% endif %}

    {% if current_user.id == g.leader_id or current_user.is_admin %}
    <form method="post" action="{{ url_for('groups.invite', group_id=g.id) }}" class="d-inline-flex gap-1">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <input type="text" name="username" class="form-control form-control-sm" placeholder="Username to invite">
        <button class="btn btn-sm btn-success">Invite</button>
    </form>
    <form method="post" action="{{ url_for('groups.delete', group_id=g.id) }}" class="d-inline"
          onsubmit="return confirm('Delete this group?');">
        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
        <button class="btn btn-sm btn-outline-danger">Delete group</button>
    </form>
    {% endif %}
</div>
{% endif %}

<div class="mt-3"><a href="{{ url_for('groups.index') }}">&larr; All groups</a></div>
{% endblock %}
