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

{% block title %}Ride Manufacturers - ThrillWiki{% endblock %}

{% block content %}
<div class="container mx-auto px-4">
    <div class="flex justify-between items-center mb-6">
        <h1 class="text-3xl font-bold text-gray-900 dark:text-white">Ride Manufacturers</h1>
    </div>

    <!-- Stats -->
    <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-6">
        <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-6 text-center">
            <div class="text-3xl font-bold text-blue-600 dark:text-blue-400">
                {{ total_manufacturers }}
            </div>
            <div class="text-gray-600 dark:text-gray-400 mt-1">Manufacturers</div>
        </div>
        
        <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-6 text-center">
            <div class="text-3xl font-bold text-blue-600 dark:text-blue-400">
                {{ total_rides }}
            </div>
            <div class="text-gray-600 dark:text-gray-400 mt-1">Total Rides</div>
        </div>
        
        <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-6 text-center">
            <div class="text-3xl font-bold text-blue-600 dark:text-blue-400">
                {{ total_roller_coasters }}
            </div>
            <div class="text-gray-600 dark:text-gray-400 mt-1">Roller Coasters</div>
        </div>
    </div>

    <!-- Filters -->
    <div class="bg-white dark:bg-gray-800 shadow rounded-lg p-4 mb-6">
        <form method="get" class="grid grid-cols-1 md:grid-cols-3 gap-4">
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Search</label>
                <input type="text" name="search" value="{{ request.GET.search }}" 
                       class="form-input w-full" placeholder="Search manufacturers...">
            </div>
            <div>
                <label class="block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1">Country</label>
                <input type="text" name="country" value="{{ request.GET.country }}" 
                       class="form-input w-full" placeholder="Filter by country...">
            </div>
            <div class="flex items-end">
                <button type="submit" class="btn-primary w-full">Apply Filters</button>
            </div>
        </form>
    </div>

    <!-- Manufacturers Grid -->
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
        {% for manufacturer in manufacturers %}
            <div class="bg-white dark:bg-gray-800 shadow rounded-lg overflow-hidden">
                <div class="p-4">
                    <h3 class="text-xl font-semibold mb-2">
                        <a href="{% url 'companies:manufacturer_detail' manufacturer.slug %}" 
                           class="text-blue-600 dark:text-blue-400 hover:underline">
                            {{ manufacturer.name }}
                        </a>
                    </h3>
                    {% if manufacturer.headquarters %}
                        <p class="text-gray-600 dark:text-gray-400 mb-2">{{ manufacturer.headquarters }}</p>
                    {% endif %}
                    <div class="text-sm text-gray-500 dark:text-gray-400">
                        {{ manufacturer.rides.count }} rides manufactured
                    </div>
                </div>
            </div>
        {% empty %}
            <div class="col-span-full text-center py-8">
                <p class="text-gray-500 dark:text-gray-400">No manufacturers found matching your criteria.</p>
            </div>
        {% endfor %}
    </div>

    <!-- Pagination -->
    {% if is_paginated %}
        <div class="flex justify-center mt-6">
            <nav class="inline-flex rounded-md shadow">
                {% if page_obj.has_previous %}
                    <a href="?page={{ page_obj.previous_page_number }}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}{% if request.GET.country %}&country={{ request.GET.country }}{% endif %}" 
                       class="pagination-link">Previous</a>
                {% endif %}
                
                {% for num in page_obj.paginator.page_range %}
                    {% if page_obj.number == num %}
                        <span class="pagination-current">{{ num }}</span>
                    {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                        <a href="?page={{ num }}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}{% if request.GET.country %}&country={{ request.GET.country }}{% endif %}" 
                           class="pagination-link">{{ num }}</a>
                    {% endif %}
                {% endfor %}
                
                {% if page_obj.has_next %}
                    <a href="?page={{ page_obj.next_page_number }}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}{% if request.GET.country %}&country={{ request.GET.country }}{% endif %}" 
                       class="pagination-link">Next</a>
                {% endif %}
            </nav>
        </div>
    {% endif %}
</div>
{% endblock %}
