import { redirect } from "next/navigation";
import AdminShell from "@/components/admin/AdminShell";
import { requireAdminSession } from "@/lib/admin/auth";
import { adminConfigurationStatus, appVersion, checkStorage, configDirectory, dataDirectory, isDockerEnvironment, olympusStatus, uploadDirectory } from "@/lib/runtime/config";
import { getSmtpSettings, isSmtpConfigured } from "@/lib/mail/config";
import { getContentSystemStatus } from "@/lib/content/service";

export default async function AdminSystemPage() {
  if (!await requireAdminSession()) redirect("/admin/login");
  let storage = "ok";

  try {
    await checkStorage();
  } catch {
    storage = "error";
  }
  const [smtpSettings, contentStatus] = await Promise.all([getSmtpSettings(), getContentSystemStatus()]);
  const smtpConfigured = isSmtpConfigured(smtpSettings);

  return (
    <AdminShell>
      <div className="admin-page-head">
        <p className="eyebrow">Betrieb</p>
        <h1>System</h1>
      </div>
      <section className="admin-card">
        <h2>Runtime Informationen</h2>
        <dl className="system-list">
          <div><dt>Version</dt><dd>{appVersion}</dd></div>
          <div><dt>Node Version</dt><dd>{process.version}</dd></div>
          <div><dt>Docker Environment</dt><dd>{isDockerEnvironment() ? "ja" : "nein"}</dd></div>
          <div><dt>Storage Status</dt><dd>{storage}</dd></div>
          <div><dt>Data Directory</dt><dd>{dataDirectory}</dd></div>
          <div><dt>Config Directory</dt><dd>{configDirectory}</dd></div>
          <div><dt>Upload Directory</dt><dd>{uploadDirectory}</dd></div>
          <div><dt>Admin Konfiguration</dt><dd>{adminConfigurationStatus()}</dd></div>
          <div><dt>Olympus Verbindung</dt><dd>{olympusStatus()}</dd></div>
          <div><dt>SMTP</dt><dd>{smtpConfigured ? "configured" : "missing"}</dd></div>
          <div><dt>Letzte SMTP-Testmail</dt><dd>{smtpSettings.lastTestAt ? `${new Date(smtpSettings.lastTestAt).toLocaleString("de-DE")} (${smtpSettings.lastTestStatus})` : "keine"}</dd></div>
          <div><dt>Letzter SMTP-Formularversand</dt><dd>{smtpSettings.lastDeliveryAt ? `${new Date(smtpSettings.lastDeliveryAt).toLocaleString("de-DE")} (${smtpSettings.lastDeliveryStatus})` : "keiner"}</dd></div>
          <div><dt>Content Version</dt><dd>{contentStatus.version}</dd></div>
          <div><dt>Letzte Content-Änderung</dt><dd>{contentStatus.lastModified ? new Date(contentStatus.lastModified).toLocaleString("de-DE") : "keine"}</dd></div>
          <div><dt>Anzahl Seiten</dt><dd>{contentStatus.pageCount}</dd></div>
          <div><dt>Anzahl Medien</dt><dd>{contentStatus.mediaCount}</dd></div>
        </dl>
      </section>
      <section className="admin-card">
        <h2>Betriebsregeln</h2>
        <ul className="list">
          <li>Runtime-Daten liegen unter <code>data/</code> und werden nicht versioniert.</li>
          <li>Uploads liegen unter <code>storage/uploads/images/</code> und werden nicht versioniert.</li>
          <li>SMTP-Versand ist serverseitig aktiv, sofern eine vollständige Konfiguration gespeichert ist.</li>
        </ul>
      </section>
      <section className="admin-card">
        <h2>Letzte Content-Backups</h2>
        <table className="admin-table">
          <thead><tr><th>Seite</th><th>Datum</th><th>Dateiname</th></tr></thead>
          <tbody>
            {contentStatus.backups.map((backup) => (
              <tr key={backup.fileName}>
                <td>{backup.page}</td>
                <td>{new Date(backup.createdAt).toLocaleString("de-DE")}</td>
                <td>{backup.fileName}</td>
              </tr>
            ))}
            {contentStatus.backups.length === 0 && <tr><td colSpan={3}>Noch keine Content-Backups vorhanden.</td></tr>}
          </tbody>
        </table>
      </section>
    </AdminShell>
  );
}
