@extends('layouts.admin')

@section('title', 'Inquiry Details #' . $inquiry->id)

@section('admin-content')
<nav aria-label="breadcrumb" class="mb-4">
    <ol class="breadcrumb">
        <li class="breadcrumb-item"><a href="{{ route('admin.dashboard') }}">Dashboard</a></li>
        <li class="breadcrumb-item"><a href="{{ route('admin.inquiries.index') }}">Inquiries</a></li>
        <li class="breadcrumb-item active">Inquiry #{{ $inquiry->id }}</li>
    </ol>
</nav>

<div class="row g-4">
    <!-- Left: Inquiry Details -->
    <div class="col-lg-8">
        <!-- Admin Status Card -->
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white py-3">
                <div class="d-flex justify-content-between align-items-center">
                    <h5 class="fw-bold mb-0">
                        <i class="bi bi-shield-check me-2 text-primary"></i>
                        Admin Review
                    </h5>
                    @if($inquiry->admin_status == 'pending')
                        <span class="badge bg-warning">Pending Review</span>
                    @elseif($inquiry->admin_status == 'assigned')
                        <span class="badge bg-success">Assigned</span>
                    @else
                        <span class="badge bg-danger">Rejected</span>
                    @endif
                </div>
            </div>
            <div class="card-body">
                @if($inquiry->admin_status == 'pending')
                    <div class="alert alert-warning">
                        <i class="bi bi-exclamation-triangle me-2"></i>
                        This inquiry is awaiting admin review.
                    </div>
                    
                    <div class="row g-2">
                        <div class="col-md-6">
                            <button type="button" class="btn btn-success w-100" data-bs-toggle="modal" data-bs-target="#assignModal">
                                <i class="bi bi-check-circle me-1"></i> Assign to Seller
                            </button>
                        </div>
                        <div class="col-md-6">
                            <button type="button" class="btn btn-danger w-100" data-bs-toggle="modal" data-bs-target="#rejectModal">
                                <i class="bi bi-x-circle me-1"></i> Reject Inquiry
                            </button>
                        </div>
                    </div>
                @else
                    <div class="tw-bg-gray-50 tw-rounded-lg tw-p-4">
                        <div class="mb-2">
                            <small class="text-muted">Assigned/Rejected by:</small>
                            <div class="fw-bold">{{ $inquiry->assignedBy->name ?? 'System' }}</div>
                        </div>
                        <div class="mb-2">
                            <small class="text-muted">Date:</small>
                            <div>{{ $inquiry->assigned_at?->format('d M Y, H:i') ?? 'N/A' }}</div>
                        </div>
                        @if($inquiry->admin_notes)
                            <div>
                                <small class="text-muted">Admin Notes:</small>
                                <div class="tw-bg-white tw-p-3 tw-rounded mt-1">{{ $inquiry->admin_notes }}</div>
                            </div>
                        @endif
                    </div>
                    
                    @if($inquiry->admin_status == 'assigned' && $inquiry->seller->canReceiveNotifications())
                        <div class="alert alert-info mt-3 mb-0">
                            <i class="bi bi-bell-fill me-2"></i>
                            Seller was notified about this assignment.
                        </div>
                    @elseif($inquiry->admin_status == 'assigned')
                        <div class="alert alert-secondary mt-3 mb-0">
                            <i class="bi bi-bell-slash me-2"></i>
                            Seller NOT notified (notifications disabled).
                        </div>
                    @endif
                @endif
            </div>
        </div>

        <!-- Inquiry Message -->
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white py-3 d-flex justify-content-between align-items-center">
                <h5 class="fw-bold mb-0">
                    <i class="bi bi-envelope-open me-2 text-primary"></i>
                    Inquiry Message
                </h5>
                @if($inquiry->status == 'new')
                    <span class="badge bg-info">New</span>
                @else
                    <span class="badge bg-secondary">Responded</span>
                @endif
            </div>
            <div class="card-body">
                <div class="tw-bg-gray-50 tw-rounded-lg tw-p-4 mb-4">
                    <p class="mb-0" style="white-space: pre-line;">{{ $inquiry->message }}</p>
                </div>

                @if($inquiry->product)
                    <div class="alert alert-info mb-0">
                        <h6 class="fw-bold mb-1"><i class="bi bi-box me-1"></i> Product Inquired</h6>
                        <p class="mb-0">
                            <a href="{{ route('public.products.show', $inquiry->product->slug) }}" target="_blank">
                                {{ $inquiry->product->name }}
                            </a>
                            - ₹{{ number_format($inquiry->product->price, 2) }}
                        </p>
                    </div>
                @endif

                @if($inquiry->seller_response)
                    <hr>
                    <h6 class="fw-bold mb-2"><i class="bi bi-reply me-1"></i> Seller Response</h6>
                    <div class="tw-bg-green-50 tw-rounded-lg tw-p-4">
                        <p class="mb-0">{{ $inquiry->seller_response }}</p>
                        <small class="text-muted">Responded on {{ $inquiry->responded_at?->format('d M Y H:i') }}</small>
                    </div>
                @endif
            </div>
        </div>

        <!-- Timeline -->
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white py-3">
                <h5 class="fw-bold mb-0">
                    <i class="bi bi-clock-history me-2 text-primary"></i>
                    Timeline
                </h5>
            </div>
            <div class="card-body">
                <div class="timeline">
                    <div class="d-flex mb-3">
                        <div class="tw-w-10 tw-h-10 tw-bg-blue-100 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-flex-shrink-0">
                            <i class="bi bi-send tw-text-blue-600"></i>
                        </div>
                        <div>
                            <div class="fw-bold">Inquiry Submitted</div>
                            <small class="text-muted">{{ $inquiry->created_at->format('d M Y, H:i') }}</small>
                        </div>
                    </div>

                    @if($inquiry->assigned_at)
                        <div class="d-flex mb-3">
                            <div class="tw-w-10 tw-h-10 {{ $inquiry->admin_status == 'assigned' ? 'tw-bg-success' : 'tw-bg-danger' }} tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-flex-shrink-0">
                                <i class="bi {{ $inquiry->admin_status == 'assigned' ? 'bi-check-lg' : 'bi-x-lg' }} tw-text-white"></i>
                            </div>
                            <div>
                                <div class="fw-bold">{{ $inquiry->admin_status == 'assigned' ? 'Assigned by Admin' : 'Rejected by Admin' }}</div>
                                <small class="text-muted">{{ $inquiry->assigned_at->format('d M Y, H:i') }} - {{ $inquiry->assignedBy->name ?? 'System' }}</small>
                            </div>
                        </div>
                    @endif

                    @if($inquiry->seller_viewed_at)
                        <div class="d-flex mb-3">
                            <div class="tw-w-10 tw-h-10 tw-bg-info tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-flex-shrink-0">
                                <i class="bi bi-eye tw-text-white"></i>
                            </div>
                            <div>
                                <div class="fw-bold">Viewed by Seller</div>
                                <small class="text-muted">{{ $inquiry->seller_viewed_at->format('d M Y, H:i') }}</small>
                            </div>
                        </div>
                    @endif

                    @if($inquiry->responded_at)
                        <div class="d-flex">
                            <div class="tw-w-10 tw-h-10 tw-bg-success tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-flex-shrink-0">
                                <i class="bi bi-check-lg tw-text-white"></i>
                            </div>
                            <div>
                                <div class="fw-bold text-success">Seller Responded</div>
                                <small class="text-muted">{{ $inquiry->responded_at->format('d M Y, H:i') }}</small>
                            </div>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>

    <!-- Right: Buyer & Seller Info -->
    <div class="col-lg-4">
        <!-- Buyer Info -->
        <div class="card border-0 shadow-sm mb-4">
            <div class="card-header bg-white py-3">
                <h6 class="fw-bold mb-0">
                    <i class="bi bi-person me-2 text-primary"></i>
                    Buyer Information
                </h6>
            </div>
            <div class="card-body">
                <div class="tw-bg-gray-50 tw-rounded-lg tw-p-3 mb-3">
                    <small class="text-muted d-block">Name</small>
                    <div class="fw-bold">{{ $inquiry->name }}</div>
                </div>
                <div class="tw-bg-gray-50 tw-rounded-lg tw-p-3 mb-3">
                    <small class="text-muted d-block">Email</small>
                    <div class="fw-bold">
                        <a href="mailto:{{ $inquiry->email }}">{{ $inquiry->email }}</a>
                    </div>
                </div>
                @if($inquiry->phone)
                    <div class="tw-bg-gray-50 tw-rounded-lg tw-p-3 mb-3">
                        <small class="text-muted d-block">Phone</small>
                        <div class="fw-bold">
                            <a href="tel:{{ $inquiry->phone }}">{{ $inquiry->phone }}</a>
                        </div>
                    </div>
                @endif
                @if($inquiry->buyer)
                    <div class="alert alert-success mb-0">
                        <i class="bi bi-check-circle-fill me-1"></i>
                        Registered Buyer
                    </div>
                @else
                    <div class="alert alert-secondary mb-0">
                        <i class="bi bi-person-x me-1"></i>
                        Guest Inquiry
                    </div>
                @endif
            </div>
        </div>

        <!-- Seller Info -->
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white py-3">
                <h6 class="fw-bold mb-0">
                    <i class="bi bi-shop me-2 text-success"></i>
                    Seller Information
                </h6>
            </div>
            <div class="card-body">
                @if($inquiry->seller)
                    <div class="d-flex align-items-center mb-3">
                        @if($inquiry->seller->logo)
                            <img src="{{ asset('storage/' . $inquiry->seller->logo) }}" class="rounded-circle me-3" style="width: 50px; height: 50px; object-fit: cover;">
                        @else
                            <div class="tw-w-12 tw-h-12 tw-bg-blue-100 tw-rounded-full tw-flex tw-items-center tw-justify-center me-3 tw-text-blue-600 tw-font-bold tw-text-lg">
                                {{ strtoupper(substr($inquiry->seller->company_name, 0, 1)) }}
                            </div>
                        @endif
                        <div>
                            <div class="fw-bold">{{ $inquiry->seller->company_name }}</div>
                            <small class="text-muted">{{ $inquiry->seller->city }}, {{ $inquiry->seller->state }}</small>
                        </div>
                    </div>

                    <div class="mb-3">
                        @if($inquiry->seller->hasActiveSubscription())
                            <span class="badge bg-success mb-2">
                                <i class="bi bi-star-fill"></i> Paid Seller
                            </span>
                            <small class="d-block text-muted">Auto-notified on new inquiries</small>
                        @else
                            <span class="badge bg-secondary mb-2">
                                <i class="bi bi-person"></i> Free Seller
                            </span>
                            <small class="d-block text-muted">
                                Notifications: 
                                @if($inquiry->seller->notification_enabled)
                                    <span class="text-success"><i class="bi bi-bell-fill"></i> Enabled</span>
                                @else
                                    <span class="text-danger"><i class="bi bi-bell-slash"></i> Disabled</span>
                                @endif
                            </small>
                        @endif
                    </div>

                    <a href="{{ route('admin.sellers') }}?search={{ $inquiry->seller->company_name }}" class="btn btn-outline-primary btn-sm w-100 mb-2">
                        <i class="bi bi-eye"></i> View Seller Profile
                    </a>

                    @if(!$inquiry->seller->hasActiveSubscription())
                        <button type="button" class="btn btn-outline-secondary btn-sm w-100" data-bs-toggle="modal" data-bs-target="#toggleNotificationModal">
                            <i class="bi bi-bell"></i> Toggle Notifications
                        </button>
                    @endif
                @else
                    <p class="text-muted mb-0">Seller data not available.</p>
                @endif
            </div>
        </div>
    </div>
</div>

<div class="mt-4">
    <a href="{{ route('admin.inquiries.index') }}" class="btn btn-outline-secondary">
        <i class="bi bi-arrow-left me-1"></i> Back to Inquiries
    </a>
</div>

<!-- Assign Modal -->
<div class="modal fade" id="assignModal" tabindex="-1">
    <div class="modal-dialog">
        <form action="{{ route('admin.inquiries.assign', $inquiry) }}" method="POST">
            @csrf
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title">Assign Inquiry to Seller</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <p>You are about to assign this inquiry to <strong>{{ $inquiry->seller->company_name }}</strong>.</p>
                    
                    @if($inquiry->seller->canReceiveNotifications())
                        <div class="alert alert-info">
                            <i class="bi bi-bell-fill"></i> Seller WILL be notified via email and in-app notification.
                        </div>
                    @else
                        <div class="alert alert-warning">
                            <i class="bi bi-bell-slash"></i> Seller WILL NOT be notified (notifications disabled).
                        </div>
                    @endif

                    <div class="mb-3">
                        <label class="form-label">Admin Notes (Optional)</label>
                        <textarea name="admin_notes" class="form-control" rows="3" placeholder="Add any notes for the seller..."></textarea>
                        <small class="text-muted">These notes will be visible to the seller if they receive a notification.</small>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-success">Assign to Seller</button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- Reject Modal -->
<div class="modal fade" id="rejectModal" tabindex="-1">
    <div class="modal-dialog">
        <form action="{{ route('admin.inquiries.reject', $inquiry) }}" method="POST">
            @csrf
            <div class="modal-content">
                <div class="modal-header bg-danger text-white">
                    <h5 class="modal-title">Reject Inquiry</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
                </div>
                <div class="modal-body">
                    <p>You are about to reject this inquiry. This action marks it as spam or irrelevant.</p>

                    <div class="mb-3">
                        <label class="form-label">Reason for Rejection <span class="text-danger">*</span></label>
                        <textarea name="admin_notes" class="form-control" rows="3" placeholder="Why is this inquiry being rejected?" required></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-light" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-danger">Reject Inquiry</button>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- Toggle Notification Modal (for free sellers) -->
<div class="modal fade" id="toggleNotificationModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">Toggle Seller Notifications</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <p>This feature allows admins to enable/disable notifications for free sellers.</p>
                <div class="alert alert-info">
                    <i class="bi bi-info-circle"></i> This will be implemented in the seller settings page.
                </div>
                <p class="mb-0">Visit <strong>Admin → Sellers</strong> to manage notification settings.</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
            </div>
        </div>
    </div>
</div>
@endsection
