from dash import html
import dash_daq as daq
import dash_bootstrap_components as dbc
from bfabric_web_apps.utils.config import settings

def expired(base_url=None):
    """
    Returns an error message for expired sessions with a login link.

    Args:
        base_url (str, optional): The B-Fabric instance base URL.
                                  Defaults to VALIDATION_INSTANCE_URL if not provided.

    Returns:
        list: Dash HTML components for the expired session message.
    """
    url = base_url or settings.VALIDATION_INSTANCE_URL
    return [
        html.P("Your session has expired. Please log into bfabric to continue:"),
        html.A('Login to Bfabric', href=url)
    ]

def no_entity(base_url=None):
    """
    Returns an error message when entity data cannot be fetched.

    Args:
        base_url (str, optional): The B-Fabric instance base URL.
                                  Defaults to VALIDATION_INSTANCE_URL if not provided.

    Returns:
        list: Dash HTML components for the no entity error message.
    """
    url = base_url or settings.VALIDATION_INSTANCE_URL
    return [
        html.P("There was an error fetching the data for your entity. Please try accessing the applicaiton again from bfabric:"),
        html.A('Login to Bfabric', href=url)
    ]

def no_auth(base_url=None):
    """
    Returns an error message for unauthenticated users with a login link.

    Args:
        base_url (str, optional): The B-Fabric instance base URL.
                                  Defaults to VALIDATION_INSTANCE_URL if not provided.

    Returns:
        dbc.Alert: A warning-styled alert component.
    """
    url = base_url or settings.VALIDATION_INSTANCE_URL
    return dbc.Alert([
        html.H4("Authentication Required", className="alert-heading"),
        html.P("You are not currently logged into an active session."),
        html.Hr(),
        html.P([
            "Please ",
            html.A('log into B-Fabric', href=url, className="alert-link"),
            " to continue."
        ], className="mb-0")
    ], color="warning")

def access_denied(message=None, base_url=None):
    """
    Returns an error message for users who lack API permissions.

    Use this when a user is authenticated but doesn't have the required
    permissions (e.g., not an admin, application manager, or feeder).

    Args:
        message (str, optional): Custom message explaining what permissions are needed.
                                 Defaults to a generic web services permission message.
        base_url (str, optional): The B-Fabric instance base URL.
                                  Defaults to VALIDATION_INSTANCE_URL if not provided.

    Returns:
        dbc.Alert: A danger-styled alert component.
    """
    url = base_url or settings.VALIDATION_INSTANCE_URL
    default_message = "Only administrators, application managers, and feeders can use web services."
    return dbc.Alert([
        html.H4("Access Denied", className="alert-heading"),
        html.P(message or default_message),
        html.Hr(),
        html.P([
            "Please contact your B-Fabric administrator if you need access, or ",
            html.A('return to B-Fabric', href=url, className="alert-link"),
            "."
        ], className="mb-0")
    ], color="danger")


def not_elevated(message=None, base_url=None):
    """
    Returns a message for authenticated users without web service access.

    Use this when a user is authenticated but webServiceUser=False,
    meaning they don't have elevated permissions for API calls.

    Args:
        message (str, optional): Custom message explaining the restriction.
                                 Defaults to a standard web service access message.
        base_url (str, optional): The B-Fabric instance base URL.
                                  Defaults to VALIDATION_INSTANCE_URL if not provided.

    Returns:
        dbc.Alert: A danger-styled alert component.
    """
    url = base_url or settings.VALIDATION_INSTANCE_URL
    default_message = "This application requires elevated permissions (web service access). Please contact an administrator if you believe this is an error."
    return dbc.Alert([
        html.H4("Elevated Access Required", className="alert-heading"),
        html.P(message or default_message),
        html.Hr(),
        html.P([
            "Please contact your B-Fabric administrator to request access, or ",
            html.A('return to B-Fabric', href=url, className="alert-link"),
            "."
        ], className="mb-0")
    ], color="danger")

dev = [html.P("This page is under development. Please check back later."),html.Br(),html.A("email the developer for more details",href=f"mailto:{settings.DEVELOPER_EMAIL_ADDRESS}")]

auth = [html.Div(id="auth-div")]

charge_switch = [
    daq.BooleanSwitch(id='charge_run', on=True, label="Charge project for run"),
    html.Br()
]
