import { redirect } from "next/navigation";
import AdminShell from "@/components/admin/AdminShell";
import StatusSelect from "@/components/admin/StatusSelect";
import { requireAdminSession } from "@/lib/admin/auth";
import { getRepairInquiries } from "@/lib/admin/store";

export default async function AdminRepairsPage() {
  if (!await requireAdminSession()) redirect("/admin/login");
  const inquiries = await getRepairInquiries();

  return (
    <AdminShell>
      <div className="admin-page-head">
        <p className="eyebrow">Anfragen</p>
        <h1>Reparaturanfragen</h1>
      </div>
      <section className="admin-card">
        <table className="admin-table">
          <thead><tr><th>Datum</th><th>Name</th><th>Gerät</th><th>Hersteller</th><th>Modell</th><th>Status</th></tr></thead>
          <tbody>
            {inquiries.map((item) => (
              <tr key={item.id}>
                <td>{new Date(item.createdAt).toLocaleDateString("de-DE")}</td>
                <td>{item.name}</td>
                <td>{item.deviceType}</td>
                <td>{item.manufacturer}</td>
                <td>{item.model}</td>
                <td><StatusSelect id={item.id} type="repair" status={item.status} /></td>
              </tr>
            ))}
            {inquiries.length === 0 && <tr><td colSpan={6}>Noch keine Reparaturanfragen vorhanden.</td></tr>}
          </tbody>
        </table>
      </section>
      <section className="admin-card">
        <h2>Technische Details</h2>
        <div className="admin-detail-grid">
          {inquiries.slice(0, 6).map((item) => (
            <article key={item.id} className="admin-detail">
              <h3>{item.manufacturer} {item.model}</h3>
              <p><strong>Fehler:</strong> {item.description}</p>
              <p><strong>Zubehör:</strong> {item.accessories || "nicht angegeben"}</p>
              <p><strong>Vorarbeiten:</strong> {item.previousWork || "nicht angegeben"}</p>
              <button className="button light" type="button" disabled>In Olympus übernehmen</button>
            </article>
          ))}
        </div>
      </section>
    </AdminShell>
  );
}
