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

{% block title %}Rides - ThrillWiki{% endblock %}

{% block content %}
<div class="container px-4 mx-auto">
    <div class="flex flex-col items-start justify-between gap-4 mb-6 md:flex-row md:items-center">
        <h1 class="text-3xl font-bold text-gray-900 dark:text-white">Rides</h1>
        {% if user.is_authenticated %}
            <a href="{% url 'rides:ride_create' %}" class="btn-primary">
                <i class="mr-2 fas fa-plus"></i>Add Ride
            </a>
        {% endif %}
    </div>

    <!-- Filters -->
    <div class="p-4 mb-6 bg-white rounded-lg shadow dark:bg-gray-800">
        <form method="get" class="grid grid-cols-1 gap-4 md:grid-cols-4">
            <div>
                <label for="search" class="block mb-1 text-sm font-medium text-gray-700 dark:text-gray-300">Search</label>
                <input type="text" name="search" id="search" 
                       value="{{ current_filters.search }}"
                       class="w-full border-gray-300 rounded-lg form-input dark:border-gray-600 dark:bg-gray-700 dark:text-white"
                       placeholder="Search rides...">
            </div>
            <div>
                <label for="category" class="block mb-1 text-sm font-medium text-gray-700 dark:text-gray-300">Category</label>
                <select name="category" id="category" 
                        class="w-full border-gray-300 rounded-lg form-select dark:border-gray-600 dark:bg-gray-700 dark:text-white">
                    <option value="">All Categories</option>
                    <option value="RC" {% if current_filters.category == 'RC' %}selected{% endif %}>Roller Coaster</option>
                    <option value="DR" {% if current_filters.category == 'DR' %}selected{% endif %}>Dark Ride</option>
                    <option value="FR" {% if current_filters.category == 'FR' %}selected{% endif %}>Flat Ride</option>
                    <option value="WR" {% if current_filters.category == 'WR' %}selected{% endif %}>Water Ride</option>
                    <option value="TR" {% if current_filters.category == 'TR' %}selected{% endif %}>Transport</option>
                    <option value="OT" {% if current_filters.category == 'OT' %}selected{% endif %}>Other</option>
                </select>
            </div>
            <div>
                <label for="status" class="block mb-1 text-sm font-medium text-gray-700 dark:text-gray-300">Status</label>
                <select name="status" id="status" 
                        class="w-full border-gray-300 rounded-lg form-select dark:border-gray-600 dark:bg-gray-700 dark:text-white">
                    <option value="">All Statuses</option>
                    <option value="OPERATING" {% if current_filters.status == 'OPERATING' %}selected{% endif %}>Operating</option>
                    <option value="CLOSED_TEMP" {% if current_filters.status == 'CLOSED_TEMP' %}selected{% endif %}>Temporarily Closed</option>
                    <option value="CLOSED_PERM" {% if current_filters.status == 'CLOSED_PERM' %}selected{% endif %}>Permanently Closed</option>
                    <option value="UNDER_CONSTRUCTION" {% if current_filters.status == 'UNDER_CONSTRUCTION' %}selected{% endif %}>Under Construction</option>
                    <option value="DEMOLISHED" {% if current_filters.status == 'DEMOLISHED' %}selected{% endif %}>Demolished</option>
                    <option value="RELOCATED" {% if current_filters.status == 'RELOCATED' %}selected{% endif %}>Relocated</option>
                </select>
            </div>
            <div class="flex items-end">
                <button type="submit" class="w-full btn-primary">
                    <i class="mr-2 fas fa-filter"></i>Filter
                </button>
            </div>
        </form>
    </div>

    <!-- Rides Grid -->
    <div class="grid grid-cols-1 gap-6 md:grid-cols-2 lg:grid-cols-3">
        {% for ride in rides %}
            <div class="overflow-hidden transition-transform transform bg-white rounded-lg shadow-lg dark:bg-gray-800 hover:-translate-y-1">
                {% if ride.photos.exists %}
                    <div class="aspect-w-16 aspect-h-9">
                        <img src="{{ ride.photos.first.image.url }}" 
                             alt="{{ ride.name }}"
                             class="object-cover w-full">
                    </div>
                {% endif %}
                <div class="p-4">
                    <h2 class="mb-2 text-xl font-bold">
                        <a href="{% url 'rides:ride_detail' ride.park.slug ride.slug %}" 
                           class="text-gray-900 hover:text-blue-600 dark:text-white dark:hover:text-blue-400">
                            {{ ride.name }}
                        </a>
                    </h2>
                    <p class="mb-3 text-gray-600 dark:text-gray-400">
                        at <a href="{% url 'parks:park_detail' ride.park.slug %}" 
                              class="text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300">
                            {{ ride.park.name }}
                        </a>
                    </p>
                    <div class="flex flex-wrap gap-2">
                        <span class="text-blue-800 bg-blue-100 status-badge dark:bg-blue-700 dark:text-blue-50">
                            {{ ride.get_category_display }}
                        </span>
                        <span class="status-badge {% if ride.status == 'OPERATING' %}status-operating
                                   {% elif ride.status == 'CLOSED_TEMP' or ride.status == 'CLOSED_PERM' %}status-closed
                                   {% elif ride.status == 'UNDER_CONSTRUCTION' %}status-construction
                                   {% elif ride.status == 'DEMOLISHED' %}status-demolished
                                   {% elif ride.status == 'RELOCATED' %}status-relocated{% endif %}">
                            {{ ride.get_status_display }}
                        </span>
                        {% if ride.average_rating %}
                            <span class="text-yellow-800 bg-yellow-100 status-badge dark:bg-yellow-400/30 dark:text-yellow-200 dark:ring-1 dark:ring-yellow-400/30">
                                <i class="mr-1 text-yellow-500 fas fa-star dark:text-yellow-300"></i>
                                {{ ride.average_rating|floatformat:1 }}/10
                            </span>
                        {% endif %}
                    </div>
                    {% if ride.coaster_stats %}
                        <div class="grid grid-cols-2 gap-2 mt-4">
                            {% if ride.coaster_stats.height_ft %}
                                <div class="text-sm text-gray-600 dark:text-gray-400">
                                    Height: {{ ride.coaster_stats.height_ft }}ft
                                </div>
                            {% endif %}
                            {% if ride.coaster_stats.speed_mph %}
                                <div class="text-sm text-gray-600 dark:text-gray-400">
                                    Speed: {{ ride.coaster_stats.speed_mph }}mph
                                </div>
                            {% endif %}
                        </div>
                    {% endif %}
                </div>
            </div>
        {% empty %}
            <div class="col-span-3 py-8 text-center">
                <p class="text-gray-500 dark:text-gray-400">No rides found matching your criteria.</p>
            </div>
        {% endfor %}
    </div>

    <!-- Pagination -->
    {% if is_paginated %}
    <div class="flex justify-center mt-6">
        <div class="inline-flex rounded-md shadow-sm">
            {% if page_obj.has_previous %}
                <a href="?page=1{{ request.GET.urlencode }}" class="px-3 py-2 text-gray-700 bg-white border border-gray-300 rounded-l-lg hover:bg-gray-50 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-700">&laquo; First</a>
                <a href="?page={{ page_obj.previous_page_number }}{{ request.GET.urlencode }}" class="px-3 py-2 text-gray-700 bg-white border-t border-b border-gray-300 hover:bg-gray-50 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-700">Previous</a>
            {% endif %}

            <span class="px-3 py-2 text-gray-700 bg-white border border-gray-300 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300">
                Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
            </span>

            {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}{{ request.GET.urlencode }}" class="px-3 py-2 text-gray-700 bg-white border-t border-b border-gray-300 hover:bg-gray-50 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-700">Next</a>
                <a href="?page={{ page_obj.paginator.num_pages }}{{ request.GET.urlencode }}" class="px-3 py-2 text-gray-700 bg-white border border-gray-300 rounded-r-lg hover:bg-gray-50 dark:bg-gray-800 dark:border-gray-600 dark:text-gray-300 dark:hover:bg-gray-700">Last &raquo;</a>
            {% endif %}
        </div>
    </div>
    {% endif %}
</div>
{% endblock %}
