from django.shortcuts import render
from django.templatetags.static import static


def _render_error(request, code, title, message, buttons, illustration_class, extra_context=None):
    current_url = ""
    try:
        current_url = request.build_absolute_uri(request.path)
    except Exception:
        current_url = ""

    context = {
        "status_code": code,
        "error_title": title,
        "error_message": message,
        "error_buttons": buttons,
        "illustration_class": illustration_class,
        "seo_meta": {
            "title": f"{title} | Aarya Holidays & Cabs",
            "description": message,
            "keywords": "error page, aarya holidays, travel error",
            "canonical_url": request.build_absolute_uri("/") if request else "",
            "robots": "noindex,nofollow",
            "og_image": static("images/logo.jpeg"),
            "site_name": "Aarya Holidays & Cabs",
            "absolute_url": current_url,
        },
    }
    if extra_context:
        context.update(extra_context)
    return render(request, "travel/error_page.html", context, status=code)


def bad_request(request, exception=None):
    buttons = [
        {"label": "Go Home", "url": "/", "style": "primary"},
        {"label": "Try Again", "url": request.path, "style": "outline"},
    ]
    return _render_error(
        request,
        400,
        "Invalid Request",
        "The request couldn't be processed. Please try again.",
        buttons,
        "error-illustration-alert",
    )


def permission_denied(request, exception=None):
    buttons = [
        {"label": "Go Home", "url": "/", "style": "primary"},
        {"label": "Login", "url": "/admin-login/", "style": "outline"},
    ]
    return _render_error(
        request,
        403,
        "Access Restricted",
        "You don't have permission to access this page.",
        buttons,
        "error-illustration-lock",
    )


def page_not_found(request, exception=None):
    buttons = [
        {"label": "Back to Home", "url": "/", "style": "primary"},
        {"label": "Browse Tours", "url": "/holiday-packages/", "style": "outline"},
        {"label": "Contact Us", "url": "/contact/", "style": "outline"},
        {"label": "Search Packages", "url": "/holiday-packages/", "style": "outline"},
    ]
    return _render_error(
        request,
        404,
        "Oops! Page Not Found",
        "The page you're looking for doesn't exist or may have been moved.",
        buttons,
        "error-illustration-map",
    )


def server_error(request):
    buttons = [
        {"label": "Refresh Page", "url": request.path, "style": "primary"},
        {"label": "Go Home", "url": "/", "style": "outline"},
        {"label": "Contact Support", "url": "/contact/", "style": "outline"},
    ]
    return _render_error(
        request,
        500,
        "Something Went Wrong",
        "We're working to fix this issue. Please try again in a few minutes.",
        buttons,
        "error-illustration-server",
    )


def service_unavailable(request, exception=None):
    buttons = [
        {"label": "Go Home", "url": "/", "style": "primary"},
        {"label": "Contact Us", "url": "/contact/", "style": "outline"},
    ]
    return _render_error(
        request,
        503,
        "We'll Be Back Soon",
        "The website is currently under maintenance.",
        buttons,
        "error-illustration-construction",
    )
