@php
    $post = $post ?? null;
    $thread = $thread ?? null;
    $isOp = (bool) ($isOp ?? false);
    $author = data_get($post, 'user');
    $postedAt = data_get($post, 'created_at');
    $editedAt = data_get($post, 'edited_at');
    $content = (string) data_get($post, 'content', '');
    $rendered = \App\Support\ForumPostContent::render($content);
@endphp

<article class="overflow-hidden rounded-xl border border-white/5 bg-slate-900/70 backdrop-blur" id="post-{{ data_get($post, 'id') }}">
    <header class="border-b border-white/10 px-4 py-3 sm:px-5">
        <div class="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
            <x-forum.thread.author-badge :user="$author" />
            <div class="text-xs text-zinc-400">
                @if ($postedAt)
                    <time datetime="{{ \Illuminate\Support\Carbon::parse($postedAt)->toIso8601String() }}">
                        {{ \Illuminate\Support\Carbon::parse($postedAt)->format('d.m.Y H:i') }}
                    </time>
                @endif
                @if ($isOp)
                    <span class="ml-2 rounded-full bg-cyan-500/15 px-2 py-0.5 text-[11px] font-medium text-cyan-300">OP</span>
                @endif
            </div>
        </div>
    </header>

    <div class="px-4 py-4 sm:px-5">
        <div class="prose prose-invert max-w-none text-sm leading-6 prose-pre:overflow-x-auto">
            {!! $rendered !!}
        </div>

        @if (data_get($post, 'is_edited') && $editedAt)
            <p class="mt-3 text-xs text-zinc-500">
                Edited <time datetime="{{ \Illuminate\Support\Carbon::parse($editedAt)->toIso8601String() }}">{{ \Illuminate\Support\Carbon::parse($editedAt)->diffForHumans() }}</time>
            </p>
        @endif

        <x-forum.thread.attachment-list :attachments="data_get($post, 'attachments', [])" />
    </div>

    <footer class="flex flex-wrap items-center gap-3 border-t border-white/10 px-4 py-3 text-xs text-zinc-400 sm:px-5">
        <button type="button" disabled aria-disabled="true" title="Like coming soon" class="cursor-not-allowed rounded border border-white/10 px-2 py-0.5 text-zinc-500">Like</button>
        @if (!empty(data_get($thread, 'id')))
            <a href="{{ route('forum.thread.show', ['thread' => data_get($thread, 'id'), 'slug' => data_get($thread, 'slug'), 'quote' => data_get($post, 'id')]) }}#reply-content" class="hover:text-zinc-200">Quote</a>
        @else
            <a href="#post-{{ data_get($post, 'id') }}" class="hover:text-zinc-200">Quote</a>
        @endif
        @auth
            @if ((int) data_get($post, 'user_id') !== (int) auth()->id())
                <form method="POST" action="{{ route('forum.post.report', ['post' => data_get($post, 'id')]) }}" class="inline">
                    @csrf
                    <button type="submit" class="hover:text-zinc-200">Report</button>
                </form>
            @endif
        @else
            <a href="{{ route('login') }}" class="hover:text-zinc-200">Report</a>
        @endauth

        @auth
            @if ((int) data_get($post, 'user_id') === (int) auth()->id() || Gate::allows('moderate-forum'))
                <a href="{{ route('forum.post.edit', ['post' => data_get($post, 'id')]) }}" class="hover:text-zinc-200">Edit</a>
            @endif
        @endauth

        @can('moderate-forum')
            <span class="ml-auto text-amber-300">Moderation tools available</span>
        @endcan
    </footer>
</article>
