{% extends "base/base.html" %}
{% load static %}

{% block title %}Search Parks - ThrillWiki{% endblock %}

{% block content %}
<div class="container mx-auto px-4 py-8">
    <!-- Top Filter Bar -->
    {% include "core/search/filters.html" %}
    
    <!-- Results Section -->
    <div id="search-results">
        <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700">
            <!-- Consolidated Search and View Controls -->
            <div class="p-6 border-b border-gray-200 dark:border-gray-700">
                <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
                    <!-- Left side: Search bar with results count -->
                    <div class="flex-1 max-w-md">
                        <div class="relative">
                            <input type="text"
                                   name="search"
                                   value="{{ request.GET.search|default:'' }}"
                                   placeholder="Search parks by name, location..."
                                   hx-get="{% url 'parks:park_list' %}"
                                   hx-target="#results-container"
                                   hx-trigger="input changed delay:500ms"
                                   hx-indicator="#loading-indicator"
                                   hx-swap="outerHTML"
                                   hx-push-url="true"
                                   class="w-full pl-10 pr-4 py-2.5 border border-gray-300 dark:border-gray-600 rounded-lg bg-white dark:bg-gray-700 text-gray-900 dark:text-white placeholder-gray-500 dark:placeholder-gray-400 focus:ring-2 focus:ring-blue-500 focus:border-blue-500 transition-colors">
                            <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                                <svg class="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
                                </svg>
                            </div>
                        </div>
                        <div class="mt-1 text-sm text-gray-500 dark:text-gray-400">
                            Parks ({{ results.count|default:0 }} found)
                        </div>
                    </div>
                    
                    <!-- Right side: View switching buttons -->
                    <div class="flex items-center gap-2">
                        <div class="flex bg-gray-100 dark:bg-gray-700 rounded-lg p-1" x-data="{ viewMode: '{{ request.GET.view_mode|default:'grid' }}' }">
                            <button type="button"
                                    @click="viewMode = 'grid'; switchView('grid')"
                                    :class="viewMode === 'grid' ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300'"
                                    class="flex items-center px-3 py-2 text-sm font-medium rounded-md transition-colors">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM14 6a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2V6zM4 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2v-2zM14 16a2 2 0 012-2h2a2 2 0 012 2v2a2 2 0 01-2 2h-2a2 2 0 01-2-2v-2z"/>
                                </svg>
                            </button>
                            <button type="button"
                                    @click="viewMode = 'list'; switchView('list')"
                                    :class="viewMode === 'list' ? 'bg-white dark:bg-gray-600 text-gray-900 dark:text-white shadow-sm' : 'text-gray-500 dark:text-gray-400 hover:text-gray-700 dark:hover:text-gray-300'"
                                    class="flex items-center px-3 py-2 text-sm font-medium rounded-md transition-colors">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 10h16M4 14h16M4 18h16"/>
                                </svg>
                            </button>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Results Container -->
            <div id="results-container" class="divide-y divide-gray-200 dark:divide-gray-700" x-data="{ viewMode: '{{ request.GET.view_mode|default:'grid' }}' }">
                <!-- Grid View -->
                <div x-show="viewMode === 'grid'" class="p-6">
                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
                        {% for park in results %}
                        <div class="bg-gray-50 dark:bg-gray-700 rounded-lg overflow-hidden hover:shadow-md transition-shadow">
                            <!-- Park Image -->
                            <div class="h-48 bg-gray-200 dark:bg-gray-600 overflow-hidden">
                                {% if park.photos.exists %}
                                <img src="{{ park.photos.first.image.url }}" 
                                     alt="{{ park.name }}"
                                     class="w-full h-full object-cover">
                                {% else %}
                                <div class="w-full h-full flex items-center justify-center text-gray-400 dark:text-gray-500">
                                    <svg class="w-12 h-12" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
                                    </svg>
                                </div>
                                {% endif %}
                            </div>

                            <!-- Park Details -->
                            <div class="p-4">
                                <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-2">
                                    <a href="{{ park.get_absolute_url }}" class="hover:text-blue-600 dark:hover:text-blue-400 transition-colors">
                                        {{ park.name }}
                                    </a>
                                </h3>
                                
                                {% if park.formatted_location %}
                                <div class="text-sm text-gray-600 dark:text-gray-400 flex items-center mb-3">
                                    <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/>
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/>
                                    </svg>
                                    {{ park.formatted_location }}
                                </div>
                                {% endif %}

                                <div class="flex flex-wrap gap-2 mb-3">
                                    {% if park.average_rating %}
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-300">
                                        <svg class="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20">
                                            <path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"/>
                                        </svg>
                                        {{ park.average_rating }}/10
                                    </span>
                                    {% endif %}
                                    
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ park.get_status_color }} dark:bg-opacity-30">
                                        {{ park.get_status_display }}
                                    </span>

                                    {% if park.ride_count %}
                                    <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-purple-100 text-purple-800 dark:bg-purple-900/30 dark:text-purple-300">
                                        {{ park.ride_count }} Ride{{ park.ride_count|pluralize }}
                                    </span>
                                    {% endif %}
                                </div>

                                {% if park.description %}
                                <p class="text-sm text-gray-600 dark:text-gray-400 line-clamp-2">
                                    {{ park.description|truncatewords:20 }}
                                </p>
                                {% endif %}
                            </div>
                        </div>
                        {% empty %}
                        <div class="col-span-full p-12 text-center text-gray-500 dark:text-gray-400">
                            <svg class="mx-auto h-12 w-12 text-gray-400 dark:text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
                            </svg>
                            <h3 class="mt-2 text-sm font-medium text-gray-900 dark:text-gray-100">No parks found</h3>
                            <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">Try adjusting your search criteria or filters.</p>
                        </div>
                        {% endfor %}
                    </div>
                </div>

                <!-- List View -->
                <div x-show="viewMode === 'list'" class="divide-y divide-gray-200 dark:divide-gray-700">
                    {% for park in results %}
                    <div class="p-6 flex flex-col md:flex-row gap-4 hover:bg-gray-50 dark:hover:bg-gray-700/50 transition-colors">
                        <!-- Park Image -->
                        <div class="md:w-48 h-32 bg-gray-200 dark:bg-gray-700 rounded-lg overflow-hidden flex-shrink-0">
                            {% if park.photos.exists %}
                            <img src="{{ park.photos.first.image.url }}" 
                                 alt="{{ park.name }}"
                                 class="w-full h-full object-cover">
                            {% else %}
                            <div class="w-full h-full flex items-center justify-center text-gray-400 dark:text-gray-500">
                                <svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z"/>
                                </svg>
                            </div>
                            {% endif %}
                        </div>

                        <!-- Park Details -->
                        <div class="flex-1 min-w-0">
                            <h3 class="text-lg font-semibold text-gray-900 dark:text-white">
                                <a href="{{ park.get_absolute_url }}" class="hover:text-blue-600 dark:hover:text-blue-400 transition-colors">
                                    {{ park.name }}
                                </a>
                            </h3>
                            
                            {% if park.formatted_location %}
                            <div class="mt-1 text-sm text-gray-600 dark:text-gray-400 flex items-center">
                                <svg class="w-4 h-4 mr-1" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17.657 16.657L13.414 20.9a1.998 1.998 0 01-2.827 0l-4.244-4.243a8 8 0 1111.314 0z"/>
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 11a3 3 0 11-6 0 3 3 0 016 0z"/>
                                </svg>
                                {{ park.formatted_location }}
                            </div>
                            {% endif %}

                            <div class="mt-3 flex flex-wrap gap-2">
                                {% if park.average_rating %}
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-green-100 text-green-800 dark:bg-green-900/30 dark:text-green-300">
                                    <svg class="w-3 h-3 mr-1" fill="currentColor" viewBox="0 0 20 20">
                                        <path d="M9.049 2.927c.3-.921 1.603-.921 1.902 0l1.07 3.292a1 1 0 00.95.69h3.462c.969 0 1.371 1.24.588 1.81l-2.8 2.034a1 1 0 00-.364 1.118l1.07 3.292c.3.921-.755 1.688-1.54 1.118l-2.8-2.034a1 1 0 00-1.175 0l-2.8 2.034c-.784.57-1.838-.197-1.539-1.118l1.07-3.292a1 1 0 00-.364-1.118L2.98 8.72c-.783-.57-.38-1.81.588-1.81h3.461a1 1 0 00.951-.69l1.07-3.292z"/>
                                    </svg>
                                    {{ park.average_rating }}/10
                                </span>
                                {% endif %}
                                
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium {{ park.get_status_color }} dark:bg-opacity-30">
                                    {{ park.get_status_display }}
                                </span>

                                {% if park.ride_count %}
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-purple-100 text-purple-800 dark:bg-purple-900/30 dark:text-purple-300">
                                    {{ park.ride_count }} Ride{{ park.ride_count|pluralize }}
                                </span>
                                {% endif %}

                                {% if park.coaster_count %}
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-yellow-100 text-yellow-800 dark:bg-yellow-900/30 dark:text-yellow-300">
                                    {{ park.coaster_count }} Coaster{{ park.coaster_count|pluralize }}
                                </span>
                                {% endif %}
                            </div>

                            {% if park.description %}
                            <p class="mt-2 text-sm text-gray-600 dark:text-gray-400 line-clamp-2">
                                {{ park.description|truncatewords:30 }}
                            </p>
                            {% endif %}
                            
                            {% if park.opening_date %}
                            <div class="mt-2 text-xs text-gray-500 dark:text-gray-500">
                                Opened: {{ park.opening_date|date:"Y" }}
                            </div>
                            {% endif %}
                        </div>
                    </div>
                    {% empty %}
                    <div class="p-12 text-center text-gray-500 dark:text-gray-400">
                        <svg class="mx-auto h-12 w-12 text-gray-400 dark:text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z"/>
                        </svg>
                        <h3 class="mt-2 text-sm font-medium text-gray-900 dark:text-gray-100">No parks found</h3>
                        <p class="mt-1 text-sm text-gray-500 dark:text-gray-400">Try adjusting your search criteria or filters.</p>
                    </div>
                    {% endfor %}
                </div>
            </div>
        </div>
    </div>
</div>

{# Include required scripts #}
<script src="https://unpkg.com/htmx.org@1.9.10"></script>
<script src="https://unpkg.com/alpinejs@3.x.x/dist/cdn.min.js" defer></script>
<script src="https://unpkg.com/unpoly@3/unpoly.min.js"></script>

<script>
// View switching functionality
function switchView(mode) {
    // Update URL parameter
    const url = new URL(window.location);
    url.searchParams.set('view_mode', mode);
    
    // Update the URL without reloading
    window.history.pushState({}, '', url);
    
    // Store preference in localStorage
    localStorage.setItem('parkViewMode', mode);
}

// Initialize view mode from URL or localStorage
document.addEventListener('DOMContentLoaded', function() {
    const urlParams = new URLSearchParams(window.location.search);
    const urlViewMode = urlParams.get('view_mode');
    const savedViewMode = localStorage.getItem('parkViewMode');
    const defaultViewMode = urlViewMode || savedViewMode || 'grid';
    
    // Set initial view mode
    if (!urlViewMode) {
        const url = new URL(window.location);
        url.searchParams.set('view_mode', defaultViewMode);
        window.history.replaceState({}, '', url);
    }
});

// Enhanced search functionality
document.addEventListener('DOMContentLoaded', function() {
    const searchInput = document.querySelector('input[name="search"]');
    if (searchInput) {
        let searchTimeout;
        
        // Preserve view mode in search requests
        searchInput.addEventListener('input', function(e) {
            clearTimeout(searchTimeout);
            searchTimeout = setTimeout(() => {
                // Get current view mode
                const currentViewMode = new URLSearchParams(window.location.search).get('view_mode') || 'grid';
                
                // Add view mode to the HTMX request
                const currentUrl = new URL(e.target.getAttribute('hx-get'), window.location.origin);
                currentUrl.searchParams.set('view_mode', currentViewMode);
                currentUrl.searchParams.set('search', e.target.value);
                
                // Update the hx-get attribute
                e.target.setAttribute('hx-get', currentUrl.pathname + currentUrl.search);
                
                // Trigger the HTMX request
                htmx.trigger(e.target, 'input');
            }, 500);
        });
    }
});
</script>
{% endblock %}
