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

{% block title %}{{ title }}{% 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">
        <div>
            <h1 class="text-3xl font-bold text-gray-900 dark:text-white">{{ title }}</h1>
            {% if park %}
                <a href="{% url 'parks:park_detail' park.slug %}" class="text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300">
                    Back to {{ park.name }}
                </a>
            {% endif %}
        </div>
    </div>

    <!-- Category Filters -->
    <div class="flex flex-wrap gap-4 mb-8">
        {% if park %}
            <a href="{% url 'parks:park_roller_coasters' park.slug %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'RC' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Roller Coasters
            </a>
            <a href="{% url 'parks:park_dark_rides' park.slug %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'DR' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Dark Rides
            </a>
            <a href="{% url 'parks:park_flat_rides' park.slug %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'FR' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Flat Rides
            </a>
            <a href="{% url 'parks:park_water_rides' park.slug %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'WR' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Water Rides
            </a>
            <a href="{% url 'parks:park_transports' park.slug %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'TR' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Transports
            </a>
            <a href="{% url 'parks:park_others' park.slug %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'OT' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Others
            </a>
        {% else %}
            <a href="{% url 'rides:roller_coasters' %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'RC' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Roller Coasters
            </a>
            <a href="{% url 'rides:dark_rides' %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'DR' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Dark Rides
            </a>
            <a href="{% url 'rides:flat_rides' %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'FR' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Flat Rides
            </a>
            <a href="{% url 'rides:water_rides' %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'WR' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Water Rides
            </a>
            <a href="{% url 'rides:transports' %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'TR' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Transports
            </a>
            <a href="{% url 'rides:others' %}" 
               class="px-4 py-2 rounded-lg transition-colors {% if category_code == 'OT' %}bg-blue-600 text-white dark:bg-blue-500{% else %}bg-gray-100 text-gray-700 hover:bg-gray-200 dark:bg-gray-700 dark:text-gray-300 dark:hover:bg-gray-600{% endif %}">
                Others
            </a>
        {% endif %}
    </div>

    {% if not categories %}
        <p class="text-gray-600 dark:text-gray-400">No rides found.</p>
    {% endif %}

    {% for category_name, rides in categories.items %}
        <div class="mb-10">
            <h2 class="mb-4 text-2xl font-semibold text-gray-900 dark:text-white">{{ category_name }}s</h2>
            <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">
                        <div class="aspect-w-16 aspect-h-9">
                            {% if ride.photos.exists %}
                                <img src="{{ ride.photos.first.image.url }}" 
                                     alt="{{ ride.name }}"
                                     class="object-cover w-full">
                            {% else %}
                                <img src="{% get_ride_placeholder_image ride.category %}"
                                     alt="{{ ride.name }}"
                                     class="object-cover w-full">
                            {% endif %}
                        </div>
                        
                        <div class="p-4">
                            <h3 class="mb-2 text-xl font-bold">
                                <a href="{% url 'parks: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>
                            </h3>
                            {% if not park %}
                                <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>
                            {% endif %}
                            {% if ride.manufacturer %}
                                <p class="mb-3 text-gray-600 dark:text-gray-400">{{ ride.manufacturer.name }}</p>
                            {% endif %}
                            <div class="flex flex-wrap gap-2">
                                <span class="text-blue-800 bg-blue-100 status-badge dark:bg-blue-400/30 dark:text-blue-200 dark:ring-1 dark:ring-blue-400/30">
                                    {{ 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>
                {% endfor %}
            </div>
        </div>
    {% endfor %}
</div>
{% endblock %}
