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

{% block title %}{{ 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">
    <!-- Park Header -->
    <div class="p-6 mb-6 bg-white border border-gray-200 rounded-lg shadow-lg dark:bg-gray-800 dark:border-gray-700 editable-container">
        <div class="flex flex-col items-start justify-between md:flex-row md:items-center">
            <div>
                <h1 class="mb-2 text-3xl font-bold text-gray-900 dark:text-white" 
                    data-editable data-content-id="{{ park.id }}" 
                    data-field-name="name">{{ park.name }}</h1>
                <p class="text-gray-600 dark:text-gray-300">
                    <i class="mr-2 fas fa-map-marker-alt"></i>
                    <span data-editable data-content-id="{{ park.id }}" 
                          data-field-name="location">{{ park.location }}</span>
                </p>
            </div>
            <div class="flex gap-2 mt-4 md:mt-0">
                {% if park.website %}
                    <a href="{{ park.website }}" target="_blank" rel="noopener noreferrer" 
                       class="btn-secondary">
                        <i class="mr-2 fas fa-external-link-alt"></i>Visit Website
                    </a>
                {% endif %}
                {% if user.is_authenticated %}
                    <button class="btn-secondary" data-edit-button 
                            data-content-id="{{ park.id }}"
                            data-content-type="park"
                            {% if not can_auto_approve %}data-require-reason="true"{% endif %}>
                        <i class="mr-2 fas fa-edit"></i>Edit
                    </button>
                {% endif %}
            </div>
        </div>
        
        <div class="flex flex-wrap gap-2 mt-4">
            <span class="status-badge {% if park.status == 'OPERATING' %}status-operating
                       {% elif park.status == 'CLOSED_TEMP' or park.status == 'CLOSED_PERM' %}status-closed
                       {% elif park.status == 'UNDER_CONSTRUCTION' %}status-construction
                       {% elif park.status == 'DEMOLISHED' %}status-demolished
                       {% elif park.status == 'RELOCATED' %}status-relocated{% endif %}"
                  data-editable data-content-id="{{ park.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"}
                  ]'>
                {{ park.get_status_display }}
            </span>
            {% if park.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>
                    {{ park.average_rating|floatformat:1 }}/10
                </span>
            {% endif %}
        </div>
    </div>

    <!-- Park Stats -->
    <div class="grid grid-cols-1 gap-6 mb-6 md:grid-cols-3">
        <div class="p-6 text-center transition-transform transform bg-white rounded-lg shadow-lg dark:bg-gray-800 hover:-translate-y-1">
            <div class="text-3xl font-bold text-blue-600 dark:text-blue-400">
                {{ rides.count }}
            </div>
            <div class="mt-1 text-gray-600 dark:text-gray-300">Total Attractions</div>
        </div>
        
        <div class="p-6 text-center transition-transform transform bg-white rounded-lg shadow-lg dark:bg-gray-800 hover:-translate-y-1">
            <div class="text-3xl font-bold text-blue-600 dark:text-blue-400">
                {% with roller_coasters=rides|dictsortreversed:"category"|slice:":RC" %}
                    {{ roller_coasters|length }}
                {% endwith %}
            </div>
            <div class="mt-1 text-gray-600 dark:text-gray-300">Roller Coasters</div>
        </div>
        
        <div class="p-6 text-center transition-transform transform bg-white rounded-lg shadow-lg dark:bg-gray-800 hover:-translate-y-1">
            <div class="text-3xl font-bold text-blue-600 dark:text-blue-400">
                {{ areas.count }}
            </div>
            <div class="mt-1 text-gray-600 dark:text-gray-300">Areas</div>
        </div>
    </div>

    <!-- Main Content Grid -->
    <div class="grid grid-cols-1 gap-6 lg:grid-cols-3">
        <!-- Left Column - Description and Areas -->
        <div class="lg:col-span-2">
            {% if park.description %}
                <div class="p-6 mb-6 bg-white rounded-lg shadow-lg dark:bg-gray-800 editable-container">
                    <h2 class="mb-4 text-2xl font-bold text-gray-900 dark:text-white">About</h2>
                    <div class="text-gray-700 dark:text-gray-300"
                         data-editable data-content-id="{{ park.id }}" 
                         data-field-name="description" data-field-type="textarea">
                        {{ park.description|linebreaks }}
                    </div>
                </div>
            {% endif %}

            <!-- Park Areas -->
            {% if areas %}
                <div class="p-6 mb-6 bg-white rounded-lg shadow-lg dark:bg-gray-800">
                    <h2 class="mb-4 text-2xl font-bold text-gray-900 dark:text-white">Areas</h2>
                    <div class="grid gap-4">
                        {% for area in areas %}
                            <div class="p-4 transition-transform transform rounded-lg bg-gray-50 dark:bg-gray-700/50 hover:-translate-y-1">
                                <h3 class="mb-2 text-lg font-semibold text-gray-900 dark:text-white">
                                    <a href="{% url 'parks:area_detail' park.slug area.slug %}">{{ area.name }}</a>
                                </h3>
                                {% if area.description %}
                                    <p class="mb-2 text-gray-600 dark:text-gray-300">
                                        {{ area.description }}
                                    </p>
                                {% endif %}
                                <div class="text-sm text-gray-500 dark:text-gray-400">
                                    {{ area.rides.count }} attractions
                                </div>
                            </div>
                        {% endfor %}
                    </div>
                </div>
            {% endif %}

            <!-- Rides List -->
            <div class="p-6 bg-white rounded-lg shadow-lg dark:bg-gray-800">
                <h2 class="mb-4 text-2xl font-bold text-gray-900 dark:text-white">Attractions</h2>
                <div class="grid grid-cols-1 gap-4 md:grid-cols-2">
                    {% for ride in rides %}
                        <div class="p-4 transition-transform transform rounded-lg bg-gray-50 dark:bg-gray-700/50 hover:-translate-y-1">
                            <h3 class="mb-2 text-lg font-semibold">
                                <a href="{% url 'rides:ride_detail' park.slug ride.slug %}" 
                                   class="text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300">
                                    {{ ride.name }}
                                </a>
                            </h3>
                            <div class="flex flex-wrap gap-2 mb-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
                                    {% else %}status-construction{% endif %}">
                                    {{ ride.get_status_display }}
                                </span>
                            </div>
                            {% if ride.coaster_stats %}
                                <div class="space-y-1 text-sm text-gray-600 dark:text-gray-400">
                                    {% if ride.coaster_stats.height_ft %}
                                        <div>Height: {{ ride.coaster_stats.height_ft }}ft</div>
                                    {% endif %}
                                    {% if ride.coaster_stats.speed_mph %}
                                        <div>Speed: {{ ride.coaster_stats.speed_mph }}mph</div>
                                    {% endif %}
                                </div>
                            {% endif %}
                        </div>
                    {% empty %}
                        <div class="col-span-2 py-8 text-center">
                            <p class="text-gray-500 dark:text-gray-400">No attractions found.</p>
                        </div>
                    {% endfor %}
                </div>
            </div>
        </div>

        <!-- Right Column - Quick Facts and History -->
        <div>
            <div class="p-6 mb-6 bg-white rounded-lg shadow-lg dark:bg-gray-800 editable-container">
                <h2 class="mb-4 text-2xl font-bold text-gray-900 dark:text-white">Quick Facts</h2>
                <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-1">
                    {% if park.owner %}
                        <div class="p-4 transition-transform transform rounded-lg bg-gray-50 dark:bg-gray-700/50 hover:-translate-y-1">
                            <dt class="flex items-center mb-1 text-gray-600 dark:text-gray-300">
                                <i class="w-5 text-blue-500 fas fa-building dark:text-blue-400"></i>
                                <span class="ml-2">Operator</span>
                            </dt>
                            <dd class="font-medium text-gray-900 dark:text-white">
                                <a href="{% url 'companies:company_detail' park.owner.slug %}" 
                                   class="text-blue-600 dark:text-blue-400 hover:text-blue-700 dark:hover:text-blue-300">
                                    {{ park.owner.name }}
                                </a>
                            </dd>
                        </div>
                    {% endif %}
                    <div class="p-4 transition-transform transform rounded-lg bg-gray-50 dark:bg-gray-700/50 hover:-translate-y-1">
                        <dt class="flex items-center mb-1 text-gray-600 dark:text-gray-300">
                            <i class="w-5 text-blue-500 fas fa-globe dark:text-blue-400"></i>
                            <span class="ml-2">Country</span>
                        </dt>
                        <dd class="font-medium text-gray-900 dark:text-white"
                            data-editable data-content-id="{{ park.id }}" 
                            data-field-name="country">
                            {{ park.get_country_name }}
                        </dd>
                    </div>
                    {% if park.opening_date %}
                        <div class="p-4 transition-transform transform rounded-lg bg-gray-50 dark:bg-gray-700/50 hover:-translate-y-1">
                            <dt class="flex items-center mb-1 text-gray-600 dark:text-gray-300">
                                <i class="w-5 text-blue-500 fas fa-calendar-alt dark:text-blue-400"></i>
                                <span class="ml-2">Opening Date</span>
                            </dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ park.id }}" 
                                data-field-name="opening_date" data-field-type="date">
                                {{ park.opening_date }}
                            </dd>
                        </div>
                    {% endif %}
                    {% if park.closing_date %}
                        <div class="p-4 transition-transform transform rounded-lg bg-gray-50 dark:bg-gray-700/50 hover:-translate-y-1">
                            <dt class="flex items-center mb-1 text-gray-600 dark:text-gray-300">
                                <i class="w-5 text-blue-500 fas fa-calendar-times dark:text-blue-400"></i>
                                <span class="ml-2">Closing Date</span>
                            </dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ park.id }}" 
                                data-field-name="closing_date" data-field-type="date">
                                {{ park.closing_date }}
                            </dd>
                        </div>
                    {% endif %}
                    {% if park.operating_season %}
                        <div class="p-4 transition-transform transform rounded-lg bg-gray-50 dark:bg-gray-700/50 hover:-translate-y-1">
                            <dt class="flex items-center mb-1 text-gray-600 dark:text-gray-300">
                                <i class="w-5 text-blue-500 fas fa-clock dark:text-blue-400"></i>
                                <span class="ml-2">Operating Season</span>
                            </dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ park.id }}" 
                                data-field-name="operating_season">
                                {{ park.operating_season }}
                            </dd>
                        </div>
                    {% endif %}
                    {% if park.size_acres %}
                        <div class="p-4 transition-transform transform rounded-lg bg-gray-50 dark:bg-gray-700/50 hover:-translate-y-1">
                            <dt class="flex items-center mb-1 text-gray-600 dark:text-gray-300">
                                <i class="w-5 text-blue-500 fas fa-ruler-combined dark:text-blue-400"></i>
                                <span class="ml-2">Size</span>
                            </dt>
                            <dd class="font-medium text-gray-900 dark:text-white"
                                data-editable data-content-id="{{ park.id }}" 
                                data-field-name="size_acres" data-field-type="number">
                                {{ park.size_acres }} acres
                            </dd>
                        </div>
                    {% endif %}
                </div>
            </div>

            <!-- History Panel -->
            <div class="p-6 mb-6 bg-white rounded-lg shadow-lg dark:bg-gray-800">
                <h2 class="mb-4 text-2xl font-bold 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 dark:text-gray-400">No history available.</p>
                    {% endfor %}
                </div>
            </div>

            <!-- Photos -->
            {% if park.photos.exists %}
                <div class="p-6 bg-white rounded-lg shadow-lg dark:bg-gray-800">
                    <h2 class="mb-4 text-2xl font-bold text-gray-900 dark:text-white">Photos</h2>
                    <div class="grid grid-cols-2 gap-2">
                        {% for photo in park.photos.all %}
                            <div class="aspect-w-16 aspect-h-9">
                                <img src="{{ photo.image.url }}" 
                                     alt="{{ photo.caption|default:park.name }}"
                                     class="object-cover rounded-lg">
                            </div>
                        {% endfor %}
                    </div>
                </div>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}

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