from django import template

register = template.Library()


# Status configuration mapping for parks and rides
STATUS_CONFIG = {
    "OPERATING": {
        "label": "Operating",
        "classes": "bg-green-100 text-green-800 dark:bg-green-900 dark:text-green-200",
        "icon": True,
    },
    "CLOSED_TEMP": {
        "label": "Temporarily Closed",
        "classes": "bg-yellow-100 text-yellow-800 dark:bg-yellow-900 dark:text-yellow-200",
        "icon": True,
    },
    "CLOSED_PERM": {
        "label": "Permanently Closed",
        "classes": "bg-red-100 text-red-800 dark:bg-red-900 dark:text-red-200",
        "icon": True,
    },
    "CONSTRUCTION": {
        "label": "Under Construction",
        "classes": "bg-orange-100 text-orange-800 dark:bg-orange-900 dark:text-orange-200",
        "icon": True,
    },
    "DEMOLISHED": {
        "label": "Demolished",
        "classes": "bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300",
        "icon": True,
    },
    "RELOCATED": {
        "label": "Relocated",
        "classes": "bg-purple-100 text-purple-800 dark:bg-purple-900 dark:text-purple-200",
        "icon": True,
    },
    "SBNO": {
        "label": "Standing But Not Operating",
        "classes": "bg-amber-100 text-amber-800 dark:bg-amber-900 dark:text-amber-200",
        "icon": True,
    },
}

# Default config for unknown statuses
DEFAULT_STATUS_CONFIG = {
    "label": "Unknown",
    "classes": "bg-gray-100 text-gray-800 dark:bg-gray-700 dark:text-gray-300",
    "icon": False,
}


@register.filter
def get_status_config(status):
    """
    Get status badge configuration for a given status value.

    Usage:
        {% with config=status|get_status_config %}
            <span class="{{ config.classes }}">{{ config.label }}</span>
        {% endwith %}

    Args:
        status: Status string (e.g., 'OPERATING', 'CLOSED_TEMP')

    Returns:
        Dictionary with 'label', 'classes', and 'icon' keys
    """
    if status is None:
        return DEFAULT_STATUS_CONFIG
    return STATUS_CONFIG.get(status, DEFAULT_STATUS_CONFIG)


@register.filter
def has_reviewed_park(user, park):
    """Check if a user has reviewed a park"""
    if not user.is_authenticated:
        return False
    return park.reviews.filter(user=user).exists()
