@extends('layouts.app')

@php
    $displayName = $community->actor->displayName();
    $handle = '!'.$community->slug.'@'.config('openbook.domain');
@endphp

@section('title', $displayName.' - '.config('app.name'))

@section('content')
    <article class="ob-card" style="padding:0;overflow:hidden">
        <div class="ob-profile-cover" @if ($community->actor->coverUrl()) style="background-image:url('{{ $community->actor->coverUrl() }}');background-size:cover;background-position:center" @endif></div>
        <div class="ob-profile-header">
            <div class="ob-profile-avatar" aria-hidden="true">
                @if ($community->actor->avatarUrl())
                    <img src="{{ $community->actor->avatarUrl() }}" alt="">
                @else
                    {{ mb_strtoupper(mb_substr($displayName, 0, 1)) }}
                @endif
            </div>

            <h1 class="ob-profile-name">{{ $displayName }}</h1>
            <p class="ob-profile-handle">{{ $handle }}</p>

            @if ($community->is_private)
                <span class="ob-badge">{{ __('openbook.communities.private_badge') }}</span>
            @endif

            @if (filled($community->actor->summary))
                <div class="ob-profile-bio">{{ \App\Domain\Posts\PostBodyRenderer::render($community->actor->summary) }}</div>
            @endif

            <div class="ob-profile-stats">
                @if ($canViewMembers ?? false)
                    <a href="{{ route('communities.members', $community) }}"><strong>{{ $community->members_count }}</strong><span>{{ __('openbook.communities.members') }}</span></a>
                @else
                    <div><strong>{{ $community->members_count }}</strong><span>{{ __('openbook.communities.members') }}</span></div>
                @endif
                <div><strong>{{ $community->posts_count }}</strong><span>{{ __('openbook.communities.posts') }}</span></div>
            </div>

            <p class="ob-field__help">
                {{ __('openbook.communities.owned_by') }}
                <a href="{{ route('profile.show', $community->owner->username) }}">{{ $community->owner->profile?->display_name ?: $community->owner->username }}</a>
            </p>

            @auth
                <div style="margin-top:1rem;display:flex;gap:0.5rem;flex-wrap:wrap">
                    @if ($canUpdate ?? false)
                        <a href="{{ route('communities.edit', $community) }}" class="ob-btn ob-btn--ghost">{{ __('openbook.communities.edit') }}</a>
                        <span class="ob-field__help" style="align-self:center">{{ __('openbook.communities.you_are_owner') }}</span>
                    @elseif ($isMember)
                        <form method="POST" action="{{ route('communities.leave', $community) }}">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="ob-btn ob-btn--ghost">{{ __('openbook.communities.leave') }}</button>
                        </form>
                    @elseif ($hasPendingRequest)
                        <span class="ob-field__help">{{ __('openbook.communities.pending') }}</span>
                    @else
                        <form method="POST" action="{{ route('communities.join', $community) }}">
                            @csrf
                            <button type="submit" class="ob-btn ob-btn--primary">
                                {{ $community->is_private ? __('openbook.communities.request_join') : __('openbook.communities.join') }}
                            </button>
                        </form>
                    @endif
                </div>
            @else
                @if ($community->is_private)
                    <p class="ob-field__help" style="margin-top:1rem">
                        {{ __('openbook.communities.private_login_prompt') }}
                        <a href="{{ route('login') }}">{{ __('openbook.nav.login') }}</a>
                    </p>
                @endif
            @endauth
        </div>
    </article>

    @if ($canManageModerators ?? false)
        @php
            $moderatorsPanelOpen = $errors->has('username');
        @endphp
        <details class="ob-card ob-community-manage" @if ($moderatorsPanelOpen) open @endif>
            <summary class="ob-community-manage__summary">
                <span>{{ __('openbook.communities.moderators_menu') }}</span>
                <x-icon name="chevron-down" />
            </summary>
            <div class="ob-community-manage__body">
                <p class="ob-field__help">{{ __('openbook.communities.moderators_help') }}</p>

                @if ($community->moderators->isNotEmpty())
                    <ul class="ob-community-list">
                        @foreach ($community->moderators as $moderator)
                            <li class="ob-community-list__item">
                                <a href="{{ route('profile.show', $moderator->username) }}" class="ob-mini-profile__link">
                                    <x-avatar :user="$moderator" style="width:40px;height:40px" />
                                    <div>
                                        <div class="ob-post__author">{{ $moderator->profile?->display_name ?: $moderator->username }}</div>
                                        <div class="ob-post__handle">{{ '@'.$moderator->username }}</div>
                                    </div>
                                </a>
                                <form method="POST" action="{{ route('communities.moderators.destroy', [$community, $moderator]) }}">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="ob-btn ob-btn--ghost ob-btn--small">{{ __('openbook.communities.remove_moderator') }}</button>
                                </form>
                            </li>
                        @endforeach
                    </ul>
                @endif

                <form method="POST" action="{{ route('communities.moderators.store', $community) }}" style="margin-top:1rem">
                    @csrf
                    <div class="ob-field">
                        <label for="moderator-username">{{ __('openbook.communities.add_moderator') }}</label>
                        <input type="text" id="moderator-username" name="username" required maxlength="32" placeholder="username" value="{{ old('username') }}">
                        @error('username') <p class="ob-field__error">{{ $message }}</p> @enderror
                    </div>
                    <button type="submit" class="ob-btn ob-btn--primary ob-btn--small">{{ __('openbook.communities.add_moderator_submit') }}</button>
                </form>
            </div>
        </details>
    @endif

    @if ($pendingJoinRequests->isNotEmpty())
        <div class="ob-card">
            <h2>{{ __('openbook.communities.pending_requests') }}</h2>
            @foreach ($pendingJoinRequests as $request)
                <div class="ob-suggestion">
                    <a href="{{ $request->follower->profileUrl() }}" class="ob-mini-profile__link">
                        <x-avatar :actor="$request->follower" style="width:40px;height:40px" />
                        <div>
                            <div class="ob-post__author">{{ $request->follower->displayName() }}</div>
                            <div class="ob-post__handle">{{ '@'.$request->follower->preferred_username }}</div>
                        </div>
                    </a>
                    <div style="display:flex;gap:0.35rem">
                        <form method="POST" action="{{ route('communities.accept', [$community, $request]) }}">
                            @csrf
                            <button type="submit" class="ob-btn ob-btn--primary ob-btn--small">{{ __('openbook.follow.accept') }}</button>
                        </form>
                        <form method="POST" action="{{ route('communities.reject', [$community, $request]) }}">
                            @csrf
                            <button type="submit" class="ob-btn ob-btn--ghost ob-btn--small">{{ __('openbook.follow.reject') }}</button>
                        </form>
                    </div>
                </div>
            @endforeach
        </div>
    @endif

    @if ($canPost ?? false)
        @include('posts._composer', [
            'composerCommunities' => collect([$community]),
            'selectedCommunityId' => $community->id,
        ])
    @endif

    @if ($canViewWall ?? true)
        @include('posts._feed', [
            'posts' => $posts,
            'emptyMessage' => __('openbook.communities.wall_empty'),
        ])
    @else
        <div class="ob-card">
            <p class="ob-field__help">{{ __('openbook.communities.private_wall_locked') }}</p>
        </div>
    @endif
@endsection
