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

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

{% block extra_css %}
<link rel="stylesheet" href="{% static 'css/inline-edit.css' %}">
{% endblock %}

{% block content %}
<div class="container px-4 mx-auto">
    <!-- Header -->
    <div class="p-6 mb-6 bg-white rounded-lg shadow dark:bg-gray-800 editable-container">
        <div class="flex items-start justify-between">
            <div>
                <h1 class="mb-2 text-3xl font-bold text-gray-900 dark:text-white" 
                    data-editable data-content-id="{{ ride.id }}" 
                    data-field-name="name">{{ ride.name }}</h1>
                <p class="mb-2 text-gray-600 dark:text-gray-400">
                    at <a href="{% url 'parks:park_detail' ride.park.slug %}" class="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 %}
                </p>
                <div class="flex flex-wrap gap-2 mt-3">
                    <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 %}"
                        data-editable data-content-id="{{ ride.id }}" 
                        data-field-name="status" data-field-type="select"
                        data-options='[
                            {"value": "OPERATING", "label": "Operating"},
                            {"value": "CLOSED_TEMP", "label": "Temporarily Closed"},
                            {"value": "CLOSED_PERM", "label": "Permanently Closed"},
                            {"value": "UNDER_CONSTRUCTION", "label": "Under Construction"},
                            {"value": "DEMOLISHED", "label": "Demolished"},
                            {"value": "RELOCATED", "label": "Relocated"}
                        ]'>
                        {{ ride.get_status_display }}
                    </span>
                    <span class="text-blue-800 bg-blue-100 status-badge dark:bg-blue-700 dark:text-blue-50"
                          data-editable data-content-id="{{ ride.id }}" 
                          data-field-name="category" data-field-type="select"
                          data-options='[
                            {"value": "RC", "label": "Roller Coaster"},
                            {"value": "DR", "label": "Dark Ride"},
                            {"value": "FR", "label": "Flat Ride"},
                            {"value": "WR", "label": "Water Ride"},
                            {"value": "TR", "label": "Transport"},
                            {"value": "OT", "label": "Other"}
                          ]'>
                        {{ ride.get_category_display }}
                    </span>
                    {% if ride.average_rating %}
                        <span class="flex items-center 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>
            {% if user.is_authenticated %}
                <div class="flex gap-2">
                    <button class="btn-secondary" data-edit-button 
                            data-content-id="{{ ride.id }}"
                            data-content-type="ride"
                            {% if not can_auto_approve %}data-require-reason="true"{% endif %}>
                        <i class="mr-2 fas fa-edit"></i>
                        Edit
                    </button>
                </div>
            {% endif %}
        </div>
    </div>

    <!-- Photos Grid -->
    {% if ride.photos.exists %}
        <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">Photos</h2>
            <div class="grid grid-cols-2 gap-4 md:grid-cols-3 lg:grid-cols-4">
                {% for photo in ride.photos.all %}
                    <div class="aspect-w-16 aspect-h-9">
                        <img src="{{ photo.image.url }}" 
                             alt="{{ photo.caption|default:ride.name }}"
                             class="object-cover rounded-lg">
                    </div>
                {% endfor %}
            </div>
        </div>
    {% endif %}

    <!-- 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">
            <div class="p-6 mb-6 bg-white rounded-lg shadow dark:bg-gray-800 editable-container">
                <h2 class="mb-4 text-xl font-semibold text-gray-900 dark:text-white">About</h2>
                <div class="prose dark:prose-invert max-w-none"
                     data-editable data-content-id="{{ ride.id }}" 
                     data-field-name="description" data-field-type="textarea">
                    {{ ride.description|linebreaks }}
                </div>
            </div>

            {% 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>{{ name_history.name }}</span>
                                <span class="text-gray-500">{{ 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 editable-container">
                    <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">
                        {% if coaster_stats.height_ft %}
                            <div>
                                <span class="block text-gray-500">Height</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white"
                                      data-editable data-content-id="{{ coaster_stats.id }}" 
                                      data-field-name="height_ft" data-field-type="number">
                                    {{ coaster_stats.height_ft }} ft
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.length_ft %}
                            <div>
                                <span class="block text-gray-500">Length</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white"
                                      data-editable data-content-id="{{ coaster_stats.id }}" 
                                      data-field-name="length_ft" data-field-type="number">
                                    {{ coaster_stats.length_ft }} ft
                                </span>
                            </div>
                        {% endif %}
                        {% if coaster_stats.speed_mph %}
                            <div>
                                <span class="block text-gray-500">Speed</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white"
                                      data-editable data-content-id="{{ coaster_stats.id }}" 
                                      data-field-name="speed_mph" data-field-type="number">
                                    {{ coaster_stats.speed_mph }} mph
                                </span>
                            </div>
                        {% endif %}
                        <div>
                            <span class="block text-gray-500">Inversions</span>
                            <span class="text-2xl font-bold text-gray-900 dark:text-white"
                                  data-editable data-content-id="{{ coaster_stats.id }}" 
                                  data-field-name="inversions" data-field-type="number">
                                {{ coaster_stats.inversions }}
                            </span>
                        </div>
                        {% if coaster_stats.ride_time_seconds %}
                            <div>
                                <span class="block text-gray-500">Ride Duration</span>
                                <span class="text-2xl font-bold text-gray-900 dark:text-white"
                                      data-editable data-content-id="{{ coaster_stats.id }}" 
                                      data-field-name="ride_time_seconds" data-field-type="number">
                                    {{ coaster_stats.ride_time_seconds }} sec
                                </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 editable-container">
                <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">Manufacturer</dt>
                        <dd class="font-medium text-gray-900 dark:text-white"
                            data-editable data-content-id="{{ ride.id }}" 
                            data-field-name="manufacturer">{{ ride.manufacturer }}</dd>
                    </div>
                    {% if ride.model_name %}
                        <div>
                            <dt class="text-gray-500">Model</dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ ride.id }}" 
                                data-field-name="model_name">{{ ride.model_name }}</dd>
                        </div>
                    {% endif %}
                    {% if ride.opening_date %}
                        <div>
                            <dt class="text-gray-500">Opening Date</dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ ride.id }}" 
                                data-field-name="opening_date" data-field-type="date">
                                {{ ride.opening_date }}
                            </dd>
                        </div>
                    {% endif %}
                    {% if ride.status_since %}
                        <div>
                            <dt class="text-gray-500">Status Since</dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ ride.id }}" 
                                data-field-name="status_since" data-field-type="date">
                                {{ ride.status_since }}
                            </dd>
                        </div>
                    {% endif %}
                    {% if ride.closing_date %}
                        <div>
                            <dt class="text-gray-500">Closing Date</dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ ride.id }}" 
                                data-field-name="closing_date" data-field-type="date">
                                {{ ride.closing_date }}
                            </dd>
                        </div>
                    {% endif %}
                    {% if ride.capacity_per_hour %}
                        <div>
                            <dt class="text-gray-500">Capacity</dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ ride.id }}" 
                                data-field-name="capacity_per_hour" data-field-type="number">
                                {{ ride.capacity_per_hour }} riders/hour
                            </dd>
                        </div>
                    {% endif %}
                    {% if ride.min_height_in %}
                        <div>
                            <dt class="text-gray-500">Minimum Height</dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ ride.id }}" 
                                data-field-name="min_height_in" data-field-type="number">
                                {{ ride.min_height_in }} inches
                            </dd>
                        </div>
                    {% endif %}
                </dl>
            </div>

            <!-- History Panel -->
            <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">History</h2>
                <div class="space-y-4">
                    {% for record in history %}
                        <div class="p-4 rounded-lg bg-gray-50 dark:bg-gray-700/50">
                            <div class="text-sm text-gray-500 dark:text-gray-400">
                                {{ record.history_date|date:"M d, Y H:i" }}
                                {% if record.history_user %}
                                    by {{ record.history_user.username }}
                                {% endif %}
                            </div>
                            <div class="mt-2">
                                {% for field, change in record.diff_against_previous %}
                                    <div class="text-sm">
                                        <span class="font-medium">{{ field }}:</span>
                                        {{ change.old }} → {{ change.new }}
                                    </div>
                                {% endfor %}
                            </div>
                        </div>
                    {% empty %}
                        <p class="text-gray-500">No history available.</p>
                    {% endfor %}
                </div>
            </div>

            <!-- Photos -->
            {% if ride.photos.exists %}
                <div class="p-6 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>
                    <div class="grid grid-cols-2 gap-2">
                        {% for photo in ride.photos.all %}
                            <div class="aspect-w-16 aspect-h-9">
                                <img src="{{ photo.image.url }}" 
                                     alt="{{ photo.caption|default:ride.name }}"
                                     class="object-cover rounded-lg">
                            </div>
                        {% endfor %}
                    </div>
                </div>
            {% endif %}
        </div>
    </div>

    <!-- Reviews Section -->
    <div class="p-6 mt-6 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">
                                    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>{{ 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">No reviews yet. Be the first to review this ride!</p>
        {% endif %}
    </div>
</div>
{% endblock %}

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