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

{% block title %}{{ ride.name }} at {{ ride.park.name }} - ThrillWiki{% endblock %}

{% block content %}
<div class="container px-4 mx-auto sm:px-6 lg:px-8">
    <!-- Action Buttons - Above header -->
    {% if user.is_authenticated %}
    <div class="flex justify-end gap-2 mb-2">
        <a href="{% url 'parks:rides:ride_update' park_slug=ride.park.slug ride_slug=ride.slug %}"
           class="transition-transform btn-secondary hover:scale-105">
            <i class="mr-1 fas fa-pencil-alt"></i>Edit
        </a>
        {% if perms.media.add_photo %}
        <button class="transition-transform btn-secondary hover:scale-105"
                @click="$dispatch('show-photo-upload')">
            <i class="mr-1 fas fa-camera"></i>Upload Photo
        </button>
        {% endif %}
    </div>
    {% endif %}

    <!-- Status Management Section (Moderators Only) -->
    <div id="ride-status-section"
         hx-get="{% url 'parks:rides:ride_status_actions' park_slug=ride.park.slug ride_slug=ride.slug %}"
         hx-trigger="load"
         hx-swap="outerHTML">
    </div>

    <!-- Ride Header -->
    <div class="p-compact mb-6 bg-white rounded-lg shadow-lg dark:bg-gray-800">
        <div class="text-center">
            <h1 class="text-3xl font-bold text-gray-900 dark:text-white lg:text-4xl">{{ ride.name }}</h1>
            <div class="flex items-center justify-center mt-2 text-sm text-gray-600 dark:text-gray-400">
                at <a href="{% url 'parks:park_detail' ride.park.slug %}" class="ml-1 text-blue-500 hover:text-blue-600 dark:text-blue-400 dark:hover:text-blue-300">
                    {{ ride.park.name }}
                </a>
                {% if ride.park_area %}
                    - {{ ride.park_area.name }}
                {% endif %}
            </div>
            <div class="flex flex-wrap items-center justify-center gap-2 mt-3">
                {% include "rides/partials/ride_header_badge.html" with ride=ride %}

                <span class="px-3 py-1 text-sm font-medium text-blue-800 bg-blue-100 status-badge dark:bg-blue-700 dark:text-blue-50">
                    {{ ride.get_category_display }}
                </span>
                {% if ride.average_rating %}
                    <span class="flex items-center px-3 py-1 text-sm font-medium text-yellow-800 bg-yellow-100 status-badge dark:bg-yellow-600 dark:text-yellow-50">
                        <span class="mr-1 text-yellow-500 dark:text-yellow-200">★</span>
                        {{ ride.average_rating|floatformat:1 }}/10
                    </span>
                {% endif %}
            </div>
        </div>
    </div>

    <!-- Horizontal Stats Bar -->
    <div class="grid grid-cols-2 gap-4 mb-6 md:grid-cols-3 lg:grid-cols-5">
        <!-- Statistics Card -->
        <div class="bg-white rounded-lg shadow-lg dark:bg-gray-800 p-compact card-stats">
            <div class="text-center">
                <dt class="text-sm font-semibold text-gray-900 dark:text-white">Statistics</dt>
                <dd class="mt-1 space-y-1">
                    {% if coaster_stats.height_ft %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">{{ coaster_stats.height_ft }}ft high</div>
                    {% endif %}
                    {% if coaster_stats.speed_mph %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">{{ coaster_stats.speed_mph }}mph</div>
                    {% endif %}
                    {% if coaster_stats.length_ft %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">{{ coaster_stats.length_ft }}ft long</div>
                    {% endif %}
                </dd>
            </div>
        </div>

        <!-- Experience Card -->
        <div class="bg-white rounded-lg shadow-lg dark:bg-gray-800 p-compact card-stats">
            <div class="text-center">
                <dt class="text-sm font-semibold text-gray-900 dark:text-white">Experience</dt>
                <dd class="mt-1 space-y-1">
                    <div class="text-xs text-sky-900 dark:text-sky-400">{{ ride.get_category_display }}</div>
                    {% if coaster_stats.ride_time_seconds %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">{{ coaster_stats.ride_time_seconds }}s ride</div>
                    {% endif %}
                    {% if ride.min_height_in %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">{{ ride.min_height_in }}" min</div>
                    {% endif %}
                </dd>
            </div>
        </div>

        <!-- Manufacturer Card -->
        <div class="bg-white rounded-lg shadow-lg dark:bg-gray-800 p-compact card-stats">
            <div class="text-center">
                <dt class="text-sm font-semibold text-gray-900 dark:text-white">Manufacturer</dt>
                <dd class="mt-1">
                    {% if ride.manufacturer %}
                        <a href="#"
                           class="text-xs text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300">
                            {{ ride.manufacturer.name }}
                        </a>
                    {% else %}
                        <span class="text-xs text-sky-900 dark:text-sky-400">Unknown</span>
                    {% endif %}
                    {% if ride.model_name %}
                        <div class="text-xs text-sky-900 dark:text-sky-400 mt-1">{{ ride.model_name }}</div>
                    {% endif %}
                </dd>
            </div>
        </div>

        <!-- History Card -->
        <div class="bg-white rounded-lg shadow-lg dark:bg-gray-800 p-compact card-stats">
            <div class="text-center">
                <dt class="text-sm font-semibold text-gray-900 dark:text-white">History</dt>
                <dd class="mt-1 space-y-1">
                    {% if ride.opening_date %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">Opened {{ ride.opening_date }}</div>
                    {% endif %}
                    {% if ride.designer %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">by {{ ride.designer.name }}</div>
                    {% endif %}
                    {% if ride.status_since %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">{{ ride.get_status_display }} since {{ ride.status_since }}</div>
                    {% endif %}
                </dd>
            </div>
        </div>

        <!-- Performance Card -->
        <div class="bg-white rounded-lg shadow-lg dark:bg-gray-800 p-compact card-stats">
            <div class="text-center">
                <dt class="text-sm font-semibold text-gray-900 dark:text-white">Performance</dt>
                <dd class="mt-1 space-y-1">
                    {% if ride.average_rating %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">★ {{ ride.average_rating|floatformat:1 }}/10</div>
                    {% endif %}
                    {% if ride.capacity_per_hour %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">{{ ride.capacity_per_hour }}/hr</div>
                    {% endif %}
                    {% if coaster_stats.inversions %}
                        <div class="text-xs text-sky-900 dark:text-sky-400">{{ coaster_stats.inversions }} inversions</div>
                    {% endif %}
                </dd>
            </div>
        </div>
    </div>

    <!-- Rest of the content remains unchanged -->
    {% if ride.photos.exists %}
        <div class="p-6 mb-8 bg-white rounded-lg shadow dark:bg-gray-800">
            <h2 class="mb-4 text-xl font-semibold text-gray-900 dark:text-white">Photos</h2>
            {% include "media/partials/photo_display.html" with photos=ride.photos.all content_type="rides.ride" object_id=ride.id %}
        </div>
    {% endif %}

    <!-- Reviews Section -->
    <div class="p-6 mb-8 bg-white rounded-lg shadow dark:bg-gray-800">
        <div class="flex items-center justify-between mb-4">
            <h2 class="text-xl font-semibold text-gray-900 dark:text-white">Reviews</h2>
            {% if user.is_authenticated %}
                <button class="btn-primary">
                    <i class="mr-2 fas fa-star"></i>
                    Write a Review
                </button>
            {% endif %}
        </div>
        
        {% if ride.reviews.exists %}
            <div class="space-y-4">
                {% for review in ride.reviews.all %}
                    <div class="pb-4 border-b border-gray-200 dark:border-gray-700">
                        <div class="flex items-start justify-between">
                            <div>
                                <h3 class="font-semibold text-gray-900 dark:text-white">{{ review.title }}</h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400">
                                    by {{ review.user.username }} on {{ review.created_at|date }}
                                </p>
                            </div>
                            <div class="flex items-center">
                                <span class="mr-1 text-yellow-400">★</span>
                                <span class="text-gray-900 dark:text-white">{{ review.rating }}/10</span>
                            </div>
                        </div>
                        <p class="mt-2 text-gray-700 dark:text-gray-300">{{ review.content }}</p>
                    </div>
                {% endfor %}
            </div>
        {% else %}
            <p class="text-gray-500 dark:text-gray-400">No reviews yet. Be the first to review this ride!</p>
        {% endif %}
    </div>

    <!-- Main Content Grid -->
    <div class="grid grid-cols-1 gap-6 lg:grid-cols-3">
        <!-- Left Column - Description and Details -->
        <div class="lg:col-span-2">
            {% if ride.description %}
                <div class="p-6 mb-6 bg-white rounded-lg shadow dark:bg-gray-800">
                    <h2 class="mb-4 text-xl font-semibold text-gray-900 dark:text-white">Trivia</h2>
                    <div class="prose dark:prose-invert max-w-none">
                        {{ ride.description|linebreaks }}
                    </div>
                </div>
            {% endif %}

            {% if ride.previous_names %}
                <div class="p-6 mb-6 bg-white rounded-lg shadow dark:bg-gray-800">
                    <h2 class="mb-4 text-xl font-semibold text-gray-900 dark:text-white">Previous Names</h2>
                    <div class="space-y-2">
                        {% for name_history in ride.previous_names %}
                            <div class="flex justify-between">
                                <span class="text-gray-900 dark:text-white">{{ name_history.name }}</span>
                                <span class="text-gray-500 dark:text-gray-400">{{ name_history.period }}</span>
                            </div>
                        {% endfor %}
                    </div>
                </div>
            {% endif %}

            {% if coaster_stats %}
                <div class="p-6 mb-6 bg-white rounded-lg shadow dark:bg-gray-800">
                    <h2 class="mb-4 text-xl font-semibold text-gray-900 dark:text-white">Roller Coaster Statistics</h2>
                    <div class="grid grid-cols-2 gap-4 md:grid-cols-3">
                        <!-- Coaster Type -->
                        {% if coaster_stats.roller_coaster_type %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Coaster Type</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.get_roller_coaster_type_display }}
                                </span>
                            </div>
                        {% endif %}

                        <!-- Height Stats -->
                        {% if coaster_stats.height_ft %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Maximum Height</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.height_ft }} ft
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.max_drop_height_ft %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Drop Height</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.max_drop_height_ft }} ft
                                </span>
                            </div>
                        {% endif %}

                        <!-- Track Stats -->
                        {% if coaster_stats.length_ft %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Track Length</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.length_ft }} ft
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.track_type %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Track Layout</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.track_type }}
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.track_material %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Track Material</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.get_track_material_display }}
                                </span>
                            </div>
                        {% endif %}

                        <!-- Speed and Time -->
                        {% if coaster_stats.speed_mph %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Maximum Speed</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.speed_mph }} mph
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.ride_time_seconds %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Ride Duration</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.ride_time_seconds }} sec
                                </span>
                            </div>
                        {% endif %}

                        <!-- Train Details -->
                        {% if coaster_stats.train_style %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Train Style</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.train_style }}
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.trains_count %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Number of Trains</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.trains_count }}
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.cars_per_train %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Cars per Train</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.cars_per_train }}
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.seats_per_car %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Seats per Car</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.seats_per_car }}
                                </span>
                            </div>
                        {% endif %}

                        <!-- Other Stats -->
                        {% if coaster_stats.inversions %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Inversions</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.inversions }}
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.launch_type %}
                            <div>
                                <span class="block text-gray-500 dark:text-gray-400">Launch Type</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white">
                                    {{ coaster_stats.get_launch_type_display }}
                                </span>
                            </div>
                        {% endif %}
                    </div>
                </div>
            {% endif %}
        </div>

        <!-- Right Column - Quick Facts -->
        <div class="lg:col-span-1">
            <div class="p-6 mb-6 bg-white rounded-lg shadow dark:bg-gray-800">
                <h2 class="mb-4 text-xl font-semibold text-gray-900 dark:text-white">Quick Facts</h2>
                <dl class="space-y-4">
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Manufacturer</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">
                            {% if ride.manufacturer %}
                                <a href="#"
                                   class="text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300">
                                    {{ ride.manufacturer.name }}
                                </a>
                            {% else %}
                                <span class="text-gray-500 dark:text-gray-400">Not specified</span>
                            {% endif %}
                        </dd>
                    </div>
                    {% if ride.designer %}
                        <div>
                            <dt class="text-gray-500 dark:text-gray-400">Designer</dt>
                            <dd class="font-medium text-gray-900 dark:text-white">
                                <a href="{% url 'designers:designer_detail' ride.designer.slug %}" 
                                   class="text-blue-600 hover:text-blue-700 dark:text-blue-400 dark:hover:text-blue-300">
                                    {{ ride.designer.name }}
                                </a>
                            </dd>
                        </div>
                    {% endif %}
                    {% if ride.model_name %}
                        <div>
                            <dt class="text-gray-500 dark:text-gray-400">Model</dt>
                            <dd class="font-medium text-gray-900 dark:text-white">{{ ride.model_name }}</dd>
                        </div>
                    {% endif %}
                    {% if ride.opening_date %}
                        <div>
                            <dt class="text-gray-500 dark:text-gray-400">Opening Date</dt>
                            <dd class="font-medium text-gray-900 dark:text-white">
                                {{ ride.opening_date }}
                            </dd>
                        </div>
                    {% endif %}
                    {% if ride.status_since %}
                        <div>
                            <dt class="text-gray-500 dark:text-gray-400">Status Since</dt>
                            <dd class="font-medium text-gray-900 dark:text-white">
                                {{ ride.status_since }}
                            </dd>
                        </div>
                    {% endif %}
                    {% if ride.closing_date %}
                        <div>
                            <dt class="text-gray-500 dark:text-gray-400">Closing Date</dt>
                            <dd class="font-medium text-gray-900 dark:text-white">
                                {{ ride.closing_date }}
                            </dd>
                        </div>
                    {% endif %}
                    {% if ride.capacity_per_hour %}
                        <div>
                            <dt class="text-gray-500 dark:text-gray-400">Capacity</dt>
                            <dd class="font-medium text-gray-900 dark:text-white">
                                {{ ride.capacity_per_hour }} riders/hour
                            </dd>
                        </div>
                    {% endif %}
                    {% if ride.min_height_in %}
                        <div>
                            <dt class="text-gray-500 dark:text-gray-400">Minimum Height</dt>
                            <dd class="font-medium text-gray-900 dark:text-white">
                                {{ ride.min_height_in }} inches
                            </dd>
                        </div>
                    {% endif %}
                </dl>
            </div>

            <!-- History Panel -->
            {% include "rides/partials/history_panel.html" with history=history %}
        </div>
    </div>
</div>

<!-- Photo Upload Modal -->
{% if perms.media.add_photo %}
    <div x-cloak
         x-data="{ show: false }"
         @show-photo-upload.window="show = true"
         x-show="show"
         class="fixed inset-0 z-[60] flex items-center justify-center bg-black/50"
         @click.self="show = false">
        <div class="w-full max-w-2xl p-6 mx-4 bg-white rounded-lg shadow-xl dark:bg-gray-800">
            <div class="flex items-center justify-between mb-4">
                <h3 class="text-xl font-semibold text-gray-900 dark:text-white">Upload Photos</h3>
                <button @click="show = false" class="text-gray-500 hover:text-gray-700 dark:hover:text-gray-300">
                    <i class="text-xl fas fa-times"></i>
                </button>
            </div>
            {% include "media/partials/photo_upload.html" with content_type="rides.ride" object_id=ride.id %}
        </div>
    </div>
{% endif %}
{% endblock content %}

{% block extra_js %}
<script src="{% static 'js/photo-gallery.js' %}"></script>
{% endblock %}
