"use client"; import { useEffect, useState } from "react"; import { getDatabaseIntegrity, repairDatabaseSchema, optimizeDatabaseIndices } from "@/lib/actions/admin"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Badge } from "@/components/ui/badge"; import { Loader2, RefreshCw, Wrench, Database, AlertTriangle, CheckCircle2, XCircle, Terminal, ArrowLeft } from "lucide-react"; import Link from "next/link"; interface TableStatus { exists: boolean; count: number; } interface IntegrityData { tables: { profiles: TableStatus; end_customers: TableStatus; pos_modules: TableStatus; licenses: TableStatus; }; errors: { foreign_keys: number; }; } export default function AdminToolsPage() { const [integrity, setIntegrity] = useState(null); const [isLoading, setIsLoading] = useState(false); const [logs, setLogs] = useState([]); const [activeAction, setActiveAction] = useState(null); const addLog = (message: string) => { const timestamp = new Date().toLocaleTimeString(); setLogs((prev) => [...prev, `[${timestamp}] ${message}`]); }; const loadStatus = async (quiet = false) => { if (!quiet) setIsLoading(true); addLog("Starte Integritätsscan der Datenbank..."); const res = await getDatabaseIntegrity(); if (res.success && res.data) { setIntegrity(res.data as IntegrityData); addLog("Integritätsscan erfolgreich abgeschlossen."); } else { addLog(`FEHLER beim Scannen: ${res.error}`); } if (!quiet) setIsLoading(false); }; const handleRepair = async () => { setIsLoading(true); setActiveAction("repair"); addLog("Starte automatische Schemareparatur..."); const res = await repairDatabaseSchema(); if (res.success && res.data) { const repaired = (res.data as any).repaired || []; if (repaired.length > 0) { addLog(`Erfolgreich reparierte Tabellen: ${repaired.join(", ")}`); } else { addLog("Keine fehlenden Tabellen gefunden. Schema intakt."); } await loadStatus(true); } else { addLog(`FEHLER bei Schemareparatur: ${res.error}`); } setIsLoading(false); setActiveAction(null); }; const handleOptimize = async () => { setIsLoading(true); setActiveAction("optimize"); addLog("Starte Reindexierung des Schemas public..."); const res = await optimizeDatabaseIndices(); if (res.success) { addLog("Reindexierung erfolgreich abgeschlossen. Suchindizes neu aufgebaut."); } else { addLog(`FEHLER bei Reindexierung: ${res.error}`); } setIsLoading(false); setActiveAction(null); }; useEffect(() => { loadStatus(); }, []); return (
{/* Background decoration */}
{/* Loading Overlay */} {isLoading && (

{activeAction === "repair" ? "Schema wird repariert..." : activeAction === "optimize" ? "Datenbank wird indexiert..." : "Verbindung zur Datenbank wird aufgebaut..."}

)}
{/* Header */}

Admin Datenbank-Cockpit

Verwalten und reparieren Sie Systemtabellen, Sicherheitsrichtlinien (RLS) und Datenbank-Indizes.

{/* Action Controls */}
Integritätsscan Prüft die physische Existenz der Tabellen und zählt deren Zeilenanzahl. Schema reparieren Erstellt fehlende Kerntabellen automatisch neu und konfiguriert die RLS-Rechte. Index optimieren Baut korrupte Suchindizes im Schema public mittels REINDEX neu auf.
{/* Tabellenstatus */} Tabellen-Status & Diagnose
{integrity?.tables ? ( Object.entries(integrity.tables).map(([name, status]) => (
{name.replace("_", " ")}
Zeilen: {status.count} {status.exists ? ( OK ) : ( Fehlt )}
)) ) : (
Lade Diagnosedaten...
)}
{/* Foreign Key Errors */} {integrity && integrity.errors.foreign_keys > 0 && (

Fremdschlüssel-Fehler gefunden!

Es wurden {integrity.errors.foreign_keys} verwaiste Lizenzen ohne zugeordneten Endkunden in der Tabelle `licenses` detektiert.

)}
{/* Console / Output logs */} Diagnose-Konsole
{logs.length === 0 ? ( Konsole bereit. Führen Sie einen Scan aus. ) : ( logs.map((log, index) => (
{log}
)) )}
); }