"""
Alerts Widget fuer das Lehrer-Dashboard.

Zeigt Google Alerts und andere Benachrichtigungen.
"""


class AlertsWidget:
    widget_id = 'alerts'
    widget_name = 'Alerts'
    widget_icon = '&#128276;'  # Bell
    widget_color = '#f59e0b'  # Orange
    default_width = 'half'
    has_settings = True

    @staticmethod
    def get_css() -> str:
        return """
/* ===== Alerts Widget Styles ===== */
.widget-alerts {
    background: var(--bp-surface, #1e293b);
    border: 1px solid var(--bp-border, #475569);
    border-radius: 12px;
    padding: 16px;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.widget-alerts .widget-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--bp-border-subtle, rgba(255,255,255,0.1));
}

.widget-alerts .widget-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    font-weight: 600;
    color: var(--bp-text, #e5e7eb);
}

.widget-alerts .widget-icon {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(245, 158, 11, 0.15);
    color: #f59e0b;
    border-radius: 8px;
    font-size: 14px;
}

.widget-alerts .alerts-list {
    flex: 1;
    overflow-y: auto;
}

.widget-alerts .alert-item {
    display: flex;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--bp-border-subtle, rgba(255,255,255,0.05));
    cursor: pointer;
    transition: background 0.2s;
}

.widget-alerts .alert-item:last-child {
    border-bottom: none;
}

.widget-alerts .alert-item:hover {
    background: var(--bp-surface-elevated, #334155);
    margin: 0 -12px;
    padding: 12px;
    border-radius: 8px;
}

.widget-alerts .alert-icon {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(245, 158, 11, 0.15);
    color: #f59e0b;
    border-radius: 8px;
    font-size: 14px;
    flex-shrink: 0;
}

.widget-alerts .alert-content {
    flex: 1;
    min-width: 0;
}

.widget-alerts .alert-title {
    font-size: 13px;
    font-weight: 500;
    color: var(--bp-text, #e5e7eb);
    margin-bottom: 4px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.widget-alerts .alert-meta {
    font-size: 11px;
    color: var(--bp-text-muted, #9ca3af);
    display: flex;
    gap: 8px;
}

.widget-alerts .alert-source {
    color: #f59e0b;
}

.widget-alerts .alerts-empty {
    text-align: center;
    padding: 24px;
    color: var(--bp-text-muted, #9ca3af);
    font-size: 13px;
}

.widget-alerts .alerts-empty-icon {
    font-size: 32px;
    margin-bottom: 8px;
    opacity: 0.5;
}

.widget-alerts .alerts-footer {
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--bp-border-subtle, rgba(255,255,255,0.1));
}

.widget-alerts .alerts-all-btn {
    width: 100%;
    padding: 10px;
    background: transparent;
    border: 1px dashed var(--bp-border, #475569);
    border-radius: 8px;
    color: var(--bp-text-muted, #9ca3af);
    font-size: 12px;
    cursor: pointer;
    transition: all 0.2s;
}

.widget-alerts .alerts-all-btn:hover {
    border-color: #f59e0b;
    color: #f59e0b;
}
"""

    @staticmethod
    def get_html() -> str:
        return """
<div class="widget-alerts" data-widget-id="alerts">
    <div class="widget-header">
        <div class="widget-title">
            <span class="widget-icon">&#128276;</span>
            <span>Google Alerts</span>
        </div>
        <button class="widget-settings-btn" onclick="openWidgetSettings('alerts')" title="Einstellungen">&#9881;</button>
    </div>
    <div class="alerts-list" id="alerts-widget-list">
        <!-- Wird dynamisch gefuellt -->
    </div>
    <div class="alerts-footer">
        <button class="alerts-all-btn" onclick="openAlertsModule()">+ Alle Alerts anzeigen</button>
    </div>
</div>
"""

    @staticmethod
    def get_js() -> str:
        return """
// ===== Alerts Widget JavaScript =====

function getDefaultAlerts() {
    const now = Date.now();
    return [
        {
            id: 1,
            title: 'Neue Mathelehrer-Studie zeigt verbesserte Lernergebnisse durch digitale Tools',
            source: 'Google Alert: Digitales Lernen',
            url: '#',
            time: new Date(now - 60 * 60 * 1000).toISOString()
        },
        {
            id: 2,
            title: 'Kultusministerium kuendigt neue Fortbildungsreihe an',
            source: 'Google Alert: Bildungspolitik NI',
            url: '#',
            time: new Date(now - 5 * 60 * 60 * 1000).toISOString()
        },
        {
            id: 3,
            title: 'Best Practices: Differenzierter Deutschunterricht',
            source: 'Google Alert: Deutschunterricht',
            url: '#',
            time: new Date(now - 24 * 60 * 60 * 1000).toISOString()
        }
    ];
}

function formatAlertTime(timeStr) {
    const time = new Date(timeStr);
    const now = new Date();
    const diffMs = now - time;
    const diffHours = Math.floor(diffMs / (60 * 60 * 1000));
    const diffDays = Math.floor(diffMs / (24 * 60 * 60 * 1000));

    if (diffHours < 1) return 'gerade eben';
    if (diffHours < 24) return `vor ${diffHours} Std.`;
    if (diffDays === 1) return 'gestern';
    return `vor ${diffDays} Tagen`;
}

function renderAlertsWidget() {
    const list = document.getElementById('alerts-widget-list');
    if (!list) return;

    const alerts = getDefaultAlerts();

    if (alerts.length === 0) {
        list.innerHTML = `
            <div class="alerts-empty">
                <div class="alerts-empty-icon">&#128276;</div>
                <div>Keine neuen Alerts</div>
            </div>
        `;
        return;
    }

    list.innerHTML = alerts.map(alert => `
        <div class="alert-item" onclick="openAlert('${alert.url}')">
            <div class="alert-icon">&#128240;</div>
            <div class="alert-content">
                <div class="alert-title">${alert.title}</div>
                <div class="alert-meta">
                    <span class="alert-source">${alert.source}</span>
                    <span>${formatAlertTime(alert.time)}</span>
                </div>
            </div>
        </div>
    `).join('');
}

function openAlert(url) {
    if (url && url !== '#') {
        window.open(url, '_blank');
    }
}

function openAlertsModule() {
    if (typeof loadModule === 'function') {
        loadModule('alerts');
    }
}

function initAlertsWidget() {
    renderAlertsWidget();
}
"""
