@extends('layouts.admin')

@section('title', 'Conversation')
@section('page-title', 'Conversation: ' . ($conversation->subject ?: 'No Subject'))

@section('content')
<div class="grid grid-cols-3 gap-6">
    <!-- Conversation Messages -->
    <div class="col-span-2">
        <div class="bg-white rounded-xl shadow-sm p-6">
            <!-- Header -->
            <div class="flex justify-between items-start mb-6 pb-4 border-b">
                <div>
                    <h2 class="text-lg font-bold text-gray-800">{{ $conversation->subject ?: 'No Subject' }}</h2>
                    <p class="text-sm text-gray-500 mt-1">
                        Started {{ $conversation->created_at->format('M d, Y h:i A') }}
                    </p>
                </div>
                <div class="flex gap-2">
                    <form action="{{ route('admin.conversations.update-status', $conversation) }}" method="POST">
                        @csrf
                        @method('PATCH')
                        <select name="status" onchange="this.form.submit()" class="border border-gray-300 rounded-lg px-3 py-1 text-sm">
                            <option value="open" {{ $conversation->status === 'open' ? 'selected' : '' }}>Open</option>
                            <option value="pending" {{ $conversation->status === 'pending' ? 'selected' : '' }}>Pending</option>
                            <option value="closed" {{ $conversation->status === 'closed' ? 'selected' : '' }}>Closed</option>
                        </select>
                    </form>
                </div>
            </div>

            <!-- Messages -->
            <div class="space-y-4 mb-6 max-h-[500px] overflow-y-auto">
                @foreach($conversation->messages as $message)
                <div class="flex gap-3 {{ $message->sender_type === 'App\Models\User' && $message->sender->isAdmin() ? '' : 'flex-row-reverse' }}">
                    <div class="w-8 h-8 rounded-full flex items-center justify-center text-sm font-bold flex-shrink-0
                        {{ $message->sender_type === 'App\Models\User' && $message->sender->isAdmin() ? 'bg-red-100 text-red-600' : 'bg-blue-100 text-blue-600' }}">
                        {{ strtoupper(substr($message->sender->name, 0, 1)) }}
                    </div>
                    
                    <div class="flex-1 {{ $message->sender_type === 'App\Models\User' && $message->sender->isAdmin() ? '' : 'text-right' }}">
                        <div class="inline-block bg-gray-100 rounded-lg px-4 py-2 text-left">
                            <p class="text-xs text-gray-500 mb-1">
                                {{ $message->sender->name }}
                                @if($message->sender_type === 'App\Models\User' && $message->sender->isAdmin())
                                    <span class="text-red-600">(Admin)</span>
                                @endif
                            </p>
                            <p class="text-gray-800">{{ $message->content }}</p>
                            
                            @if($message->attachments)
                                <div class="mt-2 space-y-1">
                                    @foreach($message->attachments as $attachment)
                                        <a href="{{ asset('storage/' . $attachment) }}" target="_blank" 
                                           class="text-blue-600 text-sm hover:underline block">
                                            📎 {{ basename($attachment) }}
                                        </a>
                                    @endforeach
                                </div>
                            @endif
                        </div>
                        <p class="text-xs text-gray-400 mt-1">{{ $message->created_at->format('M d, h:i A') }}</p>
                    </div>
                </div>
                @endforeach
            </div>

            <!-- Reply Form -->
            @if($conversation->status !== 'closed')
            <form action="{{ route('admin.conversations.reply', $conversation) }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="border-t pt-4">
                    <textarea name="content" rows="3" required
                        class="w-full border border-gray-300 rounded-lg px-3 py-2"
                        placeholder="Type your reply..."></textarea>
                    
                    <div class="flex justify-between items-center mt-3">
                        <input type="file" name="attachments[]" multiple 
                            class="text-sm text-gray-600"
                            accept=".jpg,.jpeg,.png,.pdf,.doc,.docx">
                        
                        <button type="submit" class="bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700">
                            Send Reply
                        </button>
                    </div>
                </div>
            </form>
            @else
            <div class="border-t pt-4 text-center text-gray-500">
                This conversation is closed.
            </div>
            @endif
        </div>
    </div>

    <!-- Sidebar Info -->
    <div class="space-y-6">
        <!-- User Info -->
        <div class="bg-white rounded-xl shadow-sm p-4">
            <h3 class="font-bold text-gray-800 mb-3">Customer</h3>
            <div class="flex items-center gap-3">
                <div class="w-10 h-10 bg-gray-200 rounded-full flex items-center justify-center font-bold">
                    {{ strtoupper(substr($conversation->user->name, 0, 1)) }}
                </div>
                <div>
                    <p class="font-medium">{{ $conversation->user->name }}</p>
                    <p class="text-sm text-gray-500">{{ $conversation->user->email }}</p>
                </div>
            </div>
        </div>

        @if($conversation->vendor)
        <div class="bg-white rounded-xl shadow-sm p-4">
            <h3 class="font-bold text-gray-800 mb-3">Related Vendor</h3>
            <p class="font-medium">{{ $conversation->vendor->shop_name }}</p>
            <a href="{{ route('admin.vendors.show', $conversation->vendor) }}" class="text-blue-600 text-sm hover:underline">View Vendor →</a>
        </div>
        @endif

        @if($conversation->order)
        <div class="bg-white rounded-xl shadow-sm p-4">
            <h3 class="font-bold text-gray-800 mb-3">Related Order</h3>
            <p class="font-medium">Order #{{ $conversation->order->order_number }}</p>
            <a href="{{ route('admin.orders.show', $conversation->order) }}" class="text-blue-600 text-sm hover:underline">View Order →</a>
        </div>
        @endif

        <div class="bg-white rounded-xl shadow-sm p-4">
            <h3 class="font-bold text-gray-800 mb-3">Actions</h3>
            
            <form action="{{ route('admin.conversations.destroy', $conversation) }}" method="POST"
                  onsubmit="return confirm('Delete this conversation?')">
                @csrf
                @method('DELETE')
                <button type="submit" class="text-red-600 text-sm hover:underline">Delete Conversation</button>
            </form>
        </div>
    </div>
</div>
@endsection