async function triggerLibraryScan(): Promise<void> {
    const token = localStorage.getItem('token');
    if (!token) return;

    try {
        const response = await fetch('/api/libraries/scan', {
            method: 'POST',
            headers: { 'Authorization': `Bearer ${token}` }
        });

        if (response.ok) {
            if ((window as any).showToast?.success) {
                (window as any).showToast.success('Library scan started');
            }
        } else {
            const error = await response.json();
            if ((window as any).showToast?.error) {
                (window as any).showToast.error(error.error || 'Failed to start scan');
            }
        }
    } catch (error) {
        console.error('Scan error:', error);
        if ((window as any).showToast?.error) {
            (window as any).showToast.error('Failed to start library scan');
        }
    }
}

async function triggerQuickScan(): Promise<void> {
    const token = localStorage.getItem('token');
    if (!token) return;

    try {
        const response = await fetch('/api/libraries/quick-scan', {
            method: 'POST',
            headers: { 'Authorization': `Bearer ${token}` }
        });

        if (response.ok) {
            if ((window as any).showToast?.success) {
                (window as any).showToast.success('Quick scan started');
            }
        } else {
            const error = await response.json();
            if ((window as any).showToast?.error) {
                (window as any).showToast.error(error.error || 'Failed to start quick scan');
            }
        }
    } catch (error) {
        console.error('Quick scan error:', error);
        if ((window as any).showToast?.error) {
            (window as any).showToast.error('Failed to start quick scan');
        }
    }
}

async function loadSystemStats(): Promise<void> {
    const token = localStorage.getItem('token');
    if (!token) return;

    try {
        const response = await fetch('/api/admin/stats', {
            headers: { 'Authorization': `Bearer ${token}` }
        });

        if (response.ok) {
            const stats = await response.json();
            renderSystemStats(stats);
        }
    } catch (error) {
        console.error('Failed to load stats:', error);
    }
}

function renderSystemStats(stats: Record<string, unknown>): void {
    const container = document.getElementById('system-stats');
    if (!container) return;

    container.innerHTML = `
        <div class="grid grid-cols-2 md:grid-cols-4 gap-4">
            <div class="p-4 rounded-lg" style="background-color: var(--bg-secondary)">
                <p class="text-2xl font-bold" style="color: var(--text-primary)">${stats.total_books || 0}</p>
                <p class="text-sm" style="color: var(--text-secondary)">Total Books</p>
            </div>
            <div class="p-4 rounded-lg" style="background-color: var(--bg-secondary)">
                <p class="text-2xl font-bold" style="color: var(--text-primary)">${stats.total_users || 0}</p>
                <p class="text-sm" style="color: var(--text-secondary)">Users</p>
            </div>
            <div class="p-4 rounded-lg" style="background-color: var(--bg-secondary)">
                <p class="text-2xl font-bold" style="color: var(--text-primary)">${stats.total_devices || 0}</p>
                <p class="text-sm" style="color: var(--text-secondary)">Devices</p>
            </div>
            <div class="p-4 rounded-lg" style="background-color: var(--bg-secondary)">
                <p class="text-2xl font-bold" style="color: var(--text-primary)">${stats.total_libraries || 0}</p>
                <p class="text-sm" style="color: var(--text-secondary)">Libraries</p>
            </div>
        </div>
    `;
}

(window as any).triggerLibraryScan = triggerLibraryScan;
(window as any).triggerQuickScan = triggerQuickScan;
(window as any).loadSystemStats = loadSystemStats;
