@extends('layouts.buyer')

@section('title', 'My Favorites')

@section('buyer-content')
    <!-- Page Header -->
    <div class="page-header">
        <div>
            <h3 class="page-title">My Favorites</h3>
            <p class="text-muted mb-0">Products you've saved for later</p>
        </div>
        <span class="badge bg-primary-subtle text-primary rounded-pill px-3 py-2">
            <i class="bi bi-heart-fill me-1"></i> {{ $favorites->total() }} Items
        </span>
    </div>

    @if($favorites->count() > 0)
        <!-- Favorites Grid -->
        <div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-4">
            @foreach($favorites as $favorite)
                <div class="col">
                    <div class="card h-100 border-0 shadow-sm rounded-4 overflow-hidden group-hover-lift">
                        <div class="position-relative">
                            @if(!empty($favorite->product->images_json) && isset($favorite->product->images_json[0]['thumbnail']))
                                <img src="{{ asset('storage/' . $favorite->product->images_json[0]['thumbnail']) }}" 
                                     class="card-img-top" 
                                     alt="{{ $favorite->product->name }}" 
                                     style="height: 220px; object-fit: cover;">
                            @else
                                <div class="bg-light card-img-top d-flex align-items-center justify-content-center" style="height: 220px;">
                                    <i class="bi bi-image fs-1 text-muted opacity-50"></i>
                                </div>
                            @endif
                            
                            <!-- Favorite/Remove Badge -->
                            <div class="position-absolute top-0 end-0 m-3 z-2">
                                <form action="{{ route('buyer.favorites.remove', $favorite->id) }}" method="POST" class="d-inline">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-light shadow-sm rounded-circle d-flex align-items-center justify-content-center p-0" 
                                            style="width: 36px; height: 36px;" title="Remove from favorites" data-bs-toggle="tooltip">
                                        <i class="bi bi-heart-fill text-danger"></i>
                                    </button>
                                </form>
                            </div>

                            <!-- Seller Badge -->
                            @if($favorite->product->seller)
                                <div class="position-absolute bottom-0 start-0 m-3 z-2">
                                    <span class="badge bg-dark bg-opacity-75 text-white shadow-sm backdrop-blur fw-normal border border-white border-opacity-25 px-3 py-2">
                                        <i class="bi bi-shop me-1"></i>{{ Str::limit($favorite->product->seller->company_name, 20) }}
                                    </span>
                                </div>
                            @endif
                            
                            <!-- Overlay Gradient -->
                            <div class="position-absolute bottom-0 start-0 w-100 h-25" style="background: linear-gradient(to top, rgba(0,0,0,0.3), transparent);"></div>
                        </div>
                        
                        <div class="card-body p-4">
                            <h5 class="card-title fw-bold text-truncate mb-2">
                                <a href="{{ route('public.products.show', $favorite->product->slug ?? 'slug') }}" class="text-decoration-none text-dark stretched-link">
                                    {{ $favorite->product->name }}
                                </a>
                            </h5>
                            
                            <div class="d-flex justify-content-between align-items-center mb-4">
                                <div>
                                    <span class="fw-bold text-success fs-5">
                                        ₹{{ number_format($favorite->product->price, 0) }}
                                    </span>
                                    <small class="text-muted ms-1">/ unit</small>
                                </div>
                                @if($favorite->product->moq)
                                    <span class="badge bg-light text-dark border shadow-sm fw-medium px-2 py-1">
                                        MOQ: {{ $favorite->product->moq }}
                                    </span>
                                @endif
                            </div>
                            
                            <div class="d-grid gap-2 position-relative z-2">
                                <a href="{{ route('public.products.show', $favorite->product->slug ?? 'slug') }}#inquiry" 
                                   class="btn btn-primary rounded-pill shadow-sm fw-medium">
                                    <i class="bi bi-envelope me-1"></i> Send Inquiry
                                </a>
                            </div>
                        </div>
                        
                        <div class="card-footer bg-white border-top border-light py-3 px-4">
                            <small class="text-muted d-flex align-items-center">
                                <i class="bi bi-clock me-2 text-primary"></i> Added {{ $favorite->created_at->diffForHumans() }}
                            </small>
                        </div>
                    </div>
                </div>
            @endforeach
        </div>

        <!-- Pagination -->
        @if($favorites->hasPages())
            <div class="d-flex justify-content-center mt-4">
                {{ $favorites->links() }}
            </div>
        @endif
    @else
        <!-- Empty State -->
        <div class="card border-0 shadow-sm rounded-4 overflow-hidden">
            <div class="card-body text-center py-5">
                <div class="rounded-circle bg-danger-subtle d-inline-flex align-items-center justify-content-center mb-4" style="width: 80px; height: 80px;">
                    <i class="bi bi-heart fs-1 text-danger"></i>
                </div>
                <h5 class="fw-bold mb-2">No Favorites Yet</h5>
                <p class="text-muted mb-4 opacity-75">Start browsing products and save your favorites for quick access.</p>
                <a href="{{ route('public.products.index') }}" class="btn btn-primary rounded-pill px-4 shadow-sm hover-lift">
                    <i class="bi bi-search me-2"></i> Browse Products
                </a>
            </div>
        </div>
    @endif
@endsection
