<x-layouts.app :title="'#' . $ticket->id . ' — ' . $ticket->subject">
    <div class="space-y-6">

        {{-- Header --}}
        <div class="flex items-center gap-3">
            <a href="{{ route('support.index') }}" class="p-1.5 text-muted/60 hover:text-ink rounded-lg hover:bg-surface transition-colors">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/>
                </svg>
            </a>
            <div class="min-w-0">
                <h1 class="text-xl font-bold text-ink truncate">{{ $ticket->subject }}</h1>
                <p class="mt-0.5 text-xs text-muted">{{ __('Ticket') }} #{{ $ticket->id }} &middot; {{ __('Opened') }} {{ $ticket->created_at?->diffForHumans() }}</p>
            </div>
        </div>

        @if(session('success'))
            <div class="p-3 rounded-xl bg-success/10 border border-success/20 text-sm text-success" role="status">{{ session('success') }}</div>
        @endif
        @if(session('error'))
            <div class="p-3 rounded-xl bg-danger/10 border border-danger/20 text-sm text-danger" role="alert">{{ session('error') }}</div>
        @endif

        <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">

            {{-- Thread --}}
            <div class="lg:col-span-2 space-y-4">

                {{-- Original message --}}
                <div class="bg-surface-2 rounded-2xl border border-border p-5">
                    <div class="flex items-center gap-3 mb-3">
                        <div class="w-9 h-9 bg-brand/15 text-brand rounded-full flex items-center justify-center text-xs font-bold shrink-0">
                            {{ strtoupper(substr(auth()->user()->name, 0, 2)) }}
                        </div>
                        <div class="flex-1 min-w-0">
                            <p class="text-sm font-semibold text-ink">{{ auth()->user()->name }}</p>
                            <p class="text-[10px] text-muted">{{ $ticket->created_at->format('M j, Y \a\t g:i A') }}</p>
                        </div>
                        <span class="px-2 py-0.5 text-[10px] font-bold bg-brand/10 text-brand rounded-full shrink-0">{{ __('You') }}</span>
                    </div>
                    <p class="text-sm text-ink leading-relaxed whitespace-pre-wrap">{{ $ticket->body }}</p>
                </div>

                {{-- Reply thread --}}
                @foreach($ticket->ticketReplies as $reply)
                <div class="bg-surface-2 rounded-2xl border {{ $reply->is_admin_reply ? 'border-brand/30' : 'border-border' }} p-5">
                    <div class="flex items-center gap-3 mb-3">
                        @if($reply->is_admin_reply)
                            <div class="w-9 h-9 bg-brand/15 text-brand rounded-full flex items-center justify-center text-xs font-bold shrink-0">
                                {{ strtoupper(substr($reply->user?->name ?? 'SU', 0, 2)) }}
                            </div>
                            <div class="flex-1 min-w-0">
                                <p class="text-sm font-semibold text-ink">{{ $reply->user?->name ?? __('Support Team') }}</p>
                                <p class="text-[10px] text-muted">{{ $reply->created_at->format('M j, Y \a\t g:i A') }}</p>
                            </div>
                            <span class="px-2 py-0.5 text-[10px] font-bold bg-brand/10 text-brand rounded-full shrink-0">{{ __('Support') }}</span>
                        @else
                            <div class="w-9 h-9 bg-info/10 text-info rounded-full flex items-center justify-center text-xs font-bold shrink-0">
                                {{ strtoupper(substr($reply->user?->name ?? auth()->user()->name, 0, 2)) }}
                            </div>
                            <div class="flex-1 min-w-0">
                                <p class="text-sm font-semibold text-ink">{{ $reply->user?->name ?? auth()->user()->name }}</p>
                                <p class="text-[10px] text-muted">{{ $reply->created_at->format('M j, Y \a\t g:i A') }}</p>
                            </div>
                            <span class="px-2 py-0.5 text-[10px] font-bold bg-info/10 text-info rounded-full shrink-0">{{ __('You') }}</span>
                        @endif
                    </div>
                    <p class="text-sm text-ink leading-relaxed whitespace-pre-wrap">{{ $reply->body }}</p>
                </div>
                @endforeach

                {{-- No replies yet --}}
                @if($ticket->ticketReplies->isEmpty() && !in_array($ticket->status, ['closed', 'resolved'], true))
                    <div class="text-center text-sm text-muted py-2">{{ __('Waiting for our team to reply...') }}</div>
                @endif

                {{-- Reply form / closed notice --}}
                @if(in_array($ticket->status, ['closed', 'resolved'], true))
                    <div class="bg-surface-2 rounded-2xl border border-border p-6 text-center">
                        <div class="w-10 h-10 bg-muted/10 rounded-xl flex items-center justify-center mx-auto mb-3">
                            <svg class="w-5 h-5 text-muted" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z"/>
                            </svg>
                        </div>
                        <p class="text-sm font-semibold text-ink">{{ __('This ticket is closed') }}</p>
                        <p class="mt-1 text-xs text-muted">{{ __("Need more help? Open a new ticket and we'll jump in.") }}</p>
                        <a href="{{ route('support.create') }}" class="btn-primary inline-flex mt-4">{{ __('New Ticket') }}</a>
                    </div>
                @else
                    <form method="POST" action="{{ route('support.reply', $ticket) }}"
                          x-data="{ submitting: false }" @submit="submitting = true"
                          class="bg-surface-2 rounded-2xl border border-border p-5 space-y-3">
                        @csrf
                        <label for="body" class="block text-sm font-semibold text-ink">{{ __('Add a reply') }}</label>
                        <textarea id="body" name="body" required rows="5" maxlength="10000"
                                  class="input @error('body') !border-danger !ring-danger/20 @enderror"
                                  placeholder="{{ __('Type your message here...') }}"></textarea>
                        @error('body') <p class="text-xs text-danger">{{ $message }}</p> @enderror
                        <div class="flex justify-end">
                            <button type="submit" :disabled="submitting"
                                    class="btn-primary inline-flex items-center gap-2"
                                    :class="submitting && 'opacity-75 cursor-not-allowed'">
                                <span x-show="!submitting">{{ __('Send Reply') }}</span>
                                <span x-show="submitting" x-cloak class="inline-flex items-center gap-2">
                                    <svg class="w-4 h-4 animate-spin" fill="none" viewBox="0 0 24 24">
                                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"/>
                                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"/>
                                    </svg>
                                    {{ __('Sending...') }}
                                </span>
                            </button>
                        </div>
                    </form>
                @endif

            </div>

            {{-- Sidebar --}}
            <div class="space-y-4">
                @php
                    $statusMap = [
                        'open'        => ['bg-info/10 text-info border-info/20',         'Open'],
                        'in_progress' => ['bg-warning/10 text-warning border-warning/20', 'In Progress'],
                        'waiting'     => ['bg-warning/10 text-warning border-warning/20', 'Waiting'],
                        'resolved'    => ['bg-success/10 text-success border-success/20', 'Resolved'],
                        'closed'      => ['bg-muted/10 text-muted border-border',         'Closed'],
                    ];
                    [$sCls, $sLabel] = $statusMap[$ticket->status] ?? ['bg-muted/10 text-muted border-border', ucfirst($ticket->status)];
                @endphp

                <div class="bg-surface-2 rounded-2xl border border-border p-5 space-y-4">
                    <h3 class="text-xs font-bold text-muted uppercase tracking-wider">{{ __('Ticket Info') }}</h3>
                    <div class="space-y-3 text-sm">
                        <div class="flex justify-between items-center">
                            <span class="text-muted">{{ __('Status') }}</span>
                            <span class="inline-flex px-2 py-0.5 text-[10px] font-bold uppercase tracking-wide rounded-full border {{ $sCls }}">{{ __($sLabel) }}</span>
                        </div>
                        <div class="flex justify-between items-center">
                            <span class="text-muted">{{ __('Priority') }}</span>
                            <span class="font-medium text-ink capitalize">{{ $ticket->priority ?? 'Normal' }}</span>
                        </div>
                        @if($ticket->category)
                        <div class="flex justify-between items-center">
                            <span class="text-muted">{{ __('Category') }}</span>
                            <span class="font-medium text-ink capitalize">{{ $ticket->category }}</span>
                        </div>
                        @endif
                        <div class="flex justify-between items-center">
                            <span class="text-muted">{{ __('Replies') }}</span>
                            <span class="font-medium text-ink">{{ $ticket->ticketReplies->count() }}</span>
                        </div>
                        <div class="flex justify-between items-center">
                            <span class="text-muted">{{ __('Opened') }}</span>
                            <span class="text-ink/80 text-xs">{{ $ticket->created_at->format('M j, Y') }}</span>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>
</x-layouts.app>
